json-schema_builder 0.7.1 → 0.8.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 74990c32e6a0f3818690fb0ce66adcb7bb14bd4e
4
- data.tar.gz: 359ba84e515b4c059320954bcfdaaf84de2dfc1b
3
+ metadata.gz: 2945d0d0e0746865b799fd0e2dcb0e1b066c52db
4
+ data.tar.gz: 40da6b42be71e5d59e72115956f9ad98c8950650
5
5
  SHA512:
6
- metadata.gz: 0c146bbba22320150069cec3b4e903ca505748775a001ef412a49aa740c488de989ffc8ee465256f13bf734dd36c4585542774efbffc1824f3f3d795680a84ad
7
- data.tar.gz: f5070d30ab1296356e8297de145c174e8b89e768854a1d1f4dbe73c2dc0ca86dac59141373ff4c84ce435a410c6ebeb47c7668274c9ee80c1ed0a8ba19b1ae7d
6
+ metadata.gz: 5854cbff037513e952a31785d38e5e2e3b029b389dfafeaff23f0bdc1f07a9969861fec5b3ec2b58db7c7737ede5d726045c39a02b157e8feb6ca42cf468a0ef
7
+ data.tar.gz: 01e0e5bb89620531afaf9392e7691ea282e36f31858b54df70cfb938ea64b8d18878262f7c5757b7c2ff001ae2e92f19299f660dba022f4e1c6dca088a2af35f
@@ -13,6 +13,7 @@ module JSON
13
13
  opts = args.extract_options!
14
14
  schema[:items] = args.first
15
15
  schema[:items] ||= items_entity(opts, &block).as_json
16
+ parent.reinitialize if parent
16
17
  end
17
18
 
18
19
  protected
@@ -7,11 +7,14 @@ module JSON
7
7
  def attribute(name, as: nil, array: false)
8
8
  attr = as || snakeize(name)
9
9
  define_method name do |*values|
10
- if array
10
+ result = if array
11
11
  _array_attr attr, values.flatten
12
12
  else
13
13
  _attr attr, values.first
14
14
  end
15
+
16
+ parent.reinitialize if parent
17
+ result
15
18
  end
16
19
  alias_method "#{ name }=", name
17
20
  end
@@ -8,7 +8,9 @@ module JSON
8
8
  opts = args.extract_options!
9
9
  klass, name = klass_and_name_from args
10
10
  set_context_for opts
11
- klass.new name, opts, &block
11
+ klass.new(name, opts, &block).tap do
12
+ reinitialize if is_a?(Entity)
13
+ end
12
14
  end
13
15
 
14
16
  protected
@@ -44,6 +44,21 @@ module JSON
44
44
  initialize_with opts
45
45
  eval_block &block
46
46
  extract_types
47
+ @initialized = true
48
+ end
49
+
50
+ def initialized?
51
+ !!@initialized
52
+ end
53
+
54
+ def reinitialize
55
+ end
56
+
57
+ def extend(child_name, &block)
58
+ children.find { |c| c.name == child_name.to_sym }.tap do |child|
59
+ raise "Property #{child_name} does not exist" unless child
60
+ child.eval_block(&block) if block_given?
61
+ end
47
62
  end
48
63
 
49
64
  def add_fragment(child)
@@ -30,6 +30,11 @@ module JSON
30
30
  super
31
31
  end
32
32
 
33
+ def reinitialize
34
+ return unless initialized?
35
+ extract_types
36
+ end
37
+
33
38
  def required(*values)
34
39
  case values
35
40
  when []
@@ -1,5 +1,5 @@
1
1
  module JSON
2
2
  module SchemaBuilder
3
- VERSION = '0.7.1'
3
+ VERSION = '0.8.0'
4
4
  end
5
5
  end
@@ -0,0 +1,49 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Examples::BuilderInitialization, type: :integration do
4
+ it_behaves_like 'a builder' do
5
+ let(:expected_json) do
6
+ {
7
+ type: :object,
8
+ properties: {
9
+ name: {
10
+ type: :string
11
+ },
12
+ settings: {
13
+ type: :object,
14
+ properties: {
15
+ email: {
16
+ type: :string
17
+ }
18
+ }
19
+ },
20
+ preferences: {
21
+ anyOf: [
22
+ {
23
+ type: :object,
24
+ properties: {
25
+ enabled: {
26
+ type: :boolean,
27
+ default: true
28
+ }
29
+ }
30
+ },
31
+ {
32
+ type: :null
33
+ }
34
+ ]
35
+ },
36
+ ids: {
37
+ type: :array,
38
+ items: {
39
+ anyOf: [
40
+ {type: :number},
41
+ {type: :string}
42
+ ]
43
+ }
44
+ }
45
+ }
46
+ }
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,49 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Examples::BuilderReopening, type: :integration do
4
+ it_behaves_like 'a builder' do
5
+ let(:expected_json) do
6
+ {
7
+ type: :object,
8
+ properties: {
9
+ name: {
10
+ type: :string
11
+ },
12
+ settings: {
13
+ type: :object,
14
+ properties: {
15
+ email: {
16
+ type: :string
17
+ },
18
+ phone_number: {
19
+ type: :string
20
+ }
21
+ }
22
+ },
23
+ preferences: {
24
+ anyOf: [
25
+ {
26
+ type: :object,
27
+ properties: {
28
+ enabled: {
29
+ type: :boolean,
30
+ default: true
31
+ }
32
+ }
33
+ },
34
+ {
35
+ type: :null
36
+ }
37
+ ]
38
+ },
39
+ ids: {
40
+ type: :array,
41
+ items: {
42
+ type: :string,
43
+ }
44
+ }
45
+ }
46
+ }
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,41 @@
1
+ module Examples
2
+ class BuilderInitialization
3
+ include JSON::SchemaBuilder
4
+
5
+ def example
6
+ obj = object
7
+ obj.string :name
8
+ settings_for(obj)
9
+ preferences_for(obj)
10
+ add_ids_to(obj)
11
+ obj
12
+ end
13
+
14
+ def settings_for(obj)
15
+ settings = obj.object :settings
16
+ settings.string :email
17
+ settings
18
+ end
19
+
20
+ def preferences_for(obj)
21
+ preferences = obj.entity :preferences
22
+ preferences.any_of preference
23
+ preferences.any_of null
24
+ end
25
+
26
+ def preference
27
+ obj = object
28
+ enabled = obj.boolean :enabled
29
+ enabled.default = true
30
+ obj
31
+ end
32
+
33
+ def add_ids_to(obj)
34
+ ids = obj.array :ids
35
+ ids.items do
36
+ any_of number
37
+ any_of string
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,13 @@
1
+ module Examples
2
+ class BuilderReopening < BuilderInitialization
3
+ def example
4
+ obj = super
5
+ obj.extend(:settings) do
6
+ string :phone_number
7
+ end
8
+ ids = obj.array :ids
9
+ ids.items type: :string
10
+ obj
11
+ end
12
+ end
13
+ end
@@ -181,4 +181,52 @@ RSpec.describe JSON::SchemaBuilder::Entity, type: :unit do
181
181
  expect(schema.example.fragments.keys).to match_array(%w(#/ #/parent_name))
182
182
  end
183
183
  end
184
+
185
+ describe "#extend" do
186
+ let(:object) do
187
+ obj = JSON::SchemaBuilder::Object.new(nil) do
188
+ string :test
189
+ object :sub_object do
190
+ string :a_string
191
+ end
192
+ end
193
+
194
+ obj.extend(:sub_object) do
195
+ string :another_string
196
+ number :a_number, required: true
197
+ end
198
+
199
+ obj
200
+ end
201
+ let(:schema) { object.schema }
202
+ let(:properties) { schema[:properties] }
203
+ let(:sub_object) { properties[:sub_object] }
204
+ let(:sub_properties) { sub_object[:properties] }
205
+
206
+ it "does not remove properties" do
207
+ expect(properties[:test]).to eq "type" => "string"
208
+ expect(sub_object).to include "type" => "object"
209
+ expect(sub_properties).to include "a_string" => {"type" => "string"}
210
+ end
211
+
212
+ it "adds new properties" do
213
+ expect(sub_properties).to include "another_string" => {"type" => "string"}
214
+ expect(sub_properties).to include "a_number" => {"type" => "number"}
215
+ expect(sub_object[:required]).to match_array ["a_number"]
216
+ end
217
+
218
+ it "raises on unknown properties" do
219
+ expect do
220
+ object.extend(:foo) do
221
+ raise "doesn't work"
222
+ end
223
+ end.to raise_error "Property foo does not exist"
224
+ end
225
+
226
+ it "returns the child" do
227
+ child = object.extend(:sub_object)
228
+ expect(child).to be_a(JSON::SchemaBuilder::Object)
229
+ expect(child.name).to eq(:sub_object)
230
+ end
231
+ end
184
232
  end
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.7.1
4
+ version: 0.8.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: 2018-03-05 00:00:00.000000000 Z
11
+ date: 2018-03-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -203,6 +203,8 @@ files:
203
203
  - lib/json/schema_builder/version.rb
204
204
  - spec/integration/array_any_of_spec.rb
205
205
  - spec/integration/array_definitions_spec.rb
206
+ - spec/integration/builder_initialization_spec.rb
207
+ - spec/integration/builder_reopening_spec.rb
206
208
  - spec/integration/context_delegation_spec.rb
207
209
  - spec/integration/custom_errors_spec.rb
208
210
  - spec/integration/entity_literals_spec.rb
@@ -224,6 +226,8 @@ files:
224
226
  - spec/support/attribute_matcher.rb
225
227
  - spec/support/examples/array_any_of.rb
226
228
  - spec/support/examples/array_definitions.rb
229
+ - spec/support/examples/builder_initialization.rb
230
+ - spec/support/examples/builder_reopening.rb
227
231
  - spec/support/examples/context_delegation.rb
228
232
  - spec/support/examples/custom_errors.rb
229
233
  - spec/support/examples/entity_literals.rb
@@ -281,6 +285,8 @@ summary: Build JSON schemas with Ruby
281
285
  test_files:
282
286
  - spec/integration/array_any_of_spec.rb
283
287
  - spec/integration/array_definitions_spec.rb
288
+ - spec/integration/builder_initialization_spec.rb
289
+ - spec/integration/builder_reopening_spec.rb
284
290
  - spec/integration/context_delegation_spec.rb
285
291
  - spec/integration/custom_errors_spec.rb
286
292
  - spec/integration/entity_literals_spec.rb
@@ -302,6 +308,8 @@ test_files:
302
308
  - spec/support/attribute_matcher.rb
303
309
  - spec/support/examples/array_any_of.rb
304
310
  - spec/support/examples/array_definitions.rb
311
+ - spec/support/examples/builder_initialization.rb
312
+ - spec/support/examples/builder_reopening.rb
305
313
  - spec/support/examples/context_delegation.rb
306
314
  - spec/support/examples/custom_errors.rb
307
315
  - spec/support/examples/entity_literals.rb