Hi,
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.
However some of the very nice features of this module depend upon being
able to introspect a class to find what attributes it has, and what
their datatypes are.
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...
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 :-)
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
In the java version, when the "cost" attribute is encountered in the xml
input, it is seen that the target class has a setCost(float) method, so
the string "12.50" is converted to a float before invoking the setCost
method.
I want to achieve the same effect in the Ruby version. I do *not* want
to effectively invoke this in ruby:
stock_item.cost=('12.50') # string passed
Anyone have any references to "pre-existing art"???
Thanks,
Simon