Method: ActivityNotification.resolve_value

Defined in:
lib/activity_notification/common.rb

.resolve_value(context, thing, *args) ⇒ Object

Used to transform value from metadata to data. Accepts Symbols, which it will send against context. Accepts Procs, which it will execute with controller and context. Both Symbols and Procs will be passed arguments of this method. Also accepts Hash of these Symbols or Procs. If any other value will be passed, returns original value.

Parameters:

  • context (Object)

    Context to resolve parameter, which is usually target or notificable model

  • thing (Symbol, Proc, Hash, Object)

    Symbol or Proc to resolve parameter

  • args (Array)

    Arguments to pass to thing as method

Returns:

  • (Object)

    Resolved parameter value



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/activity_notification/common.rb', line 14

def self.resolve_value(context, thing, *args)
  case thing
  when Symbol
    symbol_method = context.method(thing)
    if symbol_method.arity > 1
      if args.last.kind_of?(Hash)
        symbol_method.call(ActivityNotification.get_controller, *args[0...-1], **args[-1])
      else
        symbol_method.call(ActivityNotification.get_controller, *args)
      end
    elsif symbol_method.arity > 0
      symbol_method.call(ActivityNotification.get_controller)
    else
      symbol_method.call
    end
  when Proc
    if thing.arity > 2
      thing.call(ActivityNotification.get_controller, context, *args)
    elsif thing.arity > 1
      thing.call(ActivityNotification.get_controller, context)
    elsif thing.arity > 0
      thing.call(context)
    else
      thing.call
    end
  when Hash
    thing.dup.tap do |hash|
      hash.each do |key, value|
        hash[key] = ActivityNotification.resolve_value(context, value, *args)
      end
    end
  else
    thing
  end
end