我想动态定义基于xml映射的方法。这工作得很好。不过,我想创建一个实例变量,这是一个动态定义的方法的数组。如何动态更新实例数组来保存实例化的动态方法列表?
我的代码看起来是这样的
def xml_attr_reader(*args)
xml_list = ""
args.each do |arg|
string_val = "def #{arg}; " +
" xml_mapping.#{arg}; " +
"end; "
self.class_eval string_val
xml_hash = xml_list + "'#{arg}',"
end
self.class_eval "@xml_attributes = [] if @xml_attributes.nil?;" +
"@xml_attributes = @xml_attributes + [#{xml_list}];" +
"puts 'xml_attrs = ' + @xml_attributes.to_s;" +
"def xml_attributes;" +
" puts 'xml_attrs = ' + @xml_attributes.to_s;" +
" @xml_attributes;" +
"end"
end
所以一切正常,除了当我请一个实例xml_attributes它返回null(并打印出“xml_attrs =”)。
而定义之前的放置实际上打印出正确的数组。 (当我实例化实例时)
更新 - kandadaboggu解决方案如下工作。我真的没有完全解释我的问题,所以这里有更多的信息。我想在扩展XmlConfig的类中调用xml_attr_reader。基本上我希望它能够像活动记录的attr_reader一样工作。
考虑的
class XmlConfig
def xml_attr_reader(*args)
args.each do |arg|
class_eval(<<-RUBY, __FILE__, __LINE__)
def #{arg}
xml_mapping.#{arg}
end
RUBY
end
unless respond_to?(:xml_attributes)
class_eval(<<-RUBY, __FILE__, __LINE__)
attr_accessor :xml_attributes
RUBY
end
(self.xml_attributes ||=[]).concat(args)
end
end
和
config = XmlConfig.new
config.xml_attr_reader("age", "name")
config.age #=> age
config.name #=> name
config.xml_attributes #=> ["age", "name" ]
config.xml_attr_reader("city")
config.city #=> city
config.xml_attributes #=> ["age", "name", "city"]
kandadaboggu解决我真正想要的,这是(在那里我有XMLConfig的一个模块不是在我的版本的类虽然)
class Event < ActiveWhatever
extend XmlConfig
xml_attr_reader :age, :name
xml_attr_reader :email, :location
end
class PrivateEvent < Event
xml_attr_reader :owner, :password
end
优秀的答案。奇迹般有效。感谢坚持这一个,非常感谢。 – Will 2010-03-12 23:05:10