figleaf 0.3.1 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rspec +1 -1
- data/.solargraph.yml +16 -0
- data/lib/figleaf/config.rb +2 -13
- data/lib/figleaf/fighash.rb +2 -0
- data/lib/figleaf/lazy_block_hash.rb +16 -0
- data/lib/figleaf/settings.rb +20 -1
- data/lib/figleaf/version.rb +3 -1
- data/lib/figleaf.rb +4 -0
- data/spec/figleaf/config_spec.rb +2 -0
- data/spec/figleaf/configuration_spec.rb +2 -0
- data/spec/figleaf/fighash_spec.rb +2 -0
- data/spec/figleaf/settings_spec.rb +96 -13
- data/spec/fixtures/extra/merged_rb.rb +33 -0
- data/spec/fixtures/extra/merged_yaml.yaml +29 -0
- data/spec/fixtures/type_errors/bad_overrides.yml +11 -0
- metadata +8 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d1b89f2110836d4dd5f0ad63b08f4a4ce2b8c6b17edbb1103a73e85ffaa898c9
|
4
|
+
data.tar.gz: b798a685d89f189e37e89624f08d8c2a6f9bce2abad278f983e83dee5cf292fe
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b00325b3a45103b5b3d9859befeff699bea806e981757b86371ada1cf933ebdb9a4e1cbb139ae96127b221543d1f6092441dfb3e591624f289ae2e0bac91cccd
|
7
|
+
data.tar.gz: 51f854f9551e34b2b5492113f027cad42ad8519c1f86be2ed8c05bf8dc6ae498360edc0bd2578971e428bf39325ebe62f67a4aec9d43d812ba70972f04e2cab4
|
data/.rspec
CHANGED
@@ -1,2 +1,2 @@
|
|
1
1
|
--color
|
2
|
-
--format
|
2
|
+
--format documentation
|
data/.solargraph.yml
ADDED
data/lib/figleaf/config.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Figleaf
|
2
4
|
# Convert a ruby block to nested hash
|
3
5
|
class Config
|
@@ -33,17 +35,4 @@ module Figleaf
|
|
33
35
|
|
34
36
|
attr_reader :property
|
35
37
|
end
|
36
|
-
|
37
|
-
class LazyBlockHash < Hash
|
38
|
-
def [](attr)
|
39
|
-
val = super(attr)
|
40
|
-
if val.is_a?(Proc)
|
41
|
-
val.call
|
42
|
-
else
|
43
|
-
val
|
44
|
-
end
|
45
|
-
rescue => e
|
46
|
-
raise Settings::InvalidRb, "Configuration has invalid Ruby\n" + e.message
|
47
|
-
end
|
48
|
-
end
|
49
38
|
end
|
data/lib/figleaf/fighash.rb
CHANGED
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Figleaf
|
4
|
+
class LazyBlockHash < Hash
|
5
|
+
def [](attr)
|
6
|
+
val = super
|
7
|
+
if val.is_a?(Proc)
|
8
|
+
val.call
|
9
|
+
else
|
10
|
+
val
|
11
|
+
end
|
12
|
+
rescue => e
|
13
|
+
raise Settings::InvalidRb, "Configuration has invalid Ruby\n#{e.message}"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
data/lib/figleaf/settings.rb
CHANGED
@@ -1,7 +1,10 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Figleaf
|
2
4
|
class Settings
|
3
5
|
InvalidYAML = Class.new(RuntimeError)
|
4
6
|
InvalidRb = Class.new(RuntimeError)
|
7
|
+
MismatchedTypes = Class.new(RuntimeError)
|
5
8
|
|
6
9
|
class_attribute :auto_define
|
7
10
|
self.auto_define = false
|
@@ -70,7 +73,23 @@ module Figleaf
|
|
70
73
|
|
71
74
|
default = config["default"]
|
72
75
|
property = config[env_to_load]
|
73
|
-
|
76
|
+
|
77
|
+
property =
|
78
|
+
if property.nil? && default.nil?
|
79
|
+
return nil
|
80
|
+
elsif property.nil?
|
81
|
+
default
|
82
|
+
elsif default.nil?
|
83
|
+
property
|
84
|
+
elsif default.class.name != property.class.name
|
85
|
+
raise MismatchedTypes, "Error loading file #{file}: mismatch between default values (type #{default.class.name}) and #{env_to_load} (type #{property.class.name})"
|
86
|
+
else
|
87
|
+
case default
|
88
|
+
when Hash then default.deep_merge(property)
|
89
|
+
when Array then default + property
|
90
|
+
else property
|
91
|
+
end
|
92
|
+
end
|
74
93
|
|
75
94
|
[property_name, use_hashie_if_hash(property)]
|
76
95
|
end
|
data/lib/figleaf/version.rb
CHANGED
data/lib/figleaf.rb
CHANGED
@@ -1,5 +1,8 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "active_support/concern"
|
2
4
|
require "active_support/core_ext/class/attribute"
|
5
|
+
require "active_support/core_ext/hash"
|
3
6
|
require "active_support/core_ext/module/attribute_accessors"
|
4
7
|
require "active_support/core_ext/object/blank"
|
5
8
|
require "erb"
|
@@ -10,6 +13,7 @@ require "yaml"
|
|
10
13
|
module Figleaf
|
11
14
|
autoload :Config, "figleaf/config"
|
12
15
|
autoload :Fighash, "figleaf/fighash"
|
16
|
+
autoload :LazyBlockHash, "figleaf/lazy_block_hash"
|
13
17
|
autoload :Settings, "figleaf/settings"
|
14
18
|
autoload :VERSION, "figleaf/version"
|
15
19
|
end
|
data/spec/figleaf/config_spec.rb
CHANGED
@@ -1,12 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "spec_helper"
|
2
4
|
|
3
5
|
describe Figleaf::Settings do
|
6
|
+
# Force a freshly loaded Figleaf::Settings class on every test run
|
7
|
+
before do
|
8
|
+
Figleaf.send(:remove_const, :Settings) if Figleaf.const_defined?(:Settings)
|
9
|
+
load "lib/figleaf/settings.rb"
|
10
|
+
end
|
11
|
+
|
12
|
+
# Force a new reference to the class
|
13
|
+
subject(:described_class) { Figleaf::Settings }
|
14
|
+
|
4
15
|
describe "self.load_settings" do
|
5
|
-
|
6
|
-
@fixtures_path = File.expand_path("../../fixtures/*.yml", __FILE__)
|
16
|
+
let(:fixtures_path) { File.expand_path("../../fixtures/*.yml", __FILE__) }
|
7
17
|
|
8
|
-
|
9
|
-
end
|
18
|
+
before { described_class.load_settings(fixtures_path, "test") }
|
10
19
|
|
11
20
|
it "load some service" do
|
12
21
|
expect(described_class.service["foo"]).to eq("bar")
|
@@ -42,7 +51,7 @@ describe Figleaf::Settings do
|
|
42
51
|
end
|
43
52
|
|
44
53
|
it "and for boolean (false)" do
|
45
|
-
described_class.load_settings(
|
54
|
+
described_class.load_settings(fixtures_path, "alt")
|
46
55
|
expect(described_class.boolean).to eq(false)
|
47
56
|
end
|
48
57
|
|
@@ -81,6 +90,29 @@ describe Figleaf::Settings do
|
|
81
90
|
end
|
82
91
|
end
|
83
92
|
|
93
|
+
context "incompatible types" do
|
94
|
+
context "bad overrides" do
|
95
|
+
subject(:settings) { described_class.bad_overrides }
|
96
|
+
|
97
|
+
let(:fixtures_path) { File.expand_path("../../fixtures/type_errors/bad_overrides.yml", __FILE__) }
|
98
|
+
|
99
|
+
it "raises an error when overloading a hash with an array" do
|
100
|
+
expect { described_class.load_settings(fixtures_path, "array") }
|
101
|
+
.to raise_error(described_class::MismatchedTypes)
|
102
|
+
end
|
103
|
+
|
104
|
+
it "raises an error when overloading a hash with a string" do
|
105
|
+
expect { described_class.load_settings(fixtures_path, "string") }
|
106
|
+
.to raise_error(described_class::MismatchedTypes)
|
107
|
+
end
|
108
|
+
|
109
|
+
it "raises an error when overloading a hash with a bool" do
|
110
|
+
expect { described_class.load_settings(fixtures_path, "bool") }
|
111
|
+
.to raise_error(described_class::MismatchedTypes)
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
84
116
|
context "overloading settings" do
|
85
117
|
before do
|
86
118
|
overload = File.expand_path("../../fixtures/extra/*.yml", __FILE__)
|
@@ -134,22 +166,68 @@ describe Figleaf::Settings do
|
|
134
166
|
end
|
135
167
|
end
|
136
168
|
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
169
|
+
shared_examples_for "merged hashes" do
|
170
|
+
context "when using the default" do
|
171
|
+
before { described_class.load_settings(fixtures_path, "some_env") }
|
172
|
+
|
173
|
+
it "test a non overridden environment" do
|
174
|
+
expect(merged).to match(
|
175
|
+
"leaf1" => "default1",
|
176
|
+
"root1" => {"leaf11" => "default2"},
|
177
|
+
"root2" => {
|
178
|
+
"leaf21" => "default3",
|
179
|
+
"root21" => {
|
180
|
+
"leaf211" => "default4",
|
181
|
+
"root211" => {"leaf2111" => "default5"}
|
182
|
+
}
|
183
|
+
}
|
184
|
+
)
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
context "with an overridden environment" do
|
189
|
+
before { described_class.load_settings(fixtures_path, "overridden") }
|
190
|
+
|
191
|
+
it "merges the values" do
|
192
|
+
expect(merged).to match(
|
193
|
+
"leaf1" => "default1",
|
194
|
+
"leaf2" => "overridden1",
|
195
|
+
"root1" => {"leaf11" => "overridden2"},
|
196
|
+
"root2" => {
|
197
|
+
"leaf21" => "default3",
|
198
|
+
"leaf22" => "overridden3",
|
199
|
+
"root21" => {
|
200
|
+
"leaf211" => "overridden4",
|
201
|
+
"root211" => {
|
202
|
+
"leaf2111" => "default5",
|
203
|
+
"leaf2112" => "overridden5"
|
204
|
+
}
|
205
|
+
}
|
206
|
+
}
|
207
|
+
)
|
208
|
+
end
|
141
209
|
end
|
210
|
+
end
|
211
|
+
|
212
|
+
context "load yaml files" do
|
213
|
+
let(:fixtures_path) { File.expand_path("../../fixtures/**/*.yaml", __FILE__) }
|
214
|
+
|
215
|
+
before { described_class.load_settings(fixtures_path, "test") }
|
142
216
|
|
143
217
|
it "reads them just fine" do
|
144
218
|
expect(described_class.yaml.works).to eq("here")
|
145
219
|
end
|
220
|
+
|
221
|
+
context "hashes are merged" do
|
222
|
+
subject(:merged) { described_class.merged_yaml }
|
223
|
+
it_behaves_like "merged hashes"
|
224
|
+
end
|
146
225
|
end
|
147
226
|
|
148
227
|
context "load ruby files" do
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
end
|
228
|
+
let(:fixtures_path) { File.expand_path("../../fixtures/extra/*.rb", __FILE__) }
|
229
|
+
|
230
|
+
before { described_class.load_settings(fixtures_path, "test") }
|
153
231
|
|
154
232
|
it "load indifferently the key names" do
|
155
233
|
expect(described_class.code["foo"]).to eq("bar")
|
@@ -188,5 +266,10 @@ describe Figleaf::Settings do
|
|
188
266
|
it "and for regexp values" do
|
189
267
|
expect(described_class.regexp.value).to eq(/\Amatcher\z/)
|
190
268
|
end
|
269
|
+
|
270
|
+
context "hashes are merged" do
|
271
|
+
subject(:merged) { described_class.merged_rb }
|
272
|
+
it_behaves_like "merged hashes"
|
273
|
+
end
|
191
274
|
end
|
192
275
|
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
default do
|
2
|
+
leaf1 "default1"
|
3
|
+
root1(leaf11: "default2")
|
4
|
+
root2(
|
5
|
+
leaf21: "default3",
|
6
|
+
root21: {
|
7
|
+
leaf211: "default4",
|
8
|
+
root211: {
|
9
|
+
leaf2111: "default5"
|
10
|
+
}
|
11
|
+
}
|
12
|
+
)
|
13
|
+
end
|
14
|
+
|
15
|
+
unused do
|
16
|
+
root1(leaf11: "unused11")
|
17
|
+
root2(root21: {root21: {leaf211: "unused2"}})
|
18
|
+
end
|
19
|
+
|
20
|
+
overridden do
|
21
|
+
leaf2 "overridden1"
|
22
|
+
root1(leaf11: "overridden2")
|
23
|
+
|
24
|
+
root2(
|
25
|
+
leaf22: "overridden3",
|
26
|
+
root21: {
|
27
|
+
leaf211: "overridden4",
|
28
|
+
root211: {
|
29
|
+
leaf2112: "overridden5"
|
30
|
+
}
|
31
|
+
}
|
32
|
+
)
|
33
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
---
|
2
|
+
default:
|
3
|
+
leaf1: default1
|
4
|
+
root1:
|
5
|
+
leaf11: default2
|
6
|
+
root2:
|
7
|
+
leaf21: default3
|
8
|
+
root21:
|
9
|
+
leaf211: default4
|
10
|
+
root211:
|
11
|
+
leaf2111: default5
|
12
|
+
|
13
|
+
unused:
|
14
|
+
root1:
|
15
|
+
leaf11: unused1
|
16
|
+
root2:
|
17
|
+
root21:
|
18
|
+
leaf211: unused2
|
19
|
+
|
20
|
+
overridden:
|
21
|
+
leaf2: overridden1
|
22
|
+
root1:
|
23
|
+
leaf11: overridden2
|
24
|
+
root2:
|
25
|
+
leaf22: overridden3
|
26
|
+
root21:
|
27
|
+
leaf211: overridden4
|
28
|
+
root211:
|
29
|
+
leaf2112: overridden5
|
metadata
CHANGED
@@ -1,14 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: figleaf
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Juan C. Müller
|
8
|
-
autorequire:
|
9
8
|
bindir: bin
|
10
9
|
cert_chain: []
|
11
|
-
date:
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
12
11
|
dependencies:
|
13
12
|
- !ruby/object:Gem::Dependency
|
14
13
|
name: activesupport
|
@@ -47,6 +46,7 @@ extra_rdoc_files: []
|
|
47
46
|
files:
|
48
47
|
- ".gitignore"
|
49
48
|
- ".rspec"
|
49
|
+
- ".solargraph.yml"
|
50
50
|
- ".travis.yml"
|
51
51
|
- CHANGELOG.md
|
52
52
|
- Gemfile
|
@@ -59,6 +59,7 @@ files:
|
|
59
59
|
- lib/figleaf.rb
|
60
60
|
- lib/figleaf/config.rb
|
61
61
|
- lib/figleaf/fighash.rb
|
62
|
+
- lib/figleaf/lazy_block_hash.rb
|
62
63
|
- lib/figleaf/settings.rb
|
63
64
|
- lib/figleaf/version.rb
|
64
65
|
- spec/figleaf/config_spec.rb
|
@@ -75,17 +76,19 @@ files:
|
|
75
76
|
- spec/fixtures/extra/code.rb
|
76
77
|
- spec/fixtures/extra/default.yml
|
77
78
|
- spec/fixtures/extra/default_anchor.yml
|
79
|
+
- spec/fixtures/extra/merged_rb.rb
|
80
|
+
- spec/fixtures/extra/merged_yaml.yaml
|
78
81
|
- spec/fixtures/extra/regexp.rb
|
79
82
|
- spec/fixtures/extra/service.yml
|
80
83
|
- spec/fixtures/regexp.yml
|
81
84
|
- spec/fixtures/service.yml
|
82
85
|
- spec/fixtures/string.yml
|
86
|
+
- spec/fixtures/type_errors/bad_overrides.yml
|
83
87
|
- spec/fixtures/yaml.yaml
|
84
88
|
- spec/spec_helper.rb
|
85
89
|
homepage: http://github.com/jcmuller/figleaf
|
86
90
|
licenses: []
|
87
91
|
metadata: {}
|
88
|
-
post_install_message:
|
89
92
|
rdoc_options: []
|
90
93
|
require_paths:
|
91
94
|
- lib
|
@@ -100,8 +103,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
100
103
|
- !ruby/object:Gem::Version
|
101
104
|
version: '0'
|
102
105
|
requirements: []
|
103
|
-
rubygems_version: 3.
|
104
|
-
signing_key:
|
106
|
+
rubygems_version: 3.7.1
|
105
107
|
specification_version: 4
|
106
108
|
summary: YAML based DRY settings manager.
|
107
109
|
test_files: []
|