settingable 0.1.0 → 0.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: 44dd382347efa4660d7eb7329684165b25a9326b
4
- data.tar.gz: 7516072936d6a315e5bbf2322721bf17d39f7e86
3
+ metadata.gz: 3a0a903c881eea9d4884ecea2b35bbab704eee6e
4
+ data.tar.gz: 63b5b9419e6eb8f4afb0781009c61182c14516be
5
5
  SHA512:
6
- metadata.gz: 5625c2068e64528122c1fe8d3e927e52a1b431629093c1d079bdb7d4b81fba78dd49a7c517dc6443df8f061f58f087e4bdef8e7656aab0d0abf25d3fde60fd70
7
- data.tar.gz: 0b8a3f1bf9c29345be135fdee6ec86fc76bd219e79d1132ab035e972d33173e3d2efc9bc901141e53f38a7324afa24f2e7be4e35f9d877ac3331d44ddeba08aa
6
+ metadata.gz: 8f5c6eed43a3b4ca3af56e393ad665f5c6c3088aade3b155494cd5c9a0e0fa00468287b83e286664f1dfa9bfe1512a309ccb64f89924c2d66180950c22b0a285
7
+ data.tar.gz: a43f6490d4254b812bb845d064e5a89debada8491f7d114d2c9cd28bca5b6d3b7a2dd03cad3bc48e2e3a2e1f5b6ad905319ea43229a2cdae82adfa3d739b0c3e
data/.rspec CHANGED
@@ -1 +1,3 @@
1
- --colour --format documentation
1
+ --colour
2
+ --require spec_helper
3
+ --format documentation
data/.rubocop.yml CHANGED
@@ -1,5 +1,8 @@
1
- Style/ClassAndModuleChildren:
2
- Enabled: false
3
-
4
1
  Style/StringLiterals:
5
2
  EnforcedStyle: double_quotes
3
+ Style/Encoding:
4
+ Enabled: true
5
+ Style/RegexpLiteral:
6
+ EnforcedStyle: mixed
7
+ Metrics/MethodLength:
8
+ Max: 15
data/lib/settingable.rb CHANGED
@@ -1,4 +1,7 @@
1
+ # encoding: utf-8
2
+
1
3
  require "settingable/version"
4
+ require "settingable/deep_merge"
2
5
  require "settingable/settings"
3
6
 
4
7
  # Contains the Setting module.
@@ -0,0 +1,52 @@
1
+ # encoding: utf-8
2
+
3
+ module Settingable
4
+ # A module to handle the merging of a hash.
5
+ module DeepMerge
6
+ module_function
7
+
8
+ # Merges the source into the destination. This is non-destructive
9
+ # to the destination. If a block is given, it's called if both
10
+ # the source and destination have a non-mergable value. Both the
11
+ # values must be a hash for it to be merged. If it is a
12
+ # non-mergable, and the either the destination doesn't have the
13
+ # key, or no block was given, the default is to overwrite the
14
+ # destination with the new value.
15
+ #
16
+ # @param (see .deep_merge!)
17
+ # @return (see .deep_merge!)
18
+ # @yield (see .deep_merge!)
19
+ def deep_merge(destination, source, &block)
20
+ deep_merge!(destination.dup, source, &block)
21
+ end
22
+
23
+ # Merges the source into the destination. This is destructive to
24
+ # the destination. If a block is given, it's called if both the
25
+ # source and destination have a non-mergable value. Both the
26
+ # values must be a hash in order for it to be merged. If it is
27
+ # a non-mergable value, and either the destination doesn't have
28
+ # the key, or no block was given, the default is to overwrite the
29
+ # destination with the new value.
30
+ #
31
+ # @param destination [Hash] The destination hash.
32
+ # @param source [Hash] The source hash.
33
+ # @yield [key, destination_value, source_value] Yields to
34
+ # determine the action that should be taken to merge the two.
35
+ # @return [Hash] The destination.
36
+ def deep_merge!(destination, source, &block)
37
+ source.each do |key, value|
38
+ dvalue = destination[key]
39
+ destination[key] = case
40
+ when value.is_a?(Hash) && dvalue.is_a?(Hash)
41
+ deep_merge(dvalue, value, &block)
42
+ when block && destination.key?(key)
43
+ block.call(key, dvalue, value)
44
+ else
45
+ value
46
+ end
47
+ end
48
+
49
+ destination
50
+ end
51
+ end
52
+ end
@@ -1,3 +1,5 @@
1
+ # encoding: utf-8
2
+
1
3
  require "forwardable"
2
4
 
3
5
  module Settingable
@@ -51,7 +53,7 @@ module Settingable
51
53
  #
52
54
  # @param settings [Hash] The initial settings.
