declarative 0.0.7 → 0.0.8
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/.travis.yml +1 -3
- data/CHANGES.md +5 -0
- data/lib/declarative/defaults.rb +26 -8
- data/lib/declarative/schema.rb +3 -2
- data/lib/declarative/version.rb +1 -1
- data/test/defaults_test.rb +27 -0
- data/test/schema_test.rb +29 -1
- data/test/test_helper.rb +2 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 821bc94b5316e638016b621016276d0b43ae7545
|
4
|
+
data.tar.gz: 330cde61ef63784b7111de6c3167c0bdc9329e37
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 84188b1b135bf31cb9d2c7b2d08c4145688a39a246c8fc99fe04d3c4824350ee20741142de62f2362f8b9d589d44c978db0694c6c923892fab23f8a1507e29c7
|
7
|
+
data.tar.gz: 2b056f07f475a823d6763c64c4e5272a218c5d214052f0e8bbe8b3730cd8e059ae995a23bc9239fa34c40392d484e2b44763b20edccfb9f931c61f8f9a55e9b2
|
data/.travis.yml
CHANGED
data/CHANGES.md
CHANGED
@@ -1,3 +1,8 @@
|
|
1
|
+
# 0.0.8
|
2
|
+
|
3
|
+
* When calling `Schema#defaults` (or `Defaults#merge!`) multiple times, same-named arrays will be joined instead of overridden. This fixes a common problem when merging different default settings.
|
4
|
+
* Remove `Defaults#[]` and `Defaults#[]=`. This now happens via `#merge!`.
|
5
|
+
|
1
6
|
# 0.0.7
|
2
7
|
|
3
8
|
* Simplify `Defaults` and remove a warning in Ruby 2.2.3.
|
data/lib/declarative/defaults.rb
CHANGED
@@ -7,20 +7,38 @@ module Declarative
|
|
7
7
|
@dynamic_options = ->(*) { Hash.new }
|
8
8
|
end
|
9
9
|
|
10
|
-
|
11
|
-
|
10
|
+
# Set default values. Usually called in Schema::defaults.
|
11
|
+
# This can be called multiple times and will "deep-merge" arrays, e.g. `_features: []`.
|
12
|
+
def merge!(hash={}, &block)
|
13
|
+
@static_options = Merge.(@static_options, hash)
|
14
|
+
|
12
15
|
@dynamic_options = block if block_given?
|
13
16
|
self
|
14
17
|
end
|
15
18
|
|
16
|
-
|
17
|
-
delegates :@static_options, :[], :[]= # mutuable API!
|
18
|
-
|
19
|
-
# TODO: allow to receive rest of options/block in dynamic block. or, rather, test it as it was already implemented.
|
19
|
+
# Evaluate defaults and merge given_options into them.
|
20
20
|
def call(name, given_options)
|
21
|
-
options
|
22
|
-
|
21
|
+
# TODO: allow to receive rest of options/block in dynamic block. or, rather, test it as it was already implemented.
|
22
|
+
evaluated_options = @dynamic_options.(name, given_options)
|
23
|
+
|
24
|
+
options = Merge.(@static_options, evaluated_options)
|
23
25
|
options = options.merge(given_options)
|
24
26
|
end
|
27
|
+
|
28
|
+
# Private! Don't use this anywhere.
|
29
|
+
# Merges two hashes and joins same-named arrays. This is often needed
|
30
|
+
# when dealing with defaults.
|
31
|
+
class Merge
|
32
|
+
def self.call(a, b)
|
33
|
+
a = a.dup
|
34
|
+
b.each do |k, v|
|
35
|
+
a[k] = v and next unless a.has_key?(k)
|
36
|
+
a[k] = v and next unless a[k].is_a?(Array)
|
37
|
+
a[k] = a[k] += v # only for arrays.
|
38
|
+
end
|
39
|
+
|
40
|
+
a
|
41
|
+
end
|
42
|
+
end
|
25
43
|
end
|
26
44
|
end
|
data/lib/declarative/schema.rb
CHANGED
@@ -78,8 +78,9 @@ module Declarative
|
|
78
78
|
def register_feature(mod)
|
79
79
|
heritage.record(:register_feature, mod) # this is only for inheritance between decorators and modules!!! ("horizontal and vertical")
|
80
80
|
|
81
|
-
defaults
|
82
|
-
defaults[:_features]
|
81
|
+
defaults.merge!(_features: [mod])
|
82
|
+
# defaults[:_features] ||= []
|
83
|
+
# defaults[:_features] << mod
|
83
84
|
end
|
84
85
|
end
|
85
86
|
end
|
data/lib/declarative/version.rb
CHANGED
data/test/defaults_test.rb
CHANGED
@@ -58,4 +58,31 @@ class DefaultsOptionsTest < Minitest::Spec
|
|
58
58
|
schema.inspect.must_equal '{"title"=>#<Declarative::Definitions::Definition: @options={:render_nil=>true, :as=>"Title", :name=>"title"}>, "author_name"=>#<Declarative::Definitions::Definition: @options={:name=>"author_name"}>, "description"=>#<Declarative::Definitions::Definition: @options={:render_nil=>true, :as=>"DESCRIPTION", :name=>"description"}>}'
|
59
59
|
end
|
60
60
|
end
|
61
|
+
|
62
|
+
|
63
|
+
describe "multiple Defaults#merge!" do
|
64
|
+
it "merges arrays automatically" do
|
65
|
+
defaults.merge!(a: 1, b: 2)
|
66
|
+
defaults.merge!( b: 3, _features: ["A"])
|
67
|
+
defaults.merge!( _features: ["B", "C"])
|
68
|
+
defaults.(nil, {}).inspect.must_equal "{:a=>1, :b=>3, :_features=>[\"A\", \"B\", \"C\"]}"
|
69
|
+
end
|
70
|
+
|
71
|
+
it "what" do
|
72
|
+
defaults.merge!(_features: ["A"]) do |name, options|
|
73
|
+
{ _features: ["B", "D"] }
|
74
|
+
end
|
75
|
+
|
76
|
+
defaults.(nil, {}).inspect.must_equal "{:_features=>[\"A\", \"B\", \"D\"]}"
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
class DefaultsMergeTest < Minitest::Spec
|
82
|
+
it do
|
83
|
+
a = { a: "a", features: ["b"] }
|
84
|
+
b = { a: "a", features: ["c", "d"], b: "b" }
|
85
|
+
|
86
|
+
Declarative::Defaults::Merge.(a, b).must_equal({:a=>"a", :features=>["b", "c", "d"], :b=>"b"})
|
87
|
+
end
|
61
88
|
end
|
data/test/schema_test.rb
CHANGED
@@ -85,4 +85,32 @@ class SchemaTest < Minitest::Spec
|
|
85
85
|
ConcreteWithOptions.extend(Declarative::Inspect::Schema).inspect.must_equal 'Schema: {"artist"=>#<Declarative::Definitions::Definition: @options={:cool=>true, :nested=>#<OpenStruct cool=true>, :name=>"artist"}>}'
|
86
86
|
end
|
87
87
|
end
|
88
|
-
|
88
|
+
|
89
|
+
describe "multiple ::defaults" do
|
90
|
+
class Twin < Decorator
|
91
|
+
module A; end
|
92
|
+
module B; end
|
93
|
+
module D; end
|
94
|
+
|
95
|
+
defaults a: "a", _features: [A] do |name|
|
96
|
+
{ first: 1, _features: [D] }
|
97
|
+
end
|
98
|
+
|
99
|
+
# DISCUSS: currently, we only allow one dynamic block.
|
100
|
+
defaults b: "b", _features: [B]# do |name, options|
|
101
|
+
# {}
|
102
|
+
#end
|
103
|
+
|
104
|
+
|
105
|
+
property :id do end
|
106
|
+
end
|
107
|
+
|
108
|
+
it do
|
109
|
+
Twin.extend(Declarative::Inspect::Schema).inspect.must_equal 'Schema: {"id"=>#<Declarative::Definitions::Definition: @options={:a=>"a", :b=>"b", :first=>1, :nested=>Schema: {}, :name=>"id"}>}'
|
110
|
+
# :_features get merged.
|
111
|
+
Twin.definitions.get(:id)[:nested].is_a? Twin::A
|
112
|
+
Twin.definitions.get(:id)[:nested].is_a? Twin::B
|
113
|
+
Twin.definitions.get(:id)[:nested].is_a? Twin::D
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
data/test/test_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: declarative
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nick Sutterer
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-06-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: uber
|