json-schema_builder 0.2.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.pryrc +4 -0
- data/json-schema_builder.gemspec +1 -1
- data/lib/json/schema_builder/array.rb +2 -2
- data/lib/json/schema_builder/entity.rb +1 -1
- data/lib/json/schema_builder/object.rb +2 -2
- data/lib/json/schema_builder/schema.rb +10 -5
- data/lib/json/schema_builder/version.rb +1 -1
- data/spec/support/shared_contexts_for_entity.rb +1 -1
- data/spec/unit/entity_spec.rb +13 -13
- data/spec/unit/schema_spec.rb +2 -2
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '095b7ab204946bf21d11560cfce5aded0284bcc2'
|
4
|
+
data.tar.gz: d36a58b116d0ab3677c76fb3235f5c138a76e0ef
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '0883e8aff0dd4d1279f8290e9707bf2997a1b4c8510a1bc0b48164de7b402f9b2c79fab754ec9b3c7a3b935b77b8a536bff218319133ee6bc7a582e662ddefa0'
|
7
|
+
data.tar.gz: 9ea5f6d0668321cfb023acc96ee418a2b0ea22ede9d0811b8e71d50ae365d7637adec06d97f207cff54a23b0eecd4c237c7cdb192700b2965ef8af3002d64d51
|
data/.pryrc
ADDED
data/json-schema_builder.gemspec
CHANGED
@@ -22,7 +22,7 @@ Gem::Specification.new do |spec|
|
|
22
22
|
spec.add_development_dependency 'rake', '>= 10.0'
|
23
23
|
spec.add_development_dependency 'rspec', '~> 3.4'
|
24
24
|
spec.add_development_dependency 'rspec-its'
|
25
|
-
spec.add_development_dependency 'pry'
|
25
|
+
spec.add_development_dependency 'pry-byebug'
|
26
26
|
spec.add_dependency 'activesupport', '>= 4.0'
|
27
27
|
spec.add_dependency 'json-schema', '>= 2'
|
28
28
|
end
|
@@ -11,8 +11,8 @@ module JSON
|
|
11
11
|
|
12
12
|
def items(*args, &block)
|
13
13
|
opts = args.extract_options!
|
14
|
-
schema
|
15
|
-
schema
|
14
|
+
schema[:items] = args.first
|
15
|
+
schema[:items] ||= items_entity(opts, &block).as_json
|
16
16
|
end
|
17
17
|
|
18
18
|
protected
|
@@ -22,12 +22,12 @@ module JSON
|
|
22
22
|
def required(*values)
|
23
23
|
case values
|
24
24
|
when []
|
25
|
-
@schema
|
25
|
+
@schema[:required]
|
26
26
|
when [true]
|
27
27
|
@parent.required ||= []
|
28
28
|
@parent.required << @name
|
29
29
|
else
|
30
|
-
@schema
|
30
|
+
@schema[:required] = values.flatten
|
31
31
|
end
|
32
32
|
end
|
33
33
|
alias_method :required=, :required
|
@@ -1,14 +1,19 @@
|
|
1
|
-
require 'ostruct'
|
2
|
-
|
3
1
|
module JSON
|
4
2
|
module SchemaBuilder
|
5
|
-
class Schema
|
3
|
+
class Schema
|
4
|
+
attr_reader :data
|
5
|
+
delegate :[], :[]=, :to_h, :as_json, to: :data
|
6
|
+
|
7
|
+
def initialize(hash = {})
|
8
|
+
@data = hash.with_indifferent_access
|
9
|
+
end
|
10
|
+
|
6
11
|
def merge(schema)
|
7
|
-
self.class.new
|
12
|
+
self.class.new data.deep_merge(schema.data)
|
8
13
|
end
|
9
14
|
|
10
15
|
def merge!(schema)
|
11
|
-
@
|
16
|
+
@data = data.deep_merge schema.data
|
12
17
|
self
|
13
18
|
end
|
14
19
|
end
|
@@ -18,7 +18,7 @@ RSpec.shared_context 'an entity with a parent' do
|
|
18
18
|
let(:parent){ OpenStruct.new children: [], required: [] }
|
19
19
|
subject do
|
20
20
|
JSON::SchemaBuilder::Entity.new 'name', title: 'test', parent: parent do
|
21
|
-
schema
|
21
|
+
schema[:evaluated_block] = true
|
22
22
|
end
|
23
23
|
end
|
24
24
|
end
|
data/spec/unit/entity_spec.rb
CHANGED
@@ -23,13 +23,13 @@ RSpec.describe JSON::SchemaBuilder::Entity, type: :unit do
|
|
23
23
|
end
|
24
24
|
|
25
25
|
it 'should delegate the reader to the schema' do
|
26
|
-
subject.schema
|
26
|
+
subject.schema[:test] = 1
|
27
27
|
expect(subject.test).to eql 1
|
28
28
|
end
|
29
29
|
|
30
30
|
it 'should delegate the writer to the schema' do
|
31
|
-
subject.test = 1
|
32
|
-
expect(subject.schema
|
31
|
+
subject.schema[:test] = 1
|
32
|
+
expect(subject.schema[:test]).to eql 1
|
33
33
|
end
|
34
34
|
|
35
35
|
it 'should accept function-style writes' do
|
@@ -38,38 +38,38 @@ RSpec.describe JSON::SchemaBuilder::Entity, type: :unit do
|
|
38
38
|
end
|
39
39
|
|
40
40
|
it 'should snakeCase attribute reads' do
|
41
|
-
subject.schema
|
41
|
+
subject.schema[:testName] = 1
|
42
42
|
expect(subject.test_name).to eql 1
|
43
43
|
end
|
44
44
|
|
45
45
|
it 'should snakeCase attribute writes' do
|
46
46
|
subject.test_name = 1
|
47
|
-
expect(subject.schema
|
47
|
+
expect(subject.schema[:testName]).to eql 1
|
48
48
|
end
|
49
49
|
|
50
50
|
it 'should handle array argument reads' do
|
51
|
-
subject.schema
|
51
|
+
subject.schema[:testList] = [1, 2, 3]
|
52
52
|
expect(subject.test_list).to eql [1, 2, 3]
|
53
53
|
end
|
54
54
|
|
55
55
|
it 'should handle array argument writes' do
|
56
56
|
subject.test_list = [1, 2, 3]
|
57
|
-
expect(subject.schema
|
57
|
+
expect(subject.schema[:testList]).to eql [1, 2, 3]
|
58
58
|
end
|
59
59
|
|
60
60
|
it 'should handle array argument lists' do
|
61
61
|
subject.test_list 1, 2, 3
|
62
|
-
expect(subject.schema
|
62
|
+
expect(subject.schema[:testList]).to eql [1, 2, 3]
|
63
63
|
end
|
64
64
|
|
65
65
|
it 'should handle arbitrary key writes' do
|
66
66
|
subject.test_as 1
|
67
|
-
expect(subject.schema
|
68
|
-
expect(subject.schema
|
67
|
+
expect(subject.schema[:testAs]).to be_nil
|
68
|
+
expect(subject.schema[:testOther]).to eql 1
|
69
69
|
end
|
70
70
|
|
71
71
|
it 'should handle arbitrary key reads' do
|
72
|
-
subject.schema
|
72
|
+
subject.schema[:testOther] = 1
|
73
73
|
expect(subject.test_as).to eql 1
|
74
74
|
end
|
75
75
|
end
|
@@ -80,7 +80,7 @@ RSpec.describe JSON::SchemaBuilder::Entity, type: :unit do
|
|
80
80
|
its(:name){ is_expected.to eql 'name' }
|
81
81
|
its(:title){ is_expected.to eql 'test' }
|
82
82
|
its(:parent){ is_expected.to eql parent }
|
83
|
-
its('schema.
|
83
|
+
its('schema.data'){ is_expected.to include evaluated_block: true }
|
84
84
|
its('parent.children'){ is_expected.to include subject }
|
85
85
|
end
|
86
86
|
|
@@ -136,7 +136,7 @@ RSpec.describe JSON::SchemaBuilder::Entity, type: :unit do
|
|
136
136
|
include_context 'an entity'
|
137
137
|
|
138
138
|
it 'should delegate to schema' do
|
139
|
-
expect(subject.schema).to
|
139
|
+
expect(subject.schema).to receive(:as_json)
|
140
140
|
subject.as_json
|
141
141
|
end
|
142
142
|
end
|
data/spec/unit/schema_spec.rb
CHANGED
@@ -3,13 +3,13 @@ require 'spec_helper'
|
|
3
3
|
RSpec.describe JSON::SchemaBuilder::Schema, type: :unit do
|
4
4
|
let(:schema){ described_class.new a: 1, b: { c: 3 } }
|
5
5
|
let(:other){ described_class.new a: 2, "b" => { d: 4 } }
|
6
|
-
|
6
|
+
its(:data){ is_expected.to be_a(HashWithIndifferentAccess) }
|
7
7
|
|
8
8
|
describe '#merge' do
|
9
9
|
it 'should deep merge' do
|
10
10
|
merged = schema.merge other
|
11
11
|
expect(merged).to be_a described_class
|
12
|
-
expect(merged.to_h).to eql a
|
12
|
+
expect(merged.to_h).to eql "a" => 2, "b" => { "c" => 3, "d" => 4 }
|
13
13
|
end
|
14
14
|
|
15
15
|
it 'should not modify the source schema' do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json-schema_builder
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michael Parrish
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-11-
|
11
|
+
date: 2017-11-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -67,7 +67,7 @@ dependencies:
|
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
|
-
name: pry
|
70
|
+
name: pry-byebug
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
73
|
- - ">="
|
@@ -116,6 +116,7 @@ extensions: []
|
|
116
116
|
extra_rdoc_files: []
|
117
117
|
files:
|
118
118
|
- ".gitignore"
|
119
|
+
- ".pryrc"
|
119
120
|
- ".rspec"
|
120
121
|
- ".travis.yml"
|
121
122
|
- Gemfile
|