json_model_rb 0.1.11 → 0.1.13
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 +4 -4
- data/lib/json_model/properties.rb +7 -0
- data/lib/json_model/schema.rb +10 -3
- data/lib/json_model/version.rb +1 -1
- data/spec/schema_spec.rb +28 -0
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: e4f530abe8264be24ec3b62c627bbab78fe3d25ace6bbd03a9360d7bbac688f2
|
|
4
|
+
data.tar.gz: e1997308062e1cd0b3af670b381bd461324f0c29e49457534cd1196048f57c47
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: aa7626727be77b8da34aa0775f2c8ba3f1f2e636dfca50b83224e51c92e7d78c70079a2732fb0ee17ded82f295d1d2f0654e274b94ceff224b55c36aa99bff88
|
|
7
|
+
data.tar.gz: 8bcd5c1fee253effded868ff5466f00969eebd6e50ae6c5ace3dce2bb5befc6ea6be3730bfd3ca9806bf5380267591df23dc679d97bb2751f227a9ef819669bc
|
|
@@ -25,6 +25,13 @@ module JsonModel
|
|
|
25
25
|
@aliased_properties ||= {}
|
|
26
26
|
end
|
|
27
27
|
|
|
28
|
+
# @param [Symbol] name
|
|
29
|
+
# @return [Symbol, nil]
|
|
30
|
+
def invert_alias(name)
|
|
31
|
+
aliased_properties[name]&.name || superclass&.invert_alias(name)
|
|
32
|
+
rescue NoMethodError
|
|
33
|
+
end
|
|
34
|
+
|
|
28
35
|
# @param [Symbol] name
|
|
29
36
|
# @param [Object, Class] type
|
|
30
37
|
# @param [Hash] options
|
data/lib/json_model/schema.rb
CHANGED
|
@@ -29,8 +29,8 @@ module JsonModel
|
|
|
29
29
|
def assign_attribute(name, value)
|
|
30
30
|
if respond_to?("#{name}=")
|
|
31
31
|
send("#{name}=", value)
|
|
32
|
-
|
|
33
|
-
|
|
32
|
+
else
|
|
33
|
+
self.class.raise_unknown_attribute_error(name)
|
|
34
34
|
end
|
|
35
35
|
end
|
|
36
36
|
|
|
@@ -38,10 +38,17 @@ module JsonModel
|
|
|
38
38
|
# @param [::Object] json
|
|
39
39
|
# @return [::Object, nil]
|
|
40
40
|
def from_json(json)
|
|
41
|
-
attributes = json.transform_keys { |key|
|
|
41
|
+
attributes = json.transform_keys { |key| invert_alias(key) || raise_unknown_attribute_error(key) }
|
|
42
42
|
new(attributes)
|
|
43
43
|
end
|
|
44
44
|
|
|
45
|
+
# @param [Symbol] name
|
|
46
|
+
def raise_unknown_attribute_error(name)
|
|
47
|
+
if !additional_properties && JsonModel.config.validate_after_instantiation
|
|
48
|
+
raise(Errors::UnknownAttributeError.new(self, name))
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
45
52
|
# @param [Symbol] ref_mode
|
|
46
53
|
# @param [Hash] _options
|
|
47
54
|
# @return [Hash]
|
data/lib/json_model/version.rb
CHANGED
data/spec/schema_spec.rb
CHANGED
|
@@ -63,6 +63,34 @@ RSpec.describe(JsonModel::Schema) do
|
|
|
63
63
|
end
|
|
64
64
|
end
|
|
65
65
|
|
|
66
|
+
describe('.from_json') do
|
|
67
|
+
before do
|
|
68
|
+
stub_const(
|
|
69
|
+
'Foo',
|
|
70
|
+
Class.new do
|
|
71
|
+
include(JsonModel::Schema)
|
|
72
|
+
|
|
73
|
+
property(:foo_bar, type: String, as: :fooBar)
|
|
74
|
+
end,
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
stub_const(
|
|
78
|
+
'Bar',
|
|
79
|
+
Class.new(Foo) {},
|
|
80
|
+
)
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
it('parent can be instantiated from json') do
|
|
84
|
+
instance = Foo.from_json({ fooBar: 'baz' })
|
|
85
|
+
expect(instance.foo_bar).to(eq('baz'))
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
it('child can be instantiated from json') do
|
|
89
|
+
instance = Bar.from_json({ fooBar: 'baz' })
|
|
90
|
+
expect(instance.foo_bar).to(eq('baz'))
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
|
|
66
94
|
describe('.as_schema') do
|
|
67
95
|
let(:klass) do
|
|
68
96
|
Class.new do
|