Class: Module
- Defined in:
- lib/amp/dependencies/zip/stdrubyext.rb,
lib/amp/dependencies/maruku/structures.rb,
lib/amp/support/support.rb
Instance Method Summary collapse
- #forward_message(forwarder, *messagesToForward) ⇒ Object
-
#memoize_method(meth_name, module_function_please = false) ⇒ Object
Makes an instance or module method memoized.
-
#opposite_method(new_method, base_method) ⇒ Object
Creates an opposite method based on a defined method: one that returns true if the base method returns false, and vice-versa.
- #safe_attr_accessor1(symbol, klass) ⇒ Object
- #safe_attr_accessor2(symbol, klass) ⇒ Object (also: #safe_attr_accessor)
Instance Method Details
#forward_message(forwarder, *messagesToForward) ⇒ Object
99 100 101 102 103 104 105 |
# File 'lib/amp/dependencies/zip/stdrubyext.rb', line 99 def (forwarder, *) methodDefs = .map { |msg| "def #{msg}; #{forwarder}(:#{msg}); end" } module_eval(methodDefs.join("\n")) end |
#memoize_method(meth_name, module_function_please = false) ⇒ Object
Makes an instance or module method memoized. Works by aliasing the old method and creating a new one in its place.
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/amp/support/support.rb', line 158 def memoize_method(meth_name, module_function_please = false) meth_name = meth_name.to_sym aliased_meth = "__memo_#{meth_name}".to_sym # alias to a new method alias_method aliased_meth, meth_name # module_function the newly aliased method if necessary if module_function_please && self.class == Module module_function aliased_meth end # incase it doesn't exist yet @__memo_cache ||= {} # our new method! Replacing the old one. define_method meth_name do |*args| # we store the memoized data with an i-var. @__memo_cache[meth_name] ||= {} cache = @__memo_cache[meth_name] # if we have the cached value, return it result = cache[args] return result if result # cache miss. find the value result = send(aliased_meth, *args) cache[args] = result result end self end |
#opposite_method(new_method, base_method) ⇒ Object
Creates an opposite method based on a defined method: one that returns true if the base method returns false, and vice-versa.
193 194 195 196 197 198 199 |
# File 'lib/amp/support/support.rb', line 193 def opposite_method(new_method, base_method) class_eval <<-EOF def #{new_method}(*args, &block) not #{base_method}(*args, &block) end EOF end |
#safe_attr_accessor1(symbol, klass) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/amp/dependencies/maruku/structures.rb', line 24 def safe_attr_accessor1(symbol, klass) attr_reader symbol code = <<-EOF def #{symbol}=(val) if not val.kind_of? #{klass} s = "\nCould not assign an object of type \#{val.class} to #{symbol}.\n\n" s += "Tried to assign object of class \#{val.class}:\n"+ "\#{val.inspect}\n"+ "to \#{self.class}::#{symbol} constrained to be of class #{klass}.\n" raise s end @#{symbol} = val end EOF module_eval code end |
#safe_attr_accessor2(symbol, klass) ⇒ Object Also known as: safe_attr_accessor
42 43 44 |
# File 'lib/amp/dependencies/maruku/structures.rb', line 42 def safe_attr_accessor2(symbol, klass) attr_accessor symbol end |