king_konf 0.3.7 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ci.yml +28 -0
- data/README.md +3 -0
- data/king_konf.gemspec +1 -1
- data/lib/king_konf/config.rb +9 -2
- data/lib/king_konf/config_file_loader.rb +7 -1
- data/lib/king_konf/variable.rb +5 -4
- data/lib/king_konf/version.rb +1 -1
- metadata +9 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2d2502b9771fc107ad2db0f49241e302ed80465c3e769d9c8f0dbb0888214aa4
|
4
|
+
data.tar.gz: bf947978e987de8ee67df77556768e7851a67637f18b5d83e74b2cdc5f8ac27e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f5419839b71bdca08588802b2ae5dac5d4e040e816e04e8482b213f7ef3866c0f7bf0a0e07fdd9a84e873fcd518cc8811c41b3a21d1a141a332192a9103b263e
|
7
|
+
data.tar.gz: f1cd338ae1e5379f65ac54a6e63d5525795a85201b97e7bb2b6f2ce876922713a18957ef696ec671d6591422b5027372575968be8a10f0b41de168b83e8f0216
|
@@ -0,0 +1,28 @@
|
|
1
|
+
name: CI
|
2
|
+
|
3
|
+
on:
|
4
|
+
push:
|
5
|
+
branches: [ "master" ]
|
6
|
+
pull_request:
|
7
|
+
branches: [ "master" ]
|
8
|
+
|
9
|
+
permissions:
|
10
|
+
contents: read
|
11
|
+
|
12
|
+
jobs:
|
13
|
+
test:
|
14
|
+
|
15
|
+
runs-on: ubuntu-latest
|
16
|
+
strategy:
|
17
|
+
matrix:
|
18
|
+
ruby-version: ['2.6', '2.7', '3.0']
|
19
|
+
|
20
|
+
steps:
|
21
|
+
- uses: actions/checkout@v3
|
22
|
+
- name: Set up Ruby
|
23
|
+
uses: ruby/setup-ruby@v1
|
24
|
+
with:
|
25
|
+
ruby-version: ${{ matrix.ruby-version }}
|
26
|
+
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
|
27
|
+
- name: Run tests
|
28
|
+
run: bundle exec rake
|
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
|
|
data/king_konf.gemspec
CHANGED
@@ -20,7 +20,7 @@ Gem::Specification.new do |spec|
|
|
20
20
|
|
21
21
|
spec.require_paths = ["lib"]
|
22
22
|
|
23
|
-
spec.add_development_dependency "bundler", "~>
|
23
|
+
spec.add_development_dependency "bundler", "~> 2"
|
24
24
|
spec.add_development_dependency "rake", "~> 10.0"
|
25
25
|
spec.add_development_dependency "rspec", "~> 3.0"
|
26
26
|
spec.add_development_dependency "rspec_junit_formatter"
|
data/lib/king_konf/config.rb
CHANGED
@@ -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
|
|
@@ -11,7 +11,13 @@ module KingKonf
|
|
11
11
|
# First, load the ERB template from disk.
|
12
12
|
template = ERB.new(File.new(path).read)
|
13
13
|
|
14
|
-
|
14
|
+
begin
|
15
|
+
# Without flag, loading the configuration file fails in Ruby 3.1 if it contains aliases.
|
16
|
+
data = YAML.load(template.result(binding), aliases: true)
|
17
|
+
rescue ArgumentError
|
18
|
+
# Covering prior YAML versions
|
19
|
+
data = YAML.load(template.result(binding))
|
20
|
+
end
|
15
21
|
|
16
22
|
# Grab just the config for the environment, if specified.
|
17
23
|
data = data.fetch(environment) unless environment.nil?
|
data/lib/king_konf/variable.rb
CHANGED
@@ -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
|
-
|
39
|
+
!!validate_with.call(cast_value)
|
39
40
|
end
|
40
41
|
|
41
42
|
def allowed?(value)
|
data/lib/king_konf/version.rb
CHANGED
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.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel Schierbeck
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-10-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '2'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '2'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -74,6 +74,7 @@ extensions: []
|
|
74
74
|
extra_rdoc_files: []
|
75
75
|
files:
|
76
76
|
- ".circleci/config.yml"
|
77
|
+
- ".github/workflows/ci.yml"
|
77
78
|
- ".gitignore"
|
78
79
|
- ".rspec"
|
79
80
|
- Gemfile
|
@@ -94,7 +95,7 @@ homepage: https://github.com/dasch/king_konf
|
|
94
95
|
licenses:
|
95
96
|
- Apache License Version 2.0
|
96
97
|
metadata: {}
|
97
|
-
post_install_message:
|
98
|
+
post_install_message:
|
98
99
|
rdoc_options: []
|
99
100
|
require_paths:
|
100
101
|
- lib
|
@@ -109,9 +110,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
109
110
|
- !ruby/object:Gem::Version
|
110
111
|
version: '0'
|
111
112
|
requirements: []
|
112
|
-
|
113
|
-
|
114
|
-
signing_key:
|
113
|
+
rubygems_version: 3.3.3
|
114
|
+
signing_key:
|
115
115
|
specification_version: 4
|
116
116
|
summary: A simple configuration library
|
117
117
|
test_files: []
|