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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5c8abfd3efdffc114b9968dc02c151d4f79cd49bb87aff04553641c284814f12
4
- data.tar.gz: 34ccde981e5e99ff2df38e89bb25a240c4036782dc00b6af930121884894d813
3
+ metadata.gz: e4f530abe8264be24ec3b62c627bbab78fe3d25ace6bbd03a9360d7bbac688f2
4
+ data.tar.gz: e1997308062e1cd0b3af670b381bd461324f0c29e49457534cd1196048f57c47
5
5
  SHA512:
6
- metadata.gz: 9918e40ee08f267dcef942ea23ef74c8f2006325b83c845f966212d946de25f41f7d8c5bdace36ced950ba624c24c9eaf94e16701b8b51b50858a3eed94ca705
7
- data.tar.gz: b9624a3dc52e2a1b902629233b11953393052f9242a9da5182361932a4eda86665e337f433bb1ec2f804e32c58e6d5a83779a9193a8d6b8beed3ad034b5f8823
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
@@ -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
- elsif !self.class.additional_properties && JsonModel.config.validate_after_instantiation
33
- raise(Errors::UnknownAttributeError.new(self.class, name))
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| aliased_properties[key]&.name || 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]
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JsonModel
4
- VERSION = '0.1.11'
4
+ VERSION = '0.1.13'
5
5
  end
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_model_rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.11
4
+ version: 0.1.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul Gillesberger