Module: LazyRecord::Methods
- Defined in:
- lib/lazy_record/methods.rb
Overview
Macro for dynamic instance method generation. Best to use for one-liners.
Constant Summary collapse
- METHODS_MODULE_NAME =
:DynamicMethods
Instance Method Summary collapse
- #lr_method(method_name, *method_args, method) ⇒ Object
- #make_method_from_proc(mod, method_name, proc) ⇒ Object
- #make_method_from_string(mod, method_name, *method_args, method) ⇒ Object
Instance Method Details
#lr_method(method_name, *method_args, method) ⇒ Object
8 9 10 11 12 13 14 15 16 |
# File 'lib/lazy_record/methods.rb', line 8 def lr_method(method_name, *method_args, method) include mod = get_or_set_mod(METHODS_MODULE_NAME) if method.respond_to?(:call) make_method_from_proc(mod, method_name, method) else make_method_from_string(mod, method_name, *method_args, method) end end |
#make_method_from_proc(mod, method_name, proc) ⇒ Object
18 19 20 21 22 |
# File 'lib/lazy_record/methods.rb', line 18 def make_method_from_proc(mod, method_name, proc) mod.module_eval do send(:define_method, method_name, &proc) end end |
#make_method_from_string(mod, method_name, *method_args, method) ⇒ Object
24 25 26 27 28 29 30 31 32 |
# File 'lib/lazy_record/methods.rb', line 24 def make_method_from_string(mod, method_name, *method_args, method) method_args = method_args.map(&:to_s).join(', ') mod.module_eval do define_method(method_name) do |*params| block = eval("lambda { |#{method_args}| #{method} }") block.call(*params) end end end |