Module: Granite::Util

Extended by:
ActiveSupport::Concern
Included in:
Base
Defined in:
lib/granite/util.rb

Instance Method Summary collapse

Instance Method Details

#conditions_satisfied?(**options) ⇒ Boolean

Evaluates ‘if` or `unless` conditions present in the supplied `options` being it a symbol or callable.

Parameters:

  • options (Hash)

    The method options to evaluate.

Options Hash (**options):

  • :if (Object)

    method name or callable

  • :unless (Object)

    method name or callable

Returns:

  • (Boolean)

    whether conditions are satisfied



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/granite/util.rb', line 29

def conditions_satisfied?(**options)
  fail ArgumentError, 'You cannot specify both if and unless' if options.key?(:if) && options.key?(:unless)

  if options.key?(:if)
    evaluate(options[:if])
  elsif options.key?(:unless)
    !evaluate(options[:unless])
  else
    true
  end
end

#evaluate(value) ⇒ Object

Evaluates value and returns result based on what was passed:

  • if Proc was passed, then executes it in context of self

  • if Symbol was passed, then calls a method with that name and returns result

  • otherwise just returns the value itself

Parameters:

  • value (Object)

    value to evaluate

Returns:

  • (Object)

    result of evaluation



11
12
13
14
15
16
17
18
19
20
# File 'lib/granite/util.rb', line 11

def evaluate(value)
  case value
  when Proc
    evaluate_proc(value)
  when Symbol
    evaluate_symbol(value)
  else
    value
  end
end