Sha256: 7dc2bfb7014ff3b549aafc76972d7b10ed675ce4f3ae1cdc34e9dde007328dac
Contents?: true
Size: 971 Bytes
Versions: 15
Compression:
Stored size: 971 Bytes
Contents
# frozen_string_literal: true module LazyRecord # Macro for dynamic instance method generation. Best to use for one-liners. module Methods METHODS_MODULE_NAME = :DynamicMethods 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 def make_method_from_proc(mod, method_name, proc) mod.module_eval do send(:define_method, method_name, &proc) end end 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 end end
Version data entries
15 entries across 15 versions & 1 rubygems