json-schema_builder 0.6.1 → 0.7.0

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: 6b2db2f9c08d0d7f20bc942b2b8f80692dcb6606
4
- data.tar.gz: 4aaa1d7fc0179c8c8f8d8dc5b3e20165f27b919b
3
+ metadata.gz: 7b51dc48c6af5bbc967ad1a806b0bcc62f2cc042
4
+ data.tar.gz: 541ffe361c94466b483d7836df3c3afd1546d813
5
5
  SHA512:
6
- metadata.gz: 3989f1a40c2f42e11540011f74f6edbfa23595868dea68b985c90f349178607f6ebe1f1a1526e156cf5c8a3de3aa000a7858a67793b434cb1b4518e251bf484a
7
- data.tar.gz: 4ab4fe02482693de7c9628e29f80258c315ddc37a886ac70489dc2e8dfe355c9bec275286b02c015a2fc8d6a49cd6e3aed20be903288f1ae5112168058cc3912
6
+ metadata.gz: 410edbc4fb0e6ca11923a2db72002c3c3147f6f5819abab96898344c92394639a68acb87796c1204ba5abf2d56cb1432b5e46fb8a1754f643c9550b704774744
7
+ data.tar.gz: bfb69c49836656cd6a671c5a5fa9021f5cfc9ca16e3601bf6a9dfce48645fbf2569b24749e2b7e83ed970f5fda15978d16e069e8ce629e33fb2ca7157cca349c
@@ -30,6 +30,7 @@ module JSON
30
30
  self.schema[attr] || []
31
31
  else
32
32
  self.schema[attr] ||= []
33
+ _rename_array_values!(values)
33
34
  self.schema[attr] += values
34
35
  self.schema[attr].uniq!
35
36
  self.schema[attr]
@@ -43,6 +44,15 @@ module JSON
43
44
  self.schema[attr] = value
44
45
  end
45
46
  end
47
+
48
+ def _rename_array_values!(values)
49
+ values.each do |value|
50
+ if value.class < Entity && value.name
51
+ value.name = nil
52
+ value.reset_fragment
53
+ end
54
+ end
55
+ end
46
56
  end
47
57
  end
48
58
  end
@@ -27,6 +27,13 @@ module JSON
27
27
  attribute :ref, as: :$ref
28
28
  attribute :definitions
29
29
 
30
+ def self.disable_attributes!(*attributes)
31
+ attributes.each do |attr|
32
+ undef_method attr rescue NameError
33
+ undef_method "#{attr}=" rescue NameError
34
+ end
35
+ end
36
+
30
37
  def initialize(name, opts = { }, &block)
31
38
  @name = name
32
39
  @children = []
@@ -41,7 +48,13 @@ module JSON
41
48
 
42
49
  def add_fragment(child)
43
50
  @fragments[child.fragment] << child
44
- @parent.add_fragment(child) if @parent
51
+ parent.add_fragment(child) if @parent
52
+ end
53
+
54
+ def reset_fragment
55
+ @fragment = [@parent.fragment, name].compact.join("/").gsub(%r(//), "/")
56
+ root._reset_fragments
57
+ root.fragments["#/"] << root
45
58
  end
46
59
 
47
60
  def schema
@@ -67,6 +80,10 @@ module JSON
67
80
  schema.as_json
68
81
  end
69
82
 
83
+ def inspect
84
+ "#<#{self.class.name}:#{object_id} @schema=#{schema.as_json}>"
85
+ end
86
+
70
87
  def respond_to?(method_name, include_all = false)
71
88
  if @parent_context
72
89
  @parent_context.respond_to? method_name, include_all
@@ -85,6 +102,25 @@ module JSON
85
102
 
86
103
  protected
87
104
 
105
+ def root
106
+ return @root if @root
107
+ node = self
108
+ node = node.parent while node.parent
109
+ @root = node
110
+ end
111
+
112
+ def _reset_fragments
113
+ @fragments = Hash.new { |hash, key| hash[key] = ::Array.new }
114
+ @fragment = if parent
115
+ [@parent.fragment, name].compact.join("/").gsub(%r(//), "/")
116
+ else
117
+ "#/"
118
+ end
119
+
120
+ children.each { |child| child._reset_fragments }
121
+ parent.add_fragment(self) if parent
122
+ end
123
+
88
124
  def extract_types
89
125
  any_of(null) if @nullable
90
126
  if any_of.present?
@@ -1,5 +1,5 @@
1
1
  module JSON
2
2
  module SchemaBuilder
3
- VERSION = '0.6.1'
3
+ VERSION = '0.7.0'
4
4
  end
5
5
  end
@@ -38,7 +38,7 @@ module Examples
38
38
  object do
39
39
  entity :user do
40
40
  any_of [
41
- object { object(:settings) { string :name, error: "Nested" } },
41
+ object(:ignored) { object(:settings) { string :name, error: "Nested" } },
42
42
  null
43
43
  ]
44
44
  end
@@ -15,6 +15,24 @@ RSpec.describe JSON::SchemaBuilder::Entity, type: :unit do
15
15
  it{ is_expected.to define_attribute :ref }
16
16
  it{ is_expected.to define_attribute :definitions }
17
17
 
18
+ describe ".disable_attributes!" do
19
+ let(:disabled_attributes) { [:default, :ref, :definitions] }
20
+
21
+ around(:each) do |example|
22
+ described_class.disable_attributes! *disabled_attributes
23
+ example.call
24
+ disabled_attributes.each { |attr| described_class.attribute(attr) }
25
+ end
26
+
27
+ it "should remove the attribute methods" do
28
+ entity = described_class.new "test"
29
+ disabled_attributes.each do |attr|
30
+ expect(entity).to_not respond_to(attr)
31
+ expect(entity).to_not respond_to("#{attr}=")
32
+ end
33
+ end
34
+ end
35
+
18
36
  describe '.attribute' do
19
37
  include_context 'an entity'
20
38
 
@@ -140,4 +158,27 @@ RSpec.describe JSON::SchemaBuilder::Entity, type: :unit do
140
158
  subject.as_json
141
159
  end
142
160
  end
161
+
162
+ describe "array attributes" do
163
+ let(:klass) do
164
+ Class.new do
165
+ include JSON::SchemaBuilder
166
+
167
+ def example
168
+ object do
169
+ object :parent_name do
170
+ any_of [
171
+ string(:name)
172
+ ]
173
+ end
174
+ end
175
+ end
176
+ end
177
+ end
178
+ subject(:schema){ klass.new }
179
+
180
+ it "prevents elements from being named" do
181
+ expect(schema.example.fragments.keys).to match_array(%w(#/ #/parent_name))
182
+ end
183
+ end
143
184
  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.6.1
4
+ version: 0.7.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-02-01 00:00:00.000000000 Z
11
+ date: 2018-02-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler