Module: VarBlock::GetvarHandlers

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

Constant Summary collapse

SUPPORTED_OPTIONS =
[:truthy?, :any?].freeze

Class Method Summary collapse

Methods included from Support

array_wrap

Class Method Details

.handle_default(value) ⇒ Object



36
37
38
# File 'lib/var_block/getvar_handlers.rb', line 36

def handle_default(value)
  value
end

.handle_proc(value, context) ⇒ Object



32
33
34
# File 'lib/var_block/getvar_handlers.rb', line 32

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
# 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? 
    return handle_options(value, context, options)

  # 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