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.3'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(o) ⇒ Object


110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# 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) ⇒ Object


106
107
108
# File 'lib/attic.rb', line 106

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


164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# 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) ⇒ Boolean

Returns:

  • (Boolean)

198
199
200
# File 'lib/attic.rb', line 198

def attic_variable?(n)
  attic_variables.member? n
end

#attic_variablesObject 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]

191
192
193
194
195
# 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