config_hound 1.1.0 → 1.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cb89f3e818a6b9ceb8e00aa4d10702425efb7e03
4
- data.tar.gz: bb2391444edc55b0639372c426a00685f7c5fdbb
3
+ metadata.gz: 2274553ac129370bd650d33390fe3fbde49a86b3
4
+ data.tar.gz: 690579fd733a8670f587ef9e190a2bb1d0843e2e
5
5
  SHA512:
6
- metadata.gz: 87e70b81bef285e6ef51d55d314794d2b6606e4ab1bcfeab8d640ffee49f21968be1b23db14f9e085b1b93febe3dbcd8942bfa3df8728d6b435bf3763c15d1db
7
- data.tar.gz: d85b9c7a397c671e0df5ef9693f80781d822029d7e9e793b1ed7621d21bbd065f6789284f7521ca4f8755514b5f95c06a5d6448b3bbf98a5e6f27efa58b6399a
6
+ metadata.gz: be1159fe1d0adc7ec520f5df420ad2f639efe0e3937ace781519aa6dccc3b1b4b4284719196c839e5a0ed2f42c1ee03a1f4175b7b218c118fb3e5bb6d7e1b206
7
+ data.tar.gz: 7d4a46f6e76069e9518e02ab6904d5a953ec0839d8cb5a588dd4d028835f31a2f4bb444291a976c002279bb252409882df7873dc1de80734831d9c9ecf4df4cc
@@ -0,0 +1,10 @@
1
+ module ConfigHound
2
+
3
+ def self.deep_merge_into(h1, h2)
4
+ return h2 unless h1.is_a?(Hash) && h2.is_a?(Hash)
5
+ h1.merge(h2) do |_key, v1, v2|
6
+ deep_merge_into(v1, v2)
7
+ end
8
+ end
9
+
10
+ end
@@ -6,43 +6,27 @@ module ConfigHound
6
6
 
7
7
  DEFAULT_INCLUDE_KEY = "_include"
8
8
 
9
- def self.load(paths, options = {})
10
- data = {}
11
- loader = new(data, options)
12
- Array(paths).each do |path|
13
- loader.load(path)
14
- end
15
- data
16
- end
17
-
18
- def initialize(data, options)
19
- @data = data
9
+ def initialize(options = {})
20
10
  @include_key = options.fetch(:include_key, DEFAULT_INCLUDE_KEY)
21
11
  end
22
12
 
23
- def load(path)
24
- resource = Resource.new(path.to_s)
25
- include_into!(data, resource.load)
26
- includes = Array(data.delete(include_key))
27
- includes.each do |i|
28
- load(resource.resolve(i))
29
- end
13
+ def load(paths)
14
+ Array(paths).reverse.map do |path|
15
+ load_resource(Resource[path])
16
+ end.reduce({}, &ConfigHound.method(:deep_merge_into))
30
17
  end
31
18
 
32
19
  private
33
20
 
34
- attr_reader :data
35
21
  attr_reader :include_key
36
22
 
37
- def include_into!(data, more_data)
38
- return unless data.is_a?(Hash) && more_data.is_a?(Hash)
39
- more_data.each do |key, value|
40
- if data.has_key?(key)
41
- include_into!(data[key], value)
42
- else
43
- data[key] = value
44
- end
23
+ def load_resource(resource)
24
+ raw_data = resource.load
25
+ includes = Array(raw_data.delete(include_key))
26
+ included_resources = includes.map do |relative_path|
27
+ resource.resolve(relative_path)
45
28
  end
29
+ ConfigHound.deep_merge_into(load(included_resources), raw_data)
46
30
  end
47
31
 
48
32
  end
@@ -10,8 +10,30 @@ module ConfigHound
10
10
  #
11
11
  class Resource
12
12
 
13
- def initialize(path)
14
- @uri = uri_for(path)
13
+ class << self
14
+
15
+ def [](arg)
16
+ return arg if arg.is_a?(Resource)
17
+ new(uri_for(arg))
18
+ end
19
+
20
+ private(:new)
21
+
22
+ private
23
+
24
+ def uri_for(arg)
25
+ case arg
26
+ when URI
27
+ arg
28
+ when %r{^\w+:/}
29
+ URI(arg)
30
+ when %r{^/}
31
+ URI("file:#{arg}")
32
+ else
33
+ URI("file:#{File.expand_path(arg)}")
34
+ end
35
+ end
36
+
15
37
  end
16
38
 
17
39
  def to_s
@@ -19,7 +41,7 @@ module ConfigHound
19
41
  end
20
42
 
21
43
  def resolve(path)
22
- self.class.new(uri + path)
44
+ Resource[uri + path]
23
45
  end
24
46
 
25
47
  def read
@@ -40,21 +62,12 @@ module ConfigHound
40
62
 
41
63
  private
42
64
 
