blumquist 0.6.0 → 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: 11ba5173f0cf7e4d511587c0183b98577fbec9f7
4
- data.tar.gz: 41c86c469189421fa8f6586897328e3fca28309f
3
+ metadata.gz: f4879db47364122779e5e676bdf8856a3ab262e3
4
+ data.tar.gz: 0e7e11fe567658c3faf6f3818ab61a18bc84f5f6
5
5
  SHA512:
6
- metadata.gz: 530d51062112f7f9c36de9c6bdd1371f27b0af5a887b2ba1c389b63f70e051f97e1a8358fb171429ab1f26e2dd200db1075b3e4e80ab5e01a0f97205b0945431
7
- data.tar.gz: 9273ce2f23bbee2a5290ef98bd15c4070e693cbcc75d8e6063d8351fb5a3022ea3e28822c8e3d581edf269c8889910865312deddba4917c7e0d92de30fcc82b8
6
+ metadata.gz: f4ead88bf98e00094da661c9ae1ec0b4e042c8f382d32c6147d4e30184a49bd917ecc5b7c4735a2e23e446819c23f4eae50868db0ef1986e3a02a177ecd68871
7
+ data.tar.gz: 20c8f94af73909b36aa723b67070b9ce340ecbb3c9aca8c43484d586b7d3211f942bb08095f81a80a1c8eaa8899162d5df3a1c863df0b7e4749c01efbe224f9b
@@ -1,3 +1,6 @@
1
+ # 0.7.0
2
+ - Add object equality comparison
3
+
1
4
  # 0.6.0
2
5
  - Add marshalling support
3
6
 
@@ -5,10 +5,13 @@ require 'json-schema'
5
5
  require 'blumquist/errors'
6
6
 
7
7
  class Blumquist
8
+ attr_reader :_type
9
+
8
10
  def initialize(options)
9
11
  # Poor man's deep clone: json 🆗 🆒
10
12
  @data = JSON.parse(options.fetch(:data).to_json)
11
13
  @schema = options.fetch(:schema).with_indifferent_access
14
+ @original_properties = options.fetch(:schema).with_indifferent_access[:properties]
12
15
  @validate = options.fetch(:validate, true)
13
16
 
14
17
  validate_schema
@@ -30,6 +33,12 @@ class Blumquist
30
33
  @schema, @data, @validate = array
31
34
  end
32
35
 
36
+ def ==(other)
37
+ self.class == other.class && other.marshal_dump == marshal_dump
38
+ end
39
+
40
+ alias_method :eql?, :==
41
+
33
42
  private
34
43
 
35
44
  def validate_data
@@ -51,6 +60,13 @@ class Blumquist
51
60
  end
52
61
  end
53
62
 
63
+ def resolve_json_pointer(type_def)
64
+ key = type_def[:$ref].split('/').last
65
+ definition = @schema[:definitions][key]
66
+ raise(Errors::InvalidPointer, pointer) unless definition
67
+ type_def.merge(definition)
68
+ end
69
+
54
70
  def resolve_json_pointer!(type_def)
55
71
  # Should read up on how json pointers are really resolved
56
72
  pointer = type_def.delete(:$ref)
@@ -120,7 +136,10 @@ class Blumquist
120
136
  definitions: @schema[:definitions]
121
137
  )
122
138
  data = @data[property]
123
- blumquistify_object(schema: sub_schema, data: data)
139
+ sub_blumquist = blumquistify_object(schema: sub_schema, data: data)
140
+ # In case of oneOf the definition was already set
141
+ sub_blumquist._type = type_name_for(property) if sub_blumquist && sub_blumquist._type.nil?
142
+ sub_blumquist
124
143
  end
125
144
 
126
145
  def blumquistify_object(options)
@@ -183,11 +202,13 @@ class Blumquist
183
202
  if one[:type]
184
203
  schema = one.merge(definitions: @schema[:definitions])
185
204
  else
186
- schema = resolve_json_pointer!(one).merge(
205
+ schema = resolve_json_pointer(one).merge(
187
206
  definitions: @schema[:definitions]
188
207
  )
189
208
  end
190
- return Blumquist.new(data: data, schema: schema, validate: true)
209
+ sub_blumquist = Blumquist.new(data: data, schema: schema, validate: true)
210
+ sub_blumquist._type = type_from_type_def(one)
211
+ return sub_blumquist
191
212
  end
192
213
  rescue
193
214
  # On to the next oneOf
@@ -213,6 +234,11 @@ class Blumquist
213
234
  raise(Errors::MissingProperties, sub_schema)
214
235
  end
215
236
 
237
+ def type_from_type_def(type_def)
238
+ return 'object' unless type_def.is_a?(Hash) && type_def.has_key?(:$ref)
239
+ type_def[:$ref].split("/").last
240
+ end
241
+
216
242
  def blumquistify_array(property)
217
243
  # We only support arrays with one type defined, either through
218
244
  #
@@ -233,6 +259,7 @@ class Blumquist
233
259
 
234
260
  # The items of this array are defined by a pointer
235
261
  if type_def[:$ref]
262
+ reference_type = type_from_type_def(type_def)
236
263
  item_schema = resolve_json_pointer!(type_def)
237
264
 
238
265
  sub_schema = item_schema.merge(
@@ -241,7 +268,9 @@ class Blumquist
241
268
 
242
269
  @data[property] ||= []
243
270
  @data[property] = @data[property].map do |item|
244
- Blumquist.new(schema: sub_schema, data: item, validate: false)
271
+ sub_blumquist = Blumquist.new(schema: sub_schema, data: item, validate: false)
272
+ sub_blumquist._type = reference_type
273
+ sub_blumquist
245
274
  end
246
275
 
247
276
  # The items are objects, defined directly or through oneOf
@@ -267,8 +296,18 @@ class Blumquist
267
296
  end
268
297
  end
269
298
 
299
+ def type_name_for(property)
300
+ type_from_type_def(@original_properties[property])
301
+ end
302
+
270
303
  def all_primitive_types(types)
271
304
  return false unless types.is_a?(Array)
272
305
  types.all? { |t| primitive_type?(t) }
273
306
  end
307
+
308
+ protected
309
+
310
+ def _type=(_type)
311
+ @_type = _type
312
+ end
274
313
  end
@@ -1,3 +1,3 @@
1
1
  class Blumquist
2
- VERSION = "0.6.0"
2
+ VERSION = "0.7.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: blumquist
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jannis Hermanns
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-10-29 00:00:00.000000000 Z
11
+ date: 2017-02-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -214,7 +214,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
214
214
  version: '0'
215
215
  requirements: []
216
216
  rubyforge_project:
217
- rubygems_version: 2.6.7
217
+ rubygems_version: 2.5.1
218
218
  signing_key:
219
219
  specification_version: 4
220
220
  summary: Turn some data and a json schema into an immutable object with getters from