Module: Attic
- Defined in:
- lib/attic.rb
Overview
Attic
A place to store instance variables.
Defined Under Namespace
Modules: InstanceMethods
Constant Summary collapse
- VERSION =
'0.5.2'
Class Method Summary collapse
Instance Method Summary collapse
-
#attic(*junk) ⇒ Object
A class method for defining variables to store in the attic.
- #attic_variable?(n) ⇒ Boolean
-
#attic_variables ⇒ Object
(also: #attic_vars)
Returns an Array of attic variables for the current class.
Class Method Details
.extended(o) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/attic.rb', line 40 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..instance_variable_set("@attic_variables", []) o.class_eval do def self.inherited(o2) #p [:inherit, self, o2] attic_vars = self.attic_variables.clone o2..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) ⇒ Object
36 37 38 |
# File 'lib/attic.rb', line 36 def self.included(o) raise "You probably meant to 'extend Attic' in #{o}" end |
Instance Method Details
#attic(*junk) ⇒ Object
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
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/attic.rb', line 94 def attic *junk return 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) ⇒ Boolean
128 129 130 |
# File 'lib/attic.rb', line 128 def attic_variable?(n) attic_variables.member? n end |
#attic_variables ⇒ Object Also known as: attic_vars
Returns an Array of attic variables for the current class. e.g.
String.extend Attic
String.attic :timestamp
String.attic_variables # => [:timestamp]
121 122 123 124 125 |
# File 'lib/attic.rb', line 121 def attic_variables a = self..instance_variable_get("@attic_variables") a ||= self..instance_variable_set("@attic_variables", []) a end |