Module: Marley::Utils::ClassAttrs
- Included in:
- Joints::Joint, Plugins::Plugin
- Defined in:
- lib/marley/utils.rb
Instance Method Summary collapse
- #class_attr(attr_name, val = nil, op = nil, &block) ⇒ Object
- #lazy_class_attrs(key_proc, atts, op = nil, &block) ⇒ Object
Instance Method Details
#class_attr(attr_name, val = nil, op = nil, &block) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/marley/utils.rb', line 25 def class_attr(attr_name, val=nil, op=nil, &block) block||=op ? lambda{ |o, x| o.__send__(op, x) } : lambda {|old, new| Marley::Utils.combine(old,new)} extend(Module.new do |m| define_method :"#{attr_name}!" do |*args| if instance_variable_defined?("@#{attr_name}") instance_variable_get("@#{attr_name}") else instance_variable_set("@#{attr_name}", Marshal.load(Marshal.dump(val))) end end define_method attr_name.to_sym do ancestors.reverse.inject(Marshal.load(Marshal.dump(val))) do |v, a| if a.respond_to?(:"#{attr_name}!") block.call(v,a.__send__(:"#{attr_name}!")) else v end end end end) end |
#lazy_class_attrs(key_proc, atts, op = nil, &block) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/marley/utils.rb', line 5 def lazy_class_attrs(key_proc,atts,op=nil,&block) atts.to_a.each do |att| att=[att] unless att.is_a?(Array) class_attr(att[0], {key_proc => att[1]}, op, &block) include(Module.new do |m| define_method :"_#{att[0]}" do a=self.class.send(att[0]) a.keys.inject(nil) {|res,key| if self.respond_to?(key) all=a[key][:all] v=(a[key].has_key?(dyn_key=self.send(key)) && a[key][dyn_key] ) || all || res Marley::Utils.combine(res,Marley::Utils.combine(all, v)) else Marley::Utils.combine(res,a[key]) end } end end) end end |