settingable 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +3 -1
- data/.rubocop.yml +6 -3
- data/lib/settingable.rb +3 -0
- data/lib/settingable/deep_merge.rb +52 -0
- data/lib/settingable/settings.rb +3 -1
- data/lib/settingable/version.rb +4 -1
- data/settingable.gemspec +2 -0
- data/spec/settingable/deep_merge_spec.rb +32 -0
- data/spec/settingable/settings_spec.rb +12 -1
- data/spec/settingable_spec.rb +2 -0
- data/spec/spec_helper.rb +2 -0
- metadata +19 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3a0a903c881eea9d4884ecea2b35bbab704eee6e
|
4
|
+
data.tar.gz: 63b5b9419e6eb8f4afb0781009c61182c14516be
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8f5c6eed43a3b4ca3af56e393ad665f5c6c3088aade3b155494cd5c9a0e0fa00468287b83e286664f1dfa9bfe1512a309ccb64f89924c2d66180950c22b0a285
|
7
|
+
data.tar.gz: a43f6490d4254b812bb845d064e5a89debada8491f7d114d2c9cd28bca5b6d3b7a2dd03cad3bc48e2e3a2e1f5b6ad905319ea43229a2cdae82adfa3d739b0c3e
|
data/.rspec
CHANGED
data/.rubocop.yml
CHANGED
data/lib/settingable.rb
CHANGED
@@ -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
|
data/lib/settingable/settings.rb
CHANGED
@@ -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
|
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
|
data/lib/settingable/version.rb
CHANGED
data/settingable.gemspec
CHANGED
@@ -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
|
-
|
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
|
data/spec/settingable_spec.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
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.
|
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-
|
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
|