module Attic
Attic¶ ↑
A place to store instance variables.
Constants
- VERSION
Public Class Methods
extended(o)
click to toggle source
# 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
included(o)
click to toggle source
# File lib/attic.rb, line 106 def self.included(o) raise "You probably meant to 'extend Attic' in #{o}" end
inherited(o2)
click to toggle source
# File lib/attic.rb, line 126 def self.inherited(o2) #p [:inherit, self, o2] attic_vars = self.attic_variables.clone o2.metaclass.instance_variable_set("@attic_variables", attic_vars) end
Public Instance Methods
attic(*junk)
click to toggle source
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
attic_variable?(n)
click to toggle source
# File lib/attic.rb, line 198 def attic_variable?(n) attic_variables.member? n end
attic_variables()
click to toggle source
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
Also aliased as: attic_vars