class Class
Public Instance Methods
Set
class instance variable attributes, and see that the values get inherited by subclasses. Attribute readers are also set up for object instances that copy it from the class.
The values are ‘dup`ed if possible and simply handed over otherwise (when inheriting and instantiating).
Note that this seems to behave very differently than active-support’s ‘class_attribute` method- which seems to use actual class-variables with all the problems they end up having. (of course I could have just been using active-support’s wrong).
The only safety that keeps these from affecting class instance variables where they shouldn’t is the naming convention. Anything more clever than that and my ruby metaprogramming skills weren’t up to snuff.
Also, as you can surmise, it won’t work if a class uses the ‘inherited` hook and fails to call super.
# File lib/nodus.rb, line 60 def class_attr_inheritable(attr_name, init_as=nil) self.class_eval("def self.#{attr_name};@__cai__#{attr_name} end") self.class_eval("def self.#{attr_name}=(v);@__cai__#{attr_name}=v end") self.send("#{attr_name}=", init_as) unless init_as.nil? self.class_eval("def #{attr_name};@#{attr_name} ||= self.class.#{attr_name}.try_dup end") end
# File lib/nodus.rb, line 67 def inherited(subclass) instance_variables.each do |v| next unless v.to_s.starts_with?('@__cai__') new_val = self.instance_variable_get(v).try_dup subclass.instance_variable_set(v, new_val) end end
# File lib/nodus.rb, line 42 def save_as(klass_name) Object.const_set(klass_name, self) end