strong_json 0.3.0 → 0.4.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: a0361a33f682e9ba1f121b0936ae8fe93f319cba
4
- data.tar.gz: c4128c7ed52306c800078de3f5121c2fe9b951a3
3
+ metadata.gz: 11c5dbfb77d22e1c192653aec8a65425fc89037a
4
+ data.tar.gz: 4c0a5335c72ee7dbe8197f88d00bf695d8175255
5
5
  SHA512:
6
- metadata.gz: 255f8a7b8e988dd081cb1ab82d916861b25effdc26bf854acbbaaad0eb3746751cc95756761e0eb8269202fd2ccd3a8160de52bcf99cbcdadd28a06224726cdf
7
- data.tar.gz: 04e267544534ec7043132494c13c7cadba63b0e61f303445714bcf5207e85eabae0675eaababc099a5cdd1aa17f73640a0d5914c9de574badb9a203ca7f7809c
6
+ metadata.gz: 6ad64a761127eafdae6dc349a6afa3940525fac50e7e6300ef8ecb1346b3fef065e9c84646b95853398e137a7c4db183a07bd3ea1c1c28abbd2916ef324144fd
7
+ data.tar.gz: 937d1ef703bc65838c4d02c357ff5d96ee963f40b1fc22f3f3add66d61b127646df023b8700c504de3630385ac08fe19faa572fdf627fca40a3e300f1447704b
data/README.md CHANGED
@@ -30,7 +30,7 @@ s = StrongJSON.new do
30
30
  let :order, object(customer: customer, items: array(item))
31
31
  end
32
32
 
33
- json = s.order.coerce(JSON.parse(input), symbolize_names: true)
33
+ json = s.order.coerce(JSON.parse(input, symbolize_names: true))
34
34
  s.order =~ JSON.parse(input, symbolize_names: true)
35
35
 
36
36
  case JSON.parse(input2, symbolize_names: true)
@@ -57,6 +57,26 @@ If an attribute has a value which does not match with given type, the `coerce` m
57
57
  * Fields, `f1`, `f2`, and ..., must be present and its values must be of `type1`, `type2`, ..., respectively
58
58
  * Objects with other fields will be rejected
59
59
 
60
+ #### Performance hint
61
+
62
+ Object attributes test is done in order of the keys.
63
+
64
+ ```ruby
65
+ slower_object = enum(
66
+ object(id: numeric, created_at: string, updated_at: string, type: literal("person"), name: string),
67
+ object(id: numeric, created_at: string, updated_at: string, type: literal("food"), object: any)
68
+ )
69
+
70
+ faster_object = enum(
71
+ object(type: literal("person"), id: numeric, created_at: string, updated_at: string, name: string),
72
+ object(type: literal("food"), id: numeric, created_at: string, updated_at: string, object: any)
73
+ )
74
+ ```
75
+
76
+ The two enums represents same object, but testing runs faster with `faster_object`.
77
+ Objects in `faster_object` have `type` attribute as their first keys.
78
+ Testing `type` is done first, and it soon determines if the object is `"person"` or `"food"`.
79
+
60
80
  ### array(type)
61
81
 
62
82
  * The value must be an array
@@ -139,10 +139,14 @@ class StrongJSON
139
139
 
140
140
  result = {}
141
141
 
142
- all_keys = (@fields.keys + object.keys).sort.uniq
143
- all_keys.each do |key|
144
- type = @fields.has_key?(key) ? @fields[key] : NONE
145
- value = object.has_key?(key) ? object[key] : NONE
142
+ object.each do |key, value|
143
+ unless @fields.key?(key)
144
+ raise UnexpectedFieldError.new(path: path + [key], value: value)
145
+ end
146
+ end
147
+
148
+ @fields.each do |key, type|
149
+ value = object.key?(key) ? object[key] : NONE
146
150
 
147
151
  test_value_type(path + [key], type, value) do |v|
148
152
  result[key] = v
@@ -153,10 +157,6 @@ class StrongJSON
153
157
  end
154
158
 
155
159
  def test_value_type(path, type, value)
156
- if NONE.equal?(type) && !NONE.equal?(value)
157
- raise UnexpectedFieldError.new(path: path, value: value)
158
- end
159
-
160
160
  v = type.coerce(value, path: path)
161
161
 
162
162
  return if NONE.equal?(v) || NONE.equal?(type)
@@ -1,3 +1,3 @@
1
1
  class StrongJSON
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
@@ -30,7 +30,7 @@ describe StrongJSON::Type::Object do
30
30
  it "rejects objects with missing fields" do
31
31
  type = StrongJSON::Type::Object.new(a: StrongJSON::Type::Base.new(:numeric))
32
32
 
33
- expect{ type.coerce(b: "test") }.to raise_error(StrongJSON::Type::Error)
33
+ expect{ type.coerce(b: "test") }.to raise_error(StrongJSON::Type::UnexpectedFieldError)
34
34
  end
35
35
  end
36
36
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: strong_json
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Soutaro Matsumoto
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-05-08 00:00:00.000000000 Z
11
+ date: 2017-05-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler