king_konf 0.3.6 → 0.3.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6321bc1dbb64b0256b68251f524a71bc93b3837e15950c51f8d1820b52ccd7b4
4
- data.tar.gz: 426d17dc7be5cc6b68376fe572e0c34b95074c3c303bbd463a42b7ac13201402
3
+ metadata.gz: 7097bd818496d31561799b6756a83779daade1ff0e7b6be133d2c287ad60e646
4
+ data.tar.gz: fbf115254e0d7bf1a906011624cee8daa43b97fcf5a1330a35f12cc2d973b450
5
5
  SHA512:
6
- metadata.gz: 5ee51b8c5fc4eefc2c33b6ced5a3a00ae627cbf206d1631c28188f0fc4032cd70a6d7475729ef0d2f6446c1fbf00e6b7875e652654086f2b37d8b7c08bebbb58
7
- data.tar.gz: 4c3f255add7dd11e2ab00c8705bfa4d4271ba9ba4f2d1758cb34dc532b9a7f66ea2a11f1cc00737b7a722b08491d170a78d39bb07914374024ca38e1fb76b498
6
+ metadata.gz: 643c92f40690416dc179f79e83617f0ff5f8cd40aaea2dc1f04b941ccec69c4ea2217ecaba7f8af2881bf90e1fa8f1458eafc439354bdd4b7f266c55ee3a917e
7
+ data.tar.gz: 2d858fffd1123d9282c705dc85ba090ab95f5ffee6e0e93a3945e87801e87103768a278b93531cf87392f2f8c8b7b904a6bce900c9ebabfaf4c15d269fe916a5
@@ -100,15 +100,13 @@ module KingKonf
100
100
 
101
101
  variable = self.class.variable(name)
102
102
 
103
- if !variable.valid?(value)
104
- raise ConfigError, "invalid value #{value.inspect} for variable `#{name}`, expected #{variable.type}"
105
- end
103
+ cast_value = value.nil? ? nil : variable.cast(value)
106
104
 
107
- if !variable.allowed?(value)
105
+ if !variable.allowed?(cast_value)
108
106
  raise ConfigError, "invalid value #{value.inspect} for variable `#{name}`, allowed values are #{variable.allowed_values}"
109
107
  end
110
108
 
111
- instance_variable_set("@#{name}", variable.cast(value))
109
+ instance_variable_set("@#{name}", cast_value)
112
110
  end
113
111
 
114
112
  def validate!
@@ -10,16 +10,20 @@ module KingKonf
10
10
  @required = required
11
11
  @allowed_values = allowed_values
12
12
  @options = options
13
- @default = cast(default)
13
+ @default = cast(default) unless default.nil?
14
14
  end
15
15
 
16
16
  def cast(value)
17
17
  case @type
18
- when :float then value.is_a?(String) || value.is_a?(Integer) ? value.to_f : value
19
- when :duration then value.is_a?(String) ? Decoder.duration(value) : value
20
- when :symbol then value.is_a?(String) ? value.to_sym : value
18
+ when :boolean then [true, false].include?(value) ? value : Decoder.boolean(value, **options)
19
+ when :integer then Integer(value)
20
+ when :float then Float(value)
21
+ when :duration then value.is_a?(Integer) ? value : Decoder.duration(value)
22
+ when :symbol then value.to_sym
21
23
  else value
22
24
  end
25
+ rescue ArgumentError, NoMethodError
26
+ raise ConfigError, "invalid value #{value.inspect} for variable `#{name}`, expected #{type}"
23
27
  end
24
28
 
25
29
  def required?
@@ -27,20 +31,17 @@ module KingKonf
27
31
  end
28
32
 
29
33
  def valid?(value)
30
- case @type
31
- when :string then value.is_a?(String) || value.nil?
32
- when :list then value.is_a?(Array)
33
- when :integer then value.is_a?(Integer) || value.nil?
34
- when :float then value.is_a?(Float) || value.is_a?(Integer) || value.nil?
35
- when :duration then value.is_a?(Float) || value.is_a?(Integer) || value.is_a?(String) || value.nil?
36
- when :boolean then value == true || value == false
37
- when :symbol then value.is_a?(Symbol) || value.is_a?(String)
38
- else raise "invalid type #{@type}"
39
- end
34
+ cast(value)
35
+ rescue ConfigError
36
+ false
37
+ else
38
+ true
40
39
  end
41
40
 
42
41
  def allowed?(value)
43
- allowed_values.nil? || allowed_values.include?(value)
42
+ allowed_values.nil? || allowed_values.include?(cast(value))
43
+ rescue ConfigError
44
+ false
44
45
  end
45
46
 
46
47
  def decode(value)
@@ -1,3 +1,3 @@
1
1
  module KingKonf
2
- VERSION = "0.3.6"
2
+ VERSION = "0.3.7"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: king_konf
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.6
4
+ version: 0.3.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Schierbeck
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-05-29 00:00:00.000000000 Z
11
+ date: 2018-10-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler