declarative 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c98390ed3e76619fa18ba957ed6b03b05929dd09
4
- data.tar.gz: 66134914bba1a3b8fc39d9f4316e0292dcd53ff0
3
+ metadata.gz: 821bc94b5316e638016b621016276d0b43ae7545
4
+ data.tar.gz: 330cde61ef63784b7111de6c3167c0bdc9329e37
5
5
  SHA512:
6
- metadata.gz: 203b7692773222fe5104e464ac6444fb571b0d0df9b8625be00f7c8b8d03f3dbc8e76972ed19f3128c4c946ca0a98045e6b787ca5dfd00d98310a92667707680
7
- data.tar.gz: 364e53c6e76737d40c1255fca40c7f62db0b20902b4b05238a696579d5d2df0c18a385b000a547a2681b3ef705c155a53c29e55585033278d30a82e393f7b8c3
6
+ metadata.gz: 84188b1b135bf31cb9d2c7b2d08c4145688a39a246c8fc99fe04d3c4824350ee20741142de62f2362f8b9d589d44c978db0694c6c923892fab23f8a1507e29c7
7
+ data.tar.gz: 2b056f07f475a823d6763c64c4e5272a218c5d214052f0e8bbe8b3730cd8e059ae995a23bc9239fa34c40392d484e2b44763b20edccfb9f931c61f8f9a55e9b2
@@ -1,8 +1,6 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.2.3
4
- - 2.1.2
5
- - 2.0.0
3
+ - 2.3.1
6
4
  - 1.9.3
7
5
  gemfile:
8
6
  - Gemfile
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.
@@ -7,20 +7,38 @@ module Declarative
7
7
  @dynamic_options = ->(*) { Hash.new }
8
8
  end
9
9
 
10
- def merge!(hash, &block)
11
- @static_options.merge!(hash) if hash.any?
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
- extend Uber::Delegates
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 = @static_options
22
- options = options.merge(@dynamic_options.(name, given_options))
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
@@ -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[:_features] ||= []
82
- defaults[:_features] << mod
81
+ defaults.merge!(_features: [mod])
82
+ # defaults[:_features] ||= []
83
+ # defaults[:_features] << mod
83
84
  end
84
85
  end
85
86
  end
@@ -1,3 +1,3 @@
1
1
  module Declarative
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
  end
@@ -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
@@ -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
- end
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
@@ -2,4 +2,5 @@ require "declarative"
2
2
  require "minitest/autorun"
3
3
  require "pp"
4
4
 
5
- require "declarative/testing"
5
+ require "declarative/testing"
6
+ require "ostruct"
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.7
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-03-27 00:00:00.000000000 Z
11
+ date: 2016-06-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: uber