Module: ParamChecker::HashExt

Defined in:
lib/param_checker/hash_ext.rb

Instance Method Summary collapse

Instance Method Details

#check(type, key, default, options = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/param_checker/hash_ext.rb', line 4

def check(type, key, default, options = {})
  value = nil
  if key.class == Array
    value = self
    key.each do |k|
      value = value[k]
      break if value.nil?
    end
  else # String || Symbol
    value = self[key]
  end

  if value.nil?
    default
  else
    case type
    when :integer, :i
      ParamChecker.check_integer(value, default, options)
    when :float, :f
      ParamChecker.check_float(value, default, options)
    when :string, :s
      ParamChecker.check_string(value, default, options)
    when :symbol, :sym
      ParamChecker.check_symbol(value, default, options)
    when :boolean, :b
      ParamChecker.check_boolean(value, default, options)
    else
      raise
    end
  end
end