module MethodOrProcHelper
def call_method_or_exec_proc(symbol_or_proc, *args)
call_method_or_exec_proc(my_proc) #=> will instance_exec in self
my_proc = Proc.new{ to_s }
Calling with a Proc
call_method_or_exec_proc(:to_s) #=> will call #to_s
Calling with a Symbol:
within self. Any args will be passed along to the method dispatch.
This method will either call the symbol on self or instance_exec the Proc
def call_method_or_exec_proc(symbol_or_proc, *args) case symbol_or_proc when Symbol, String send(symbol_or_proc, *args) when Proc instance_exec(*args, &symbol_or_proc) else symbol_or_proc end end
def call_method_or_proc_on(receiver, *args)
call_method_or_proc_on(@my_obj, :find, 1) #=> @my_obj.find(1)
example:
You can pass along any necessary arguments to the method / Proc as arguments. For
call_method_or_proc_on(@my_obj, proc, exec: false)
proc = Proc.new{|s| s.size }
the options hash.
not instance exec, but just call the Proc, then pass along `exec: false` in
By default, the Proc will be instance_exec'd within self. If you would rather
call_method_or_proc_on(@my_obj, proc)
proc = Proc.new{|s| s.size }
Calling with a Proc:
call_method_or_proc_on(@my_obj, :size) same as @my_obj.size
Calling with a String or Symbol:
method wraps that pattern.
or instance_exec a proc passing in the object as the first parameter. This
Many times throughout the views we want to either call a method on an object
def call_method_or_proc_on(receiver, *args) options = { exec: true }.merge(args.extract_options!) symbol_or_proc = args.shift case symbol_or_proc when Symbol, String receiver.public_send symbol_or_proc.to_sym, *args when Proc if options[:exec] instance_exec(receiver, *args, &symbol_or_proc) else symbol_or_proc.call(receiver, *args) end else symbol_or_proc end end
def render_in_context(context, obj, *args)
passing it the proc. This evaluates the proc in the context of the receiver, thus changing
This method is different from the others in that it calls `instance_exec` on the receiver,
def render_in_context(context, obj, *args) context = self if context.nil? # default to `self` only when nil case obj when Proc context.instance_exec *args, &obj when Symbol context.public_send obj, *args else obj end end
def render_or_call_method_or_proc_on(obj, string_symbol_or_proc, options = {})
returning the content when String or call call_method_or_proc_on when Symbol or Proc.
static (String), methods (Symbol) or procs (Proc). This helper takes care of
Many configuration options (Ex: site_title, title_image) could either be
def render_or_call_method_or_proc_on(obj, string_symbol_or_proc, options = {}) case string_symbol_or_proc when Symbol, Proc call_method_or_proc_on(obj, string_symbol_or_proc, options) else string_symbol_or_proc end end