Module: Attic
- Defined in:
- lib/attic.rb,
lib/attic/class_methods.rb,
lib/attic/instance_methods.rb
Overview
Attic::InstanceMethods
Defined Under Namespace
Modules: ClassMethods, InstanceMethods
Constant Summary collapse
- VERSION =
"1.0.1".freeze
Class Method Summary collapse
-
.construct(obj) ⇒ Object
A convenince method at the class level for including ConstructMethods in the given object specifically.
- .extended(obj) ⇒ Object
-
.included(obj) ⇒ Object
Friendly exception to say we’re not to be included.
Instance Method Summary collapse
-
#attic(*names) ⇒ Object
A class method for defining variables to store in the attic.
- #attic? ⇒ Boolean
- #attic_variable?(name) ⇒ Boolean
-
#attic_variables ⇒ Object
Returns an Array of attic variables for the current class.
Class Method Details
.construct(obj) ⇒ Object
A convenince method at the class level for including ConstructMethods in the given object specifically.
e.g.
Add Attic support to all objects available now and
in the future:
Attic.construct(Object)
which is equivalent to:
class Object; include Attic::ClassMethods; end
119 120 121 |
# File 'lib/attic.rb', line 119 def self.construct(obj) obj.include Attic::ClassMethods end |
.extended(obj) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/attic.rb', line 129 def self.extended(obj) # If the class has already been extended, we don't need # to add the class methods again. return if obj.ancestors.member? self # Add the instance methods for accessing attic variables obj.send :include, Attic::InstanceMethods # If the object doesn't have a dedicated singleton class # an exception will be raised so it can be caught and # handled appropriately. obj.attic.instance_variable_defined?("@attic_variables") obj.attic.instance_variable_set("@attic_variables", []) def obj.inherited(klass) super attic_vars = self.attic_variables.clone klass.attic.instance_variable_set("@attic_variables", attic_vars) end if obj.method_defined? :instance_variables instance_variables_orig = obj.instance_method(:instance_variables) obj.define_method :instance_variables do ret = instance_variables_orig.bind(self).call.clone ret.reject! { |v| v.to_s =~ /^@___?attic/ } # match 2 or 3 underscores ret end obj.define_method :all_instance_variables do instance_variables_orig.bind(self).call end end rescue TypeError => e raise NoSingletonError, obj, caller end |
.included(obj) ⇒ Object
Friendly exception to say we’re not to be included
125 126 127 |
# File 'lib/attic.rb', line 125 def self.included(obj) raise RuntimeError, "Did you mean to `extend Attic`` in #{obj}" end |
Instance Method Details
#attic(*names) ⇒ Object
A class method for defining variables to store in the attic.
-
names
is a list of variables names. Accessor methods are created for each variable name in the list.
Returns an Array of all attic variables for the current class unless no arguments are given in which case it returns its singleton.
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
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/attic.rb', line 182 def attic(*names) return singleton_class if names.empty? names.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_variables # only after defining new attic vars end |
#attic? ⇒ Boolean
206 207 208 209 210 211 212 213 214 |
# File 'lib/attic.rb', line 206 def attic? return false if NoSingletonError.member? self singleton_class? rescue TypeError NoSingletonError.add_member self false end |
#attic_variable?(name) ⇒ Boolean
229 230 231 |
# File 'lib/attic.rb', line 229 def attic_variable?(name) attic_variables.member? name end |
#attic_variables ⇒ Object
Returns an Array of attic variables for the current class. e.g.
String.extend Attic
String.attic :timestamp
String.attic_variables # => [:timestamp]
223 224 225 226 227 |
# File 'lib/attic.rb', line 223 def attic_variables a = attic.instance_variable_get("@attic_variables") a ||= attic.instance_variable_set("@attic_variables", []) a end |