[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Managing metadata about attribute types

Austin Ziegler

11/5/2003 4:10:00 AM

On Wed, 5 Nov 2003 09:38:16 +0900, Simon Kitching wrote:
> I'm porting the Apache Jakarta Commons Digester (written in Java)
> to Ruby at the moment. This module processes xml in a rules-based
> manner. It is particularly useful for handling complex xml
> configuration files.

You may want to note xml-configfile in addition to XMLDigester that
James Britt mentioned.
http://raa.ruby-lang.org/list.rhtml?name=xml-...

> Finding attributes on a Ruby class is simple (just look for
> "attr=" methods). Unfortunately, determining what object types it
> is valid to assign to that attribute is not so simple...

Aside from Ryan Pavlik's StrongTyping module, I'm not sure that this
is absolutely necessary. See below for a bit more information.

> I was wondering if there were any other Ruby projects which have
> faced this problem and come up with solutions? I would rather
> steal a solution than invent one :-)

I frankly don't see a reason to worry about this.

> Example of problem:
>
> Input xml is:
> <stock>
> <stock-item name="spanner" cost="12.50"/>
> <stock-item name="screwdriver" cost="3.80"/>
> </stock>
>
> // java
> class StockItem {
> public void setName(String name) {....}
> public void setCost(float cost) {....}
> }

> # Ruby
> class StockItem
> attr_accessor :name
> attr_accessor :cost
> end


Why not do:

class StockItem
attr_accessor :name # Defaults to String
attr_reader :cost # Returns cost
def cost=(x)
@cost = x.to_f
end

This way, you don't have to care what the appropriate type is --
your type worries about it.

-austin
--
austin ziegler * austin@halostatue.ca * Toronto, ON, Canada
software designer * pragmatic programmer * 2003.11.04
* 22.46.52


1 Answer

Simon Kitching

11/5/2003 4:27:00 AM

0

On Wed, 2003-11-05 at 17:09, Austin Ziegler wrote:
>
> class StockItem
> attr_accessor :name # Defaults to String
> attr_reader :cost # Returns cost
> def cost=(x)
> @cost = x.to_f
> end

Hi Austin,

Thanks for your reply.

One of the goals of xmldigester is to be able to instantiate and
initialise objects from some input xml without making any changes to the
classes themselves.

Thus if you already have a library of warehouse management classes, I
can write some rules that can take an xml description of the contents of
that warehouse and build appropriately configured objects without
changing that library. And once the parsing is complete, the resulting
tree of objects should look no different than one created using normal
calls to the library API.

In addition, the approach you suggest is quite labour-intensive;
for every attribute, a "wrapper" method needs to be written.

I feel Ryan's MetaTags approach is easier to use; a simple string format
can be used to document the types to which strings from the xml input
should be converted before assignment to various attributes.

Regards,

Simon