json_model_rb 0.1.12 → 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: 452a7c1ae9d8d3c1df881d768520e439d92dce607b3e4fa609b8cafdce702b70
4
- data.tar.gz: eba5d91dfd64620a0d64fd82e610837644d8e019fb3d7e4978eed050ea009487
3
+ metadata.gz: e4f530abe8264be24ec3b62c627bbab78fe3d25ace6bbd03a9360d7bbac688f2
4
+ data.tar.gz: e1997308062e1cd0b3af670b381bd461324f0c29e49457534cd1196048f57c47
5
5
  SHA512:
6
- metadata.gz: e9f23593b4a6d8647d0854be6c50e2e62ea5929571273bbe5229cf1f67f069d38638bd8cd2acc066fb69c513e48da929eed72782ba195bfd3d39f592391e9471
7
- data.tar.gz: 777dd3fe984c380aabddd1d199e69bbaeafc94a5ebca9e6bf6a889138095133c8bb0ae002efcc944b733123a103b6adf06ab9e82b06d1654c86c9b23d137f2e6
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
@@ -38,7 +38,7 @@ 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 || raise_unknown_attribute_error(key) }
41
+ attributes = json.transform_keys { |key| invert_alias(key) || raise_unknown_attribute_error(key) }
42
42
  new(attributes)
43
43
  end
44
44
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JsonModel
4
- VERSION = '0.1.12'
4
+ VERSION = '0.1.13'
5
5
  end
data/spec/schema_spec.rb CHANGED
@@ -64,16 +64,29 @@ RSpec.describe(JsonModel::Schema) do
64
64
  end
65
65
 
66
66
  describe('.from_json') do
67
- let(:klass) do
68
- Class.new do
69
- include(JsonModel::Schema)
67
+ before do
68
+ stub_const(
69
+ 'Foo',
70
+ Class.new do
71
+ include(JsonModel::Schema)
70
72
 
71
- property(:foo_bar, type: String, as: :fooBar)
72
- end
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'))
73
86
  end
74
87
 
75
- it('can be instantiated from json') do
76
- instance = klass.from_json({ fooBar: 'baz' })
88
+ it('child can be instantiated from json') do
89
+ instance = Bar.from_json({ fooBar: 'baz' })
77
90
  expect(instance.foo_bar).to(eq('baz'))
78
91
  end
79
92
  end
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.12
4
+ version: 0.1.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul Gillesberger