king_konf 0.3.7 → 1.0.0

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: 7097bd818496d31561799b6756a83779daade1ff0e7b6be133d2c287ad60e646
4
- data.tar.gz: fbf115254e0d7bf1a906011624cee8daa43b97fcf5a1330a35f12cc2d973b450
3
+ metadata.gz: 71dbeabd8a526f0bec6631102946c33bb9c5c0c429a2ca3f902ee6b7fba41fe2
4
+ data.tar.gz: 64d4309c980c1c7f50c6b143ba467bf23cf7989e90f1a83d030c30800c6ef01d
5
5
  SHA512:
6
- metadata.gz: 643c92f40690416dc179f79e83617f0ff5f8cd40aaea2dc1f04b941ccec69c4ea2217ecaba7f8af2881bf90e1fa8f1458eafc439354bdd4b7f266c55ee3a917e
7
- data.tar.gz: 2d858fffd1123d9282c705dc85ba090ab95f5ffee6e0e93a3945e87801e87103768a278b93531cf87392f2f8c8b7b904a6bce900c9ebabfaf4c15d269fe916a5
6
+ metadata.gz: 3708b5c46176d0783cfe5b37cbd435edd2065e4aee68daa0ca2082fb6e184c6d591c6f99788a6d1ac9ae491f0134e4848a55ac57e2d77949b0790b546e9bee58
7
+ data.tar.gz: bfc65c399877fd31c49cf2647094802b51e3cb54e09883de20d3dd6d1070c27782f4be4d8b4616e9ce9df9387978dda138a129fc3350879c7e97f723ddec8f55
data/README.md CHANGED
@@ -55,6 +55,9 @@ class MyApplication::Config < KingKonf::Config
55
55
 
56
56
  # You can restrict the set of allowed values:
57
57
  string :category, allowed_values: ["news", "stuff", "accouncements"]
58
+
59
+ # You can provide a custom validation function:
60
+ integer :even_number, validate_with: ->(int) { int % 2 == 0 }
58
61
  end
59
62
  ```
60
63
 
@@ -33,8 +33,8 @@ module KingKonf
33
33
  end
34
34
 
35
35
  TYPES.each do |type|
36
- define_method(type) do |name, default: nil, required: false, allowed_values: nil, **options|
37
- description, @desc = @desc, nil
36
+ define_method(type) do |name, default: nil, required: false, allowed_values: nil, validate_with: ->(_) { true }, **options|
37
+ description, @desc = @desc, nil if defined?(@desc)
38
38
  variable = Variable.new(
39
39
  name: name,
40
40
  type: type,
@@ -42,6 +42,7 @@ module KingKonf
42
42
  required: required,
43
43
  description: description,
44
44
  allowed_values: allowed_values,
45
+ validate_with: validate_with,
45
46
  options: options,
46
47
  )
47
48
 
@@ -70,6 +71,8 @@ module KingKonf
70
71
  end
71
72
 
72
73
  def initialize(env: ENV)
74
+ @desc = nil
75
+ @ignore_unknown_variables = nil
73
76
  load_env(env)
74
77
  end
75
78
 
@@ -106,6 +109,10 @@ module KingKonf
106
109
  raise ConfigError, "invalid value #{value.inspect} for variable `#{name}`, allowed values are #{variable.allowed_values}"
107
110
  end
108
111
 
112
+ if !variable.valid?(value)
113
+ raise ConfigError, "invalid value #{value.inspect} for variable `#{name}`"
114
+ end
115
+
109
116
  instance_variable_set("@#{name}", cast_value)
110
117
  end
111
118
 
@@ -2,15 +2,16 @@ require "king_konf/decoder"
2
2
 
3
3
  module KingKonf
4
4
  class Variable
5
- attr_reader :name, :type, :default, :description, :allowed_values, :options
5
+ attr_reader :name, :type, :default, :description, :allowed_values, :validate_with, :options
6
6
 
7
- def initialize(name:, type:, default: nil, description: "", required: false, allowed_values: nil, options: {})
7
+ def initialize(name:, type:, default: nil, description: "", required: false, allowed_values: nil, validate_with: ->(_) { true }, options: {})
8
8
  @name, @type = name, type
9
9
  @description = description
10
10
  @required = required
11
11
  @allowed_values = allowed_values
12
12
  @options = options
13
13
  @default = cast(default) unless default.nil?
14
+ @validate_with = validate_with
14
15
  end
15
16
 
16
17
  def cast(value)
@@ -31,11 +32,11 @@ module KingKonf
31
32
  end
32
33
 
33
34
  def valid?(value)
34
- cast(value)
35
+ cast_value = cast(value)
35
36
  rescue ConfigError
36
37
  false
37
38
  else
38
- true
39
+ !!validate_with.call(cast_value)
39
40
  end
40
41
 
41
42
  def allowed?(value)
@@ -1,3 +1,3 @@
1
1
  module KingKonf
2
- VERSION = "0.3.7"
2
+ VERSION = "1.0.0"
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.7
4
+ version: 1.0.0
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-10-26 00:00:00.000000000 Z
11
+ date: 2020-02-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler