Module: Cult::Drivers::Common::ClassMethods
- Defined in:
- lib/cult/drivers/common.rb
Instance Method Summary collapse
- #memoize(method_name) ⇒ Object
-
#with_id_mapping(method_name) ⇒ Object
Lets us write a method “something_map” that returns => …, and also get a function “something” that returns the keys.
Instance Method Details
#memoize(method_name) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/cult/drivers/common.rb', line 21 def memoize(method_name) old_method_name = "#{method_name}_unmemoized".to_sym alias_method old_method_name, method_name var_name = "@#{method_name}".to_sym define_method(method_name) do if !instance_variable_defined?(var_name) instance_variable_set(var_name, send(old_method_name)) end instance_variable_get(var_name) end define_method("#{method_name}_unmemo!") do remove_instance_variable(var_name) end end |
#with_id_mapping(method_name) ⇒ Object
Lets us write a method “something_map” that returns => …, and also get a function “something” that returns the keys.
13 14 15 16 17 18 |
# File 'lib/cult/drivers/common.rb', line 13 def with_id_mapping(method_name) new_method = method_name.to_s.sub(/_map\z/, '') define_method(new_method) do send(method_name).keys end end |