json_model_rb 0.1.10 → 0.1.12

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: 98bb614ecc704ac48a1a6d72b079f26eae33b6e6e392c64309ef3dead031cef3
4
- data.tar.gz: cae571a4e4f58b34a1c5add1135ebbe8382dcf588a40fcdca719343addaf66b5
3
+ metadata.gz: 452a7c1ae9d8d3c1df881d768520e439d92dce607b3e4fa609b8cafdce702b70
4
+ data.tar.gz: eba5d91dfd64620a0d64fd82e610837644d8e019fb3d7e4978eed050ea009487
5
5
  SHA512:
6
- metadata.gz: 0c7d8e9859704abbdc88e0659de62c34c586e448288e58063f80a7448a5c0c32ccf374146ed14488a1ee67f6a358e80f162fb4b9cd79a531b479c89bc5d814e0
7
- data.tar.gz: 9a2ca8d4b13ee9ac3ff8fb6e68adca65bdad4a320f2216fe5a277842a1feacf5f5aa0b9f3049ab39658c3f6fa5f833339bfcca967e7782ce9068e9039dde8dfb
6
+ metadata.gz: e9f23593b4a6d8647d0854be6c50e2e62ea5929571273bbe5229cf1f67f069d38638bd8cd2acc066fb69c513e48da929eed72782ba195bfd3d39f592391e9471
7
+ data.tar.gz: 777dd3fe984c380aabddd1d199e69bbaeafc94a5ebca9e6bf6a889138095133c8bb0ae002efcc944b733123a103b6adf06ab9e82b06d1654c86c9b23d137f2e6
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # JSON Model
2
2
 
3
- [![Gem Version](https://badge.fury.io/rb/json_model.svg)](https://badge.fury.io/rb/json_model)
3
+ [![Gem Version](https://badge.fury.io/rb/json_model_rb.svg)](https://badge.fury.io/rb/json_model_rb)
4
4
  [![Ruby](https://github.com/gillesbergerp/json_model_rb/actions/workflows/ci.yml/badge.svg)](https://github.com/gillesbergerp/json_model_rb/actions/workflows/ci.yml)
5
5
  [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
6
6
 
@@ -20,6 +20,11 @@ module JsonModel
20
20
  @properties ||= {}
21
21
  end
22
22
 
23
+ # @return [Hash]
24
+ def aliased_properties
25
+ @aliased_properties ||= {}
26
+ end
27
+
23
28
  # @param [Symbol] name
24
29
  # @param [Object, Class] type
25
30
  # @param [Hash] options
@@ -38,6 +43,7 @@ module JsonModel
38
43
  def add_property(name, type:, **options)
39
44
  property = Property.new(name, type: type, **options)
40
45
  properties[name] = property
46
+ aliased_properties[property.alias] = property
41
47
  define_accessors(property)
42
48
  define_validations(property)
43
49
  end
@@ -29,12 +29,26 @@ 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
 
37
37
  class_methods do
38
+ # @param [::Object] json
39
+ # @return [::Object, nil]
40
+ def from_json(json)
41
+ attributes = json.transform_keys { |key| aliased_properties[key]&.name || raise_unknown_attribute_error(key) }
42
+ new(attributes)
43
+ end
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
+
38
52
  # @param [Symbol] ref_mode
39
53
  # @param [Hash] _options
40
54
  # @return [Hash]
@@ -23,7 +23,7 @@ module JsonModel
23
23
  # @param [::Object] json
24
24
  # @return [::Object, nil]
25
25
  def cast(json)
26
- @type.new(**json)
26
+ @type.from_json(**json)
27
27
  end
28
28
  end
29
29
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JsonModel
4
- VERSION = '0.1.10'
4
+ VERSION = '0.1.12'
5
5
  end
@@ -4,41 +4,45 @@ require('spec_helper')
4
4
 
5
5
  RSpec.describe('User schema') do
6
6
  it('todo') do
7
- base_component = stub_const(
7
+ stub_const(
8
8
  'BaseComponent',
9
9
  Class.new do
10
10
  include(JsonModel::Schema)
11
+
11
12
  property(:type, type: String)
12
13
  property(:styles, type: T::Array[String], optional: true)
13
- end
14
+ end,
14
15
  )
15
- text_component = stub_const(
16
+ stub_const(
16
17
  'TextComponent',
17
18
  Class.new(BaseComponent) do
18
19
  include(JsonModel::Schema)
20
+
19
21
  property(:type, type: T::Enum['text'])
20
22
  property(:value, type: String)
21
- end
23
+ end,
22
24
  )
23
- number_component = stub_const(
25
+ stub_const(
24
26
  'NumberComponent',
25
27
  Class.new(BaseComponent) do
26
28
  include(JsonModel::Schema)
29
+
27
30
  property(:type, type: T::Const['number'])
28
31
  property(:value, type: Integer)
29
- end
32
+ end,
30
33
  )
31
34
 
32
35
  all_component_types = ObjectSpace
33
- .each_object(Class)
34
- .select { |klass| klass.ancestors.include?(BaseComponent) && klass != BaseComponent }
35
- grid_component = stub_const(
36
+ .each_object(Class)
37
+ .select { |klass| klass.ancestors.include?(BaseComponent) && klass != BaseComponent }
38
+ stub_const(
36
39
  'GridComponent',
37
40
  Class.new(BaseComponent) do
38
41
  include(JsonModel::Schema)
42
+
39
43
  property(:type, type: T::Const['grid'])
40
44
  property(:components, type: T::Array[T::OneOf[*all_component_types]], optional: true, ref_mode: JsonModel::RefMode::EXTERNAL)
41
- end
45
+ end,
42
46
  )
43
47
 
44
48
 
@@ -50,4 +54,4 @@ RSpec.describe('User schema') do
50
54
  expect(instance.components.first).to(be_instance_of(TextComponent))
51
55
  expect(instance.components.last).to(be_instance_of(NumberComponent))
52
56
  end
53
- end
57
+ end
data/spec/schema_spec.rb CHANGED
@@ -63,6 +63,21 @@ RSpec.describe(JsonModel::Schema) do
63
63
  end
64
64
  end
65
65
 
66
+ describe('.from_json') do
67
+ let(:klass) do
68
+ Class.new do
69
+ include(JsonModel::Schema)
70
+
71
+ property(:foo_bar, type: String, as: :fooBar)
72
+ end
73
+ end
74
+
75
+ it('can be instantiated from json') do
76
+ instance = klass.from_json({ fooBar: 'baz' })
77
+ expect(instance.foo_bar).to(eq('baz'))
78
+ end
79
+ end
80
+
66
81
  describe('.as_schema') do
67
82
  let(:klass) do
68
83
  Class.new do
@@ -9,7 +9,7 @@ RSpec.describe(JsonModel::TypeSpec::Const) do
9
9
  .to(
10
10
  eq(
11
11
  {
12
- const: 'a'
12
+ const: 'a',
13
13
  },
14
14
  ),
15
15
  )
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_model_rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.10
4
+ version: 0.1.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul Gillesberger
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2025-12-29 00:00:00.000000000 Z
10
+ date: 2025-12-30 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: rake