lib/classy-inheritance.rb in classy-inheritance-0.2.1 vs lib/classy-inheritance.rb in classy-inheritance-0.3.0
- old
+ new
@@ -37,11 +37,11 @@
if options[:as]
define_can_be_method_on_requisite_class(model_sym, options[:as])
end
- options[:attrs].each{|attr| define_accessors(model_sym, attr)}
+ options[:attrs].each{|attr| define_accessors(model_sym, attr, options[:prefix])}
end
def can_be(model_sym, options = {})
unless options[:as]
@@ -101,15 +101,17 @@
find(*args)
CODE
end
end
- def define_accessors(model_sym, attr)
- define_method attr do
+ def define_accessors(model_sym, attr, prefix)
+ accessor_method_name = ( prefix ? "#{model_sym}_#{attr}" : attr)
+
+ define_method accessor_method_name do
eval("self.#{model_sym} ? self.#{model_sym}.#{attr} : nil")
end
- define_method "#{attr}=" do |val|
+ define_method "#{accessor_method_name}=" do |val|
model_defined = eval("self.#{model_sym}")
unless model_defined
klass = model_sym.to_s.classify
eval("self.#{model_sym} = #{klass}.new")