Metaprogramming

Hello,

I am doing one open source project and I hit the wall with this. Now I
am writing lib which would convert custom xml format to Ruby objects and
back to xml. I have finished translating DTD to Classes but it is really
messy and really big and lack many features that I want to do
(https://github.com/gcds/libeagle/blob/master/lib/libeagle/eagle.rb). So
I asking maybe someone could help me to write some methods in meta
programming because I am new with it and cant figure out how to do.
Google doesn’t provide good results. I could use already written
libraries like Doodle, ActiveModel but it would be to heavy. What I want
to do is this:

I am trying to make method which would set getter, setter, default
value, and would check if attribute is filled in.

class Library < Eagle::Base
attribute :name, :default => “Tom”, :required => true
end

Another method that replicates .new method which would take nokogiri doc
and would catch all attributes which are defined with first method and
get values.

xml[‘name’] = “John”
lib = Library.newXML(xml)
lib.name
=> “John”

And the last method To take all atributes which are defined with the
first one (maybe save attribute name to array?) run validations and make
xml block from that attributes.

So if someone could help with this things I would be really appreciate.
And your help will help to release this project to open community. Thank
you.

Aurimas

This is what I have done with this so far:

Aurimas N. wrote in post #1085124:

I am trying to make method which would set getter, setter, default
value, and would check if attribute is filled in.

That makes no sense. You are going to have to learn enough ruby to be
able to talk ruby. I suggest you read a beginning ruby book.

Another method that replicates .new method which would take nokogiri doc
and would catch all attributes which are defined with first method and
get values.

Also nearly incomprehensible.

xml[‘name’] = “John”
lib = Library.newXML(xml)
lib.name
=> “John”

Okay, I can see something in there.

require ‘delegate’

class XML
attr_accessor :name, :date
end

class Lib < SimpleDelegator
end

xml = XML.new
xml.name = “John”
lib = Lib.new(xml)
puts lib.name

–output:–
John

And the last method To take all atributes which are defined with the
first one…

Complete gibberish. You need to learn some basic XML, too; so
that you can speak XML as well.

Arlen C. wrote in post #1085299:

This is not difficult to understand, particularly in the context of the
code examples given.

Well, then why don’t you rewrite the op’s post so that English speakers
can understand it, and then maybe we can get the op’s questions
answered?

7stud, you are being needlessly belligerent. A moment’s Googling would
tell you that Aurimas is not a native English speaker, and you should
adjust your attitude accordingly.

Another method that replicates .new method which would take nokogiri doc
and would catch all attributes which are defined with first method and
get values.

This is not difficult to understand, particularly in the context of the
code examples given.

On Nov 20, 2012, at 19:06, 7stud – [email protected] wrote:

Arlen C. wrote in post #1085299:

This is not difficult to understand, particularly in the context of the
code examples given.

Well, then why don’t you rewrite the op’s post so that English speakers
can understand it, and then maybe we can get the op’s questions
answered?

You are acting like a dick. Stop. If you can’t contribute positively,
stfu.

:DDD Why everybody thinks I do not know anything about ruby?

I have already figured out the way that I want to do. Here you can look
at it maybe something better you would suggest:

Quit feeding the troll. Matz is nice, so we should be as well.

Now then, you say getters and setters. I believe this puts you at a C
like
background.

Look into attr_accessor, reader, and writer.

This tutorial should prove invaluable:

Note that Ruby implicitly returns the last operation performed in a
function. You can still explicitly call return if you want.

Def name is the getter, Def name= is the setter. Again, read into attr,
as
it saves quite a bit of time.

Best of luck mate,

Lemur

The code looks fine to me - is there any particular piece you aren’t
happy with?

One improvement I can see is that self.attribute could be def
self.attribute(name, params = {})

1.9.3-p327 :016 > def f(name, params={})
1.9.3-p327 :017?> p [name, params]
1.9.3-p327 :018?> end
=> nil
1.9.3-p327 :019 > f(‘hello’)
[“hello”, {}]
=> [“hello”, {}]
1.9.3-p327 :020 > f(‘a’, :b => true, :c => ‘d’)
[“a”, {:b=>true, :c=>“d”}]
=> [“a”, {:b=>true, :c=>“d”}]

That will save you the if params check everywhere.

martin

Oh, thank you really much!! It’s my first gem :stuck_out_tongue:

One more suggestion - you really need a README explaining what Eagle
is (or at least a pointer to the Eagle website) :slight_smile:

martin

Yep, I know. I will write normal readme file today. I am creating other
tool and I thought maybe it would be nice to give away library which
parses these files.

P.S. Maybe someone has some unwritten rules for gems?