43
- attr_reader :uri
44
-
45
- def uri_for(path)
46
- case path
47
- when URI
48
- path
49
- when %r{^\w+:/}
50
- URI(path)
51
- when %r{^/}
52
- URI("file:#{path}")
53
- else
54
- URI("file:#{File.expand_path(path)}")
55
- end
65
+ def initialize(uri)
66
+ @uri = uri
56
67
  end
57
68
 
69
+ attr_reader :uri
70
+
58
71
  end
59
72
 
60
73
  end
@@ -1,3 +1,3 @@
1
1
  module ConfigHound
2
- VERSION = "1.1.0"
2
+ VERSION = "1.2.0"
3
3
  end
data/lib/config_hound.rb CHANGED
@@ -3,8 +3,8 @@ require "config_hound/version"
3
3
 
4
4
  module ConfigHound
5
5
 
6
- def self.load(*args)
7
- Loader.load(*args)
6
+ def self.load(paths, options = {})
7
+ Loader.new(options).load(paths)
8
8
  end
9
9
 
10
10
  end
@@ -0,0 +1,81 @@
1
+ require "spec_helper"
2
+
3
+ require "config_hound/deep_merge"
4
+
5
+ describe ConfigHound do
6
+
7
+ describe ".deep_merge_into" do
8
+
9
+ let(:result) { ConfigHound.deep_merge_into(v1, v2) }
10
+
11
+ context "with non-Hash arguments" do
12
+
13
+ let(:v1) { "a" }
14
+ let(:v2) { "b" }
15
+
16
+ it "takes the second value" do
17
+ expect(result).to eq("b")
18
+ end
19
+
20
+ end
21
+
22
+ context "with independent keys" do
23
+
24
+ let(:v1) { {a: 1} }
25
+ let(:v2) { {b: 2} }
26
+
27
+ it "merges" do
28
+ expect(result).to eq(a: 1, b: 2)
29
+ end
30
+
31
+ end
32
+
33
+ context "when keys clash" do
34
+
35
+ let(:v1) { {x: 1} }
36
+ let(:v2) { {x: 2} }
37
+
38
+ it "takes the second value" do
39
+ expect(result).to eq(x: 2)
40
+ end
41
+
42
+ end
43
+
44
+ context "with nested values" do
45
+
46
+ let(:v1) do
47
+ {
48
+ :foo => {
49
+ :bar => {
50
+ :x => 1
51
+ }
52
+ }
53
+ }
54
+ end
55
+
56
+ let(:v2) do
57
+ {
58
+ :foo => {
59
+ :bar => {
60
+ :y => 2
61
+ }
62
+ }
63
+ }
64
+ end
65
+
66
+ it "merges deeply" do
67
+ expect(result).to eq(
68
+ :foo => {
69
+ :bar => {
70
+ :x => 1,
71
+ :y => 2
72
+ }
73
+ }
74
+ )
75
+ end
76
+
77
+ end
78
+
79
+ end
80
+
81
+ end
@@ -5,7 +5,7 @@ require "uri"
5
5
 
6
6
  describe ConfigHound::Resource do
7
7
 
8
- let(:resource) { described_class.new(path) }
8
+ let(:resource) { described_class[path] }
9
9
 
10
10
  context "with a URI" do
11
11
 
@@ -4,11 +4,7 @@ require "config_hound"
4
4
 
5
5
  describe ConfigHound do
6
6
 
7
- def load(path)
8
- ConfigHound.load(path)
9
- end
10
-
11
- let(:config) { load(["fileA.yml", "fileB.yml"]) }
7
+ let(:config) { ConfigHound.load(["fileA.yml", "fileB.yml"]) }
12
8
 
13
9
  given_resource "fileA.yml", %{
14
10
  source: A
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: config_hound
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Williams
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-10 00:00:00.000000000 Z
11
+ date: 2015-08-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -53,10 +53,12 @@ files:
53
53
  - Rakefile
54
54
  - config_hound.gemspec
55
55
  - lib/config_hound.rb
56
+ - lib/config_hound/deep_merge.rb
56
57
  - lib/config_hound/loader.rb
57
58
  - lib/config_hound/parser.rb
58
59
  - lib/config_hound/resource.rb
59
60
  - lib/config_hound/version.rb
61
+ - spec/config_hound/deep_merge_spec.rb
60
62
  - spec/config_hound/resource_spec.rb
61
63
  - spec/features/basics_spec.rb
62
64
  - spec/features/include_spec.rb
@@ -82,11 +84,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
82
84
  version: '0'
83
85
  requirements: []
84
86
  rubyforge_project:
85
- rubygems_version: 2.4.5
87
+ rubygems_version: 2.4.8
86
88
  signing_key:
87
89
  specification_version: 4
88
90
  summary: Sniffs out config, wherever it may be.
89
91
  test_files:
92
+ - spec/config_hound/deep_merge_spec.rb
90
93
  - spec/config_hound/resource_spec.rb
91
94
  - spec/features/basics_spec.rb
92
95
  - spec/features/include_spec.rb