Module: VarBlock::GetvarHandlers

Extended by:
Support
Defined in:
lib/var_block/getvar_handlers.rb

Constant Summary collapse

SUPPORTED_OPTIONS =
[:truthy?].freeze

Class Method Summary collapse

Methods included from Support

array_wrap

Class Method Details

.handle_default(value) ⇒ Object



47
48
49
# File 'lib/var_block/getvar_handlers.rb', line 47

def handle_default(value)
  value
end

.handle_proc(value, context) ⇒ Object



43
44
45
# File 'lib/var_block/getvar_handlers.rb', line 43

def handle_proc(value, context)
  context.instance_exec(&value)
end

.handle_var_array(value, context, options) ⇒ Object



11
12
13
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
# File 'lib/var_block/getvar_handlers.rb', line 11

def handle_var_array(value, context, options)
  # if :truthy?, we need to check each item in the array, and return false immediately if at least one is found to be not "truthy", else return true
  if options.any? && options.include?(:truthy?)
    is_truthy = true

    value.each do |v|
      if v.is_a? Proc
        is_truthy = handle_proc(v, context)
      else
        is_truthy = handle_default(v)
      end
      break unless is_truthy
    end

    return is_truthy

  # else, if no options, defaults to return as a wrapped Array
  else
    merged_values = []

    value.each do |v|
      if v.is_a? Proc
        merged_values = merged_values + array_wrap(handle_proc(v, context))
      else
        merged_values = merged_values + array_wrap(handle_default(v))
      end
    end

    return merged_values
  end
end