king_konf 0.1.5 → 0.1.6

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
  SHA1:
3
- metadata.gz: abc81cff30b2a3d480020c088f5134c054571f73
4
- data.tar.gz: e076879bbfc0d94890fe661fdd2162df8ab21902
3
+ metadata.gz: bbb0ce734f3e5eff7f0c2fca94727d80ca6e9ca8
4
+ data.tar.gz: 58e2b1c6539e83090d91be374963bcd1ad2dae6a
5
5
  SHA512:
6
- metadata.gz: df30bcc671ebd55378a87ca970a86df2cf8f702523092aaaea634902df34475bd1b375ff7abeb5670f5d80ea419650230d612b872bca631b65a402a4906d5546
7
- data.tar.gz: 60706bc33e11bef6bb59238a55d651ec8d157d74c9d81603ec9bdc777ed3bd4e352bdf1c2167b95c069676a642e5ee6f14d20c2e664c3f88b939c96dfff7979b
6
+ metadata.gz: 9745236ead021ed7741da1dedd6f9fa8db02c09412f9ecff22e0b5f9c90e6e8082b6cd28c41af2ea8f5fd674958a3680216ee21bc50f718074f1a6c1bbf06bb7
7
+ data.tar.gz: c4467ce101c3a2b514266f771e7bc035e94eea558873ee8dd0ce1eb0a7808e356fa26b83a74041df02a4b34474fedbd9d87ef2fbcdb53302297ac61e4dd9e997
data/README.md CHANGED
@@ -75,6 +75,35 @@ config.tags #=> ["greetings", "introductions", "articles"]
75
75
  config.codes #=> [435, 2342, 8678]
76
76
  ```
77
77
 
78
+ If you prefer to use a config file, that's also possible. Simply load a YAML file with `#load_file`:
79
+
80
+ ```ruby
81
+ config.load_file("config/my_app.yml")
82
+ ```
83
+
84
+ A common pattern is to store config for all runtime environments in a single file and select the config based on the current environment, e.g.:
85
+
86
+ ```ruby
87
+ config.load_file("config/my_app.yml", Rails.environment)
88
+ ```
89
+
90
+ In that case, structure the config file like so:
91
+
92
+ ```yaml
93
+ development:
94
+ title: hello
95
+ score: 25
96
+
97
+ test:
98
+ title: yolo
99
+ score: 13
100
+
101
+ production:
102
+ title: yeah
103
+ score: 99
104
+ ```
105
+
106
+
78
107
  ## Development
79
108
 
80
109
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -1,31 +1,7 @@
1
- require "king_konf/decoder"
1
+ require "king_konf/variable"
2
2
  require "king_konf/config_file_loader"
3
3
 
4
4
  module KingKonf
5
- class Variable
6
- attr_reader :name, :type, :default, :description, :options
7
-
8
- def initialize(name, type, default, description, options)
9
- @name, @type, @default = name, type, default
10
- @description = description
11
- @options = options
12
- end
13
-
14
- def valid?(value)
15
- case @type
16
- when :string then value.is_a?(String) || value.nil?
17
- when :list then value.is_a?(Array)
18
- when :integer then value.is_a?(Integer) || value.nil?
19
- when :boolean then value == true || value == false
20
- else raise "invalid type #{@type}"
21
- end
22
- end
23
-
24
- def decode(value)
25
- Decoder.public_send(@type, value, **options)
26
- end
27
- end
28
-
29
5
  class Config
30
6
  @variables = {}
31
7
 
@@ -48,9 +24,16 @@ module KingKonf
48
24
  end
49
25
 
50
26
  %i(boolean integer string list).each do |type|
51
- define_method(type) do |name, default: nil, **options|
27
+ define_method(type) do |name, default: nil, required: false, **options|
52
28
  description, @desc = @desc, nil
53
- variable = Variable.new(name, type, default, description, options)
29
+ variable = Variable.new(
30
+ name: name,
31
+ type: type,
32
+ default: default,
33
+ required: required,
34
+ description: description,
35
+ options: options,
36
+ )
54
37
 