53
55
  def initialize(settings = {})
54
- @settings = self.class.default_settings.merge(settings)
56
+ @settings = DeepMerge.deep_merge(self.class.default_settings, settings)
55
57
  end
56
58
 
57
59
  # Builds the settings construct. It yields itself, and then returns
@@ -1,4 +1,7 @@
1
+ # encoding: utf-8
2
+
3
+ # :nodoc:
1
4
  module Settingable
2
5
  # settingable version
3
- VERSION = "0.1.0"
6
+ VERSION = "0.2.0"
4
7
  end
data/settingable.gemspec CHANGED
@@ -21,4 +21,6 @@ Gem::Specification.new do |gem|
21
21
  gem.add_development_dependency "rake"
22
22
  gem.add_development_dependency "rspec"
23
23
  gem.add_development_dependency "yard"
24
+
25
+ gem.add_dependency "deep_merge"
24
26
  end
@@ -0,0 +1,32 @@
1
+ # encoding: utf-8
2
+
3
+ RSpec.describe Settingable::DeepMerge do
4
+ let(:destination) { { foo: { bar: "baz" }, hello: "world" } }
5
+ let(:source) { { foo: { bar: "ham" } } }
6
+
7
+ describe "#deep_merge" do
8
+ subject { Settingable::DeepMerge.deep_merge(destination, source) }
9
+
10
+ it "merges without modification" do
11
+ expect(subject).to eq foo: { bar: "ham" }, hello: "world"
12
+ expect(destination).to eq foo: { bar: "baz" }, hello: "world"
13
+ expect(source).to eq foo: { bar: "ham" }
14
+ end
15
+ end
16
+
17
+ describe "#deep_merge!" do
18
+ subject { Settingable::DeepMerge.deep_merge!(destination, source) }
19
+
20
+ it "merges with modification" do
21
+ expect(subject).to eq foo: { bar: "ham" }, hello: "world"
22
+ expect(destination).to eq foo: { bar: "ham" }, hello: "world"
23
+ expect(source).to eq foo: { bar: "ham" }
24
+ end
25
+
26
+ it "yields" do
27
+ expect do |y|
28
+ Settingable::DeepMerge.deep_merge!(destination, source, &y)
29
+ end.to yield_control
30
+ end
31
+ end
32
+ end
@@ -1,4 +1,4 @@
1
- require "spec_helper"
1
+ # encoding: utf-8
2
2
 
3
3
  # Test module for Setting.
4
4
  class Configuration
@@ -60,6 +60,17 @@ RSpec.describe Settingable::Settings do
60
60
  subject[:hello] = "you"
61
61
  expect(defaults[:hello]).to eq "world"
62
62
  end
63
+
64
+ context "with recursive settings" do
65
+ let(:defaults) { { hello: { foo: "bar", world: "baz" } }.freeze }
66
+ let(:settings) { { hello: { world: "hello" }, foo: "bar" }.freeze }
67
+
68
+ it "merges the settings" do
69
+ expect(subject[:hello][:world]).to eq "hello"
70
+ expect(defaults[:hello][:world]).to eq "baz"
71
+ expect(subject[:foo]).to eq "bar"
72
+ end
73
+ end
63
74
  end
64
75
 
65
76
  describe "#method_missing" do
@@ -1,3 +1,5 @@
1
+ # encoding: utf-8
2
+
1
3
  require "spec_helper"
2
4
  require "settingable"
3
5
 
data/spec/spec_helper.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # encoding: utf-8
2
+
1
3
  require "rspec"
2
4
  require "settingable"
3
5
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: settingable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy Rodi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-26 00:00:00.000000000 Z
11
+ date: 2015-07-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: deep_merge
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
69
83
  description: Handles configuring your gems.
70
84
  email: redjazz96@gmail.com
71
85
  executables: []
@@ -83,9 +97,11 @@ files:
83
97
  - README.md
84
98
  - Rakefile
85
99
  - lib/settingable.rb
100
+ - lib/settingable/deep_merge.rb
86
101
  - lib/settingable/settings.rb
87
102
  - lib/settingable/version.rb
88
103
  - settingable.gemspec
104
+ - spec/settingable/deep_merge_spec.rb
89
105
  - spec/settingable/settings_spec.rb
90
106
  - spec/settingable_spec.rb
91
107
  - spec/spec_helper.rb
@@ -114,6 +130,7 @@ signing_key:
114
130
  specification_version: 4
115
131
  summary: Handles configuring your gems.
116
132
  test_files:
133
+ - spec/settingable/deep_merge_spec.rb
117
134
  - spec/settingable/settings_spec.rb
118
135
  - spec/settingable_spec.rb
119
136
  - spec/spec_helper.rb