Module: SoapyCake::Helper

Included in:
Admin, AdminAddedit, AdminTrack, Response
Defined in:
lib/soapy_cake/helper.rb

Instance Method Summary collapse

Instance Method Details

#const_lookup(type, key) ⇒ Object



39
40
41
42
43
# File 'lib/soapy_cake/helper.rb', line 39

def const_lookup(type, key)
  Const::CONSTS[type].fetch(key) do
    fail ArgumentError, "#{key} is not a valid value for #{type}"
  end
end

#require_params(opts, params) ⇒ Object



20
21
22
23
24
# File 'lib/soapy_cake/helper.rb', line 20

def require_params(opts, params)
  params.each do |param|
    fail Error, "Parameter '#{param}' missing!" unless opts.key?(param)
  end
end

#translate_booleans!(opts) ⇒ Object



26
27
28
29
30
31
# File 'lib/soapy_cake/helper.rb', line 26

def translate_booleans!(opts)
  opts.each do |k, v|
    opts[k] = 'on' if v == true
    opts[k] = 'off' if v == false
  end
end

#translate_values!(opts, params) ⇒ Object



33
34
35
36
37
# File 'lib/soapy_cake/helper.rb', line 33

def translate_values!(opts, params)
  params.each do |type|
    opts[type] = const_lookup(type, opts[type]) if opts.key?(type)
  end
end

#validate_id(opts, key) ⇒ Object



16
17
18
# File 'lib/soapy_cake/helper.rb', line 16

def validate_id(opts, key)
  fail Error, "Parameter '#{key}' must be > 0!" if opts[key].to_i < 1
end

#walk_tree(obj, key = nil, &block) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/soapy_cake/helper.rb', line 3

def walk_tree(obj, key = nil, &block)
  return nil if obj == {}

  case obj
  when Hash
    obj.map { |hk, hv| [hk, walk_tree(hv, hk, &block)] }.to_h
  when Array
    obj.map { |av| walk_tree(av, &block) }
  else
    yield(obj, key)
  end
end