55
38
  @variables ||= {}
56
39
  @variables[name.to_s] = variable
@@ -76,7 +59,7 @@ module KingKonf
76
59
  load_env(env)
77
60
  end
78
61
 
79
- def load_file(path, environment)
62
+ def load_file(path, environment = nil)
80
63
  loader = ConfigFileLoader.new(self)
81
64
  loader.load_file(path, environment)
82
65
  end
@@ -110,6 +93,14 @@ module KingKonf
110
93
  end
111
94
  end
112
95
 
96
+ def validate!
97
+ self.class.variables.each do |variable|
98
+ if variable.required? && get(variable.name).nil?
99
+ raise ConfigError, "required variable `#{variable.name}` is not defined"
100
+ end
101
+ end
102
+ end
103
+
113
104
  private
114
105
 
115
106
  def load_config(config)
@@ -7,15 +7,18 @@ module KingKonf
7
7
  @config = config
8
8
  end
9
9
 
10
- def load_file(path, environment)
10
+ def load_file(path, environment = nil)
11
11
  # First, load the ERB template from disk.
12
12
  template = ERB.new(File.new(path).read)
13
13
 
14
14
  # The last argument to `safe_load` allows us to use aliasing to share
15
15
  # configuration between environments.
16
- processed = YAML.safe_load(template.result(binding), [], [], true)
16
+ data = YAML.safe_load(template.result(binding), [], [], true)
17
17
 
18
- processed.fetch(environment).each do |variable, value|
18
+ # Grab just the config for the environment, if specified.
19
+ data = data.fetch(environment) unless environment.nil?
20
+
21
+ data.each do |variable, value|
19
22
  @config.set(variable, value)
20
23
  end
21
24
  end
@@ -0,0 +1,38 @@
1
+ require "king_konf/decoder"
2
+
3
+ module KingKonf
4
+ class Variable
5
+ attr_reader :name, :type, :default, :description, :options
6
+
7
+ def initialize(name:, type:, default: nil, description: "", required: false, options: {})
8
+ @name, @type, @default = name, type, default
9
+ @description = description
10
+ @required = required
11
+ @options = options
12
+ end
13
+
14
+ def required?
15
+ @required
16
+ end
17
+
18
+ def valid?(value)
19
+ case @type
20
+ when :string then value.is_a?(String) || value.nil?
21
+ when :list then value.is_a?(Array)
22
+ when :integer then value.is_a?(Integer) || value.nil?
23
+ when :boolean then value == true || value == false
24
+ else raise "invalid type #{@type}"
25
+ end
26
+ end
27
+
28
+ def decode(value)
29
+ Decoder.public_send(@type, value, **options)
30
+ end
31
+
32
+ %i(boolean integer string list).each do |type|
33
+ define_method("#{type}?") do
34
+ @type == type
35
+ end
36
+ end
37
+ end
38
+ end
@@ -1,3 +1,3 @@
1
1
  module KingKonf
2
- VERSION = "0.1.5"
2
+ VERSION = "0.1.6"
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.1.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Schierbeck
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-09-15 00:00:00.000000000 Z
11
+ date: 2017-09-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -72,6 +72,7 @@ files:
72
72
  - lib/king_konf/config.rb
73
73
  - lib/king_konf/config_file_loader.rb
74
74
  - lib/king_konf/decoder.rb
75
+ - lib/king_konf/variable.rb
75
76
  - lib/king_konf/version.rb
76
77
  homepage: https://github.com/dasch/king_konf
77
78
  licenses:
@@ -93,7 +94,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
93
94
  version: '0'
94
95
  requirements: []
95
96
  rubyforge_project:
96
- rubygems_version: 2.4.5.1
97
+ rubygems_version: 2.6.11
97
98
  signing_key:
98
99
  specification_version: 4
99
100
  summary: A simple configuration library