Module: KingKonf::Decoder

Extended by:
Decoder
Included in:
Decoder
Defined in:
lib/king_konf/decoder.rb

Instance Method Summary collapse

Instance Method Details

#boolean(value, true_values: ["true", "1"], false_values: ["false", "0"]) ⇒ Object



7
8
9
10
11
12
13
14
15
16
# File 'lib/king_konf/decoder.rb', line 7

def boolean(value, true_values: ["true", "1"], false_values: ["false", "0"])
  if true_values.include?(value)
    true
  elsif false_values.include?(value)
    false
  else
    values = true_values + false_values
    raise ConfigError, "#{value.inspect} is not a boolean: must be one of #{values.join(', ')}"
  end
end

#duration(value) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/king_konf/decoder.rb', line 40

def duration(value, **)
  case value
  when ""
    nil
  when/^\d*\.\d+$/
    value.to_f
  when/^\d*$/
    value.to_i
  else
    DurationDecoder.decode(value)
  end
end

#float(value) ⇒ Object



36
37
38
# File 'lib/king_konf/decoder.rb', line 36

def float(value, **)
  Float(value)
end

#integer(value) ⇒ Object



30
31
32
33
34
# File 'lib/king_konf/decoder.rb', line 30

def integer(value, **)
  Integer(value)
rescue ArgumentError
  raise ConfigError, "#{value.inspect} is not an integer"
end

#list(value, sep: ",", items: :string) ⇒ Object



26
27
28
# File 'lib/king_konf/decoder.rb', line 26

def list(value, sep: ",", items: :string)
  value.split(sep).map {|s| public_send(items, s) }
end

#string(value) ⇒ Object



18
19
20
# File 'lib/king_konf/decoder.rb', line 18

def string(value, **)
  value
end

#symbol(value) ⇒ Object



22
23
24
# File 'lib/king_konf/decoder.rb', line 22

def symbol(value, **)
  value.to_sym
end