Module: Granite::Form::Util

Extended by:
ActiveSupport::Concern
Defined in:
lib/granite/form/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

Raises:

  • (ArgumentError)


32
33
34
35
36
37
38
39
40
41
42
# File 'lib/granite/form/util.rb', line 32

def conditions_satisfied?(**options)
  raise 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, *args) ⇒ 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



12
13
14
# File 'lib/granite/form/util.rb', line 12

def evaluate(value, *args)
  value.is_a?(Symbol) ? evaluate_symbol(value, *args) : evaluate_if_proc(value, *args)
end

#evaluate_if_proc(value, *args) ⇒ Object

Evaluates value and returns result based on what was passed:

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

  • otherwise just returns the value itself

Parameters:

  • value (Object)

    value to evaluate

Returns:

  • (Object)

    result of evaluation



21
22
23
# File 'lib/granite/form/util.rb', line 21

def evaluate_if_proc(value, *args)
  value.is_a?(Proc) ? evaluate_proc(value, *args) : value
end