blumquist 0.1.1 → 0.2.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: 483e9c180b39596478efde4ee3979745358b3c49
4
- data.tar.gz: a87b488598a2be1322c3cadfff04646426dc0cf3
3
+ metadata.gz: be0386ec7a441132a1b6c5f6a230341218c357bc
4
+ data.tar.gz: d744092db33a8cd3f753645f99599f428291b5ee
5
5
  SHA512:
6
- metadata.gz: f9d25548bf324c88a7b909ec2ee416f28b3ce5c61d0ae20733ca87a89e7f8e416f274edc85b6ad967f259db5b6f22ff938539206901fadd15c67eb3032f244f4
7
- data.tar.gz: e2235e061456a355abbc856fa1f552906e56177ef72491a5e074f4a790ef3422dc5f2b3d6bd8fb3021c86e1a6ded9f2a795365f038b2b868e2abe41c18f2b8da
6
+ metadata.gz: eab80f79b5a23c652f00291243bb79db5b9f4b5454d5f32fd6b8344f97ea52003e0c2d9a97171f54d29418d5e7a1b6815db838eb0be31cce8d3547d73c8fb135
7
+ data.tar.gz: 0f346735db4c91f040a1ec6f39e245000293404a643b6ec7153be20353d73ba3b39a8a16e64a9d9bff99b8dcb7883b66de1167e7da63b997312453e3b34e7b32
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ # 0.2.0 (Oct-30-15)
2
+ - validate objects (if desired)
3
+ - use keyword arguments (in <2.0 compat mode)
data/README.md CHANGED
@@ -68,19 +68,22 @@ data = {
68
68
  ... an object with getters for all properties defined in the schema
69
69
 
70
70
  ```ruby
71
- > b = Blumquist.new(schema, data)
72
- => #<Blumquist:0x....>
71
+ > b = Blumquist.new(schema: schema, data: data)
72
+ => #<Blumquist:0x0....>
73
73
  > b.name
74
74
  => "Moviepilot, Inc."
75
75
  > b.old_addresses.first.street
76
76
  => "Blücherstr. 22"
77
77
  ```
78
78
 
79
- ## TODO
79
+ ### Validation
80
80
 
81
- - Put it to use, fix bugs
82
- - Validate data against schema and reject invalid data
81
+ By default, Blumquist will validate the data. If you don't want that to happen, do as follows:
83
82
 
83
+ ```ruby
84
+ > b = Blumquist.new(schema: schema, data: data, validate: false)
85
+ => ...
86
+ ```
84
87
 
85
88
  ## Installation
86
89
 
data/blumquist.gemspec CHANGED
@@ -20,6 +20,7 @@ Gem::Specification.new do |spec|
20
20
  spec.require_paths = ["lib"]
21
21
 
22
22
  spec.add_runtime_dependency "activesupport"
23
+ spec.add_runtime_dependency "json-schema"
23
24
 
24
25
  spec.add_development_dependency "bundler", "~> 1"
25
26
  spec.add_development_dependency "rake", "~> 10.0"
data/lib/blumquist.rb CHANGED
@@ -1,27 +1,33 @@
1
1
  require "blumquist/version"
2
2
  require 'active_support/core_ext/hash/indifferent_access'
3
3
  require 'json'
4
+ require 'json-schema'
4
5
 
5
6
  class Blumquist
6
- def initialize(schema, data)
7
+ def initialize(options)
7
8
  # Poor man's deep clone: json 🆗 🆒
8
- @data = JSON.parse(data.to_json)
9
- @schema = schema.with_indifferent_access
9
+ @data = JSON.parse(options.fetch(:data).to_json)
10
+ @schema = options.fetch(:schema).with_indifferent_access
11
+ @validate = options.fetch(:validate, true)
10
12
 
11
13
  validate_schema
14
+ validate_data
15
+
12
16
  resolve_json_pointers
13
17
  define_getters
14
18
  end
15
19
 
16
20
  private
17
21
 
22
+ def validate_data
23
+ return unless @validate
24
+ JSON::Validator.validate!(@schema, @data)
25
+ end
26
+
18
27
  def validate_schema
19
28
  if @schema[:type] != 'object'
20
29
  raise "Can only deal with 'object' types, not '#{@schema[:type]}'"
21
30
  end
22
- unless @schema[:properties].is_a?(Hash)
23
- raise "Properties are a #{@schema[:properties].class.name}, not a Hash"
24
- end
25
31
  end
26
32
 
27
33
  def resolve_json_pointers
@@ -82,7 +88,7 @@ class Blumquist
82
88
  sub_schema = @schema[:properties][property].merge(
83
89
  definitions: @schema[:definitions]
84
90
  )
85
- @data[property] = Blumquist.new(sub_schema, @data[property])
91
+ @data[property] = Blumquist.new(schema: sub_schema, data: @data[property], validate: @validate)
86
92
  end
87
93
 
88
94
  def blumquistify_array(property)
@@ -92,7 +98,7 @@ class Blumquist
92
98
  )
93
99
  @data[property] ||= []
94
100
  @data[property] = @data[property].map do |item|
95
- Blumquist.new(sub_schema, item)
101
+ Blumquist.new(schema: sub_schema, data: item, validate: @validate)
96
102
  end
97
103
  end
98
104
  end
@@ -1,3 +1,3 @@
1
1
  class Blumquist
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: blumquist
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jannis Hermanns
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: json-schema
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: bundler
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -145,6 +159,7 @@ extra_rdoc_files: []
145
159
  files:
146
160
  - ".gitignore"
147
161
  - ".rspec"
162
+ - CHANGELOG.md
148
163
  - CODE_OF_CONDUCT.md
149
164
  - Gemfile
150
165
  - Guardfile