json-schema_builder 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/json/schema_builder/schema.rb +12 -2
- data/lib/json/schema_builder/version.rb +1 -1
- data/spec/unit/schema_spec.rb +35 -11
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 84f7722dca12970cabc9b5a3e3f464883c34272c
|
4
|
+
data.tar.gz: dbb78e125209b463035cbd05d29abbff4da0e95a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a98084c33aedabf1fc46b37a56ff71da7ecf179013bac5d8e4edaf4292a88ba4ea5f8e49655b39f115b6525c8b7d87f7c80fa3c7f7aa17a721620790b55adf37
|
7
|
+
data.tar.gz: 907cabeb1f50698e2d7c4103a43c273521ddba15dfeac29fcdc438563d697577675bae8dd764052bbaae8a8a3d99c9305dc98329a025d46e8544c46acaba5af7
|
@@ -9,13 +9,23 @@ module JSON
|
|
9
9
|
end
|
10
10
|
|
11
11
|
def merge(schema)
|
12
|
-
self.class.new data
|
12
|
+
self.class.new _deep_merge(data, schema.data)
|
13
13
|
end
|
14
14
|
|
15
15
|
def merge!(schema)
|
16
|
-
@data = data
|
16
|
+
@data = _deep_merge(data, schema.data)
|
17
17
|
self
|
18
18
|
end
|
19
|
+
|
20
|
+
def _deep_merge(this, other)
|
21
|
+
this.deep_merge(other) do |current_key, this_value, other_value|
|
22
|
+
if this_value.is_a?(::Array) && other_value.is_a?(::Array)
|
23
|
+
this_value + other_value
|
24
|
+
else
|
25
|
+
other_value
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
19
29
|
end
|
20
30
|
end
|
21
31
|
end
|
data/spec/unit/schema_spec.rb
CHANGED
@@ -1,35 +1,59 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
RSpec.describe JSON::SchemaBuilder::Schema, type: :unit do
|
4
|
-
let(:schema){ described_class.new a: 1, b: { c: 3 } }
|
5
|
-
let(:other){ described_class.new a: 2, "b" => { d: 4 } }
|
4
|
+
let(:schema){ described_class.new a: 1, b: { c: 3 }, array: [123, { foo: 1, bar: { baz: 0 }}] }
|
5
|
+
let(:other){ described_class.new a: 2, "b" => { d: 4 }, array: [456, { foo: 2, bar: { qux: 1 }}] }
|
6
|
+
let(:merged) do
|
7
|
+
{
|
8
|
+
"a" => 2,
|
9
|
+
"b" => {
|
10
|
+
"c" => 3,
|
11
|
+
"d" => 4
|
12
|
+
},
|
13
|
+
"array" => [
|
14
|
+
123, {
|
15
|
+
"foo" => 1,
|
16
|
+
"bar" => {
|
17
|
+
"baz" => 0,
|
18
|
+
}
|
19
|
+
},
|
20
|
+
456, {
|
21
|
+
"foo" => 2,
|
22
|
+
"bar" => {
|
23
|
+
"qux" => 1
|
24
|
+
}
|
25
|
+
}
|
26
|
+
]
|
27
|
+
}
|
28
|
+
end
|
29
|
+
|
6
30
|
its(:data){ is_expected.to be_a(HashWithIndifferentAccess) }
|
7
31
|
|
8
32
|
describe '#merge' do
|
9
33
|
it 'should deep merge' do
|
10
|
-
|
11
|
-
expect(
|
12
|
-
expect(
|
34
|
+
merged_schema = schema.merge other
|
35
|
+
expect(merged_schema).to be_a described_class
|
36
|
+
expect(merged_schema.data).to eql merged
|
13
37
|
end
|
14
38
|
|
15
39
|
it 'should not modify the source schema' do
|
16
|
-
expect{ schema.merge other }.to_not change{ schema.
|
40
|
+
expect{ schema.merge other }.to_not change{ schema.data }
|
17
41
|
end
|
18
42
|
|
19
43
|
it 'should not modify the merging schema' do
|
20
|
-
expect{ schema.merge other }.to_not change{ other.
|
44
|
+
expect{ schema.merge other }.to_not change{ other.data }
|
21
45
|
end
|
22
46
|
end
|
23
47
|
|
24
48
|
describe '#merge!' do
|
25
49
|
it 'should deep merge in place' do
|
26
|
-
|
27
|
-
expect(
|
28
|
-
expect(
|
50
|
+
merged_schema = schema.merge! other
|
51
|
+
expect(merged_schema).to be_a described_class
|
52
|
+
expect(merged_schema.data).to eql merged
|
29
53
|
end
|
30
54
|
|
31
55
|
it 'should not modify the merging schema' do
|
32
|
-
expect{ schema.merge! other }.to_not change { other.
|
56
|
+
expect{ schema.merge! other }.to_not change { other.data }
|
33
57
|
end
|
34
58
|
end
|
35
59
|
end
|