A place to store instance variables.
# File lib/attic.rb, line 110 def self.extended(o) # This class has already been extended. return if o.ancestors.member? Attic::InstanceMethods ## NOTE: This is just a reminder for a more descerning way to ## include the meta methods, instead of using a global mixin. ##o.class_eval do ## include ObjectHelpers ##end # Create an instance method that returns the attic variables. o.send :include, Attic::InstanceMethods #p [:extend, self, o] o.metaclass.instance_variable_set("@attic_variables", []) o.class_eval do def self.inherited(o2) #p [:inherit, self, o2] attic_vars = self.attic_variables.clone o2.metaclass.instance_variable_set("@attic_variables", attic_vars) end if method_defined? :instance_variables old_instance_variables = instance_method(:instance_variables) define_method :instance_variables do ret = old_instance_variables.bind(self).call.clone ret.reject! { |v| v.to_s =~ /^@___?attic/ } # match 2 or 3 underscores ret end define_method :all_instance_variables do old_instance_variables.bind(self).call end end end end
A class method for defining variables to store in the attic.
junk is a list of variables names. Accessor methods are created for each variable name in the list.
Returns the list of attic variable names or if not junk was given, returns the metaclass.
e.g.
String.extend Attic String.attic :timestamp
In this example, attic created two instance methods:
String#timestamp for getting the value
String#timestamp for setting the value
# File lib/attic.rb, line 164 def attic *junk return metaclass if junk.empty? junk.each do |name| next if attic_variable? name self.attic_variables << name unless method_defined? name define_method(name) do attic_variable_get name end end unless method_defined? "#{name}=" define_method("#{name}=") do |val| attic_variable_set name, val end end end attic_vars end
# File lib/attic.rb, line 198 def attic_variable?(n) attic_variables.member? n end
Returns an Array of attic variables for the current class. e.g.
String.extend Attic String.attic :timestamp String.attic_variables # => [:timestamp]
# File lib/attic.rb, line 191 def attic_variables a = self.metaclass.instance_variable_get("@attic_variables") a ||= self.metaclass.instance_variable_set("@attic_variables", []) a end
Generated with the Darkfish Rdoc Generator 2.