json_model_rb 0.1.9 → 0.1.11

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: 49c30126ff23f7ec073ef981be5682bb6db7976e4d3f7fde6e5ad5c26b3fc7d8
4
- data.tar.gz: 88e4abe3490277fc63c1f3b4d612dc398cb68e982c1038d00cf75fce7bff2759
3
+ metadata.gz: 5c8abfd3efdffc114b9968dc02c151d4f79cd49bb87aff04553641c284814f12
4
+ data.tar.gz: 34ccde981e5e99ff2df38e89bb25a240c4036782dc00b6af930121884894d813
5
5
  SHA512:
6
- metadata.gz: 782eebf325932cd06bccf71c410e5a820868bf07989372da9b6329b8e3e2eaab68fea6c16e4d2234279d57cee3c831871c786507618914683b30ce57aec0bf37
7
- data.tar.gz: 235dce29d095bab73e393c39b6912ffb1de547a52de845a4a3c309a14b7584baa816d5f71584a4ead8a1c2b64f5210956541e747ef059774a5c721c34647bcc1
6
+ metadata.gz: 9918e40ee08f267dcef942ea23ef74c8f2006325b83c845f966212d946de25f41f7d8c5bdace36ced950ba624c24c9eaf94e16701b8b51b50858a3eed94ca705
7
+ data.tar.gz: b9624a3dc52e2a1b902629233b11953393052f9242a9da5182361932a4eda86665e337f433bb1ec2f804e32c58e6d5a83779a9193a8d6b8beed3ad034b5f8823
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
@@ -35,6 +35,13 @@ module JsonModel
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 || key }
42
+ new(attributes)
43
+ end
44
+
38
45
  # @param [Symbol] ref_mode
39
46
  # @param [Hash] _options
40
47
  # @return [Hash]
@@ -26,7 +26,7 @@ module JsonModel
26
26
  def register_validations(name, klass)
27
27
  super
28
28
 
29
- klass.validates(name, inclusion: { in: @value })
29
+ klass.validates(name, inclusion: { in: @value }, allow_nil: true)
30
30
  end
31
31
  end
32
32
  end
@@ -26,7 +26,7 @@ module JsonModel
26
26
  def register_validations(name, klass)
27
27
  super
28
28
 
29
- klass.validates(name, inclusion: { in: @values })
29
+ klass.validates(name, inclusion: { in: @values }, allow_nil: true)
30
30
  end
31
31
  end
32
32
  end
@@ -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.9'
4
+ VERSION = '0.1.11'
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
@@ -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
  )
@@ -34,7 +34,7 @@ RSpec.describe(JsonModel::TypeSpec::Enum) do
34
34
  described_class
35
35
  .new(1)
36
36
  .register_validations(:foo, klass)
37
- instance = klass.new(foo: nil)
37
+ instance = klass.new(foo: 2)
38
38
 
39
39
  expect(instance.valid?)
40
40
  .to(be(false))
@@ -62,15 +62,6 @@ RSpec.describe(JsonModel::TypeSpec::Enum) do
62
62
  .register_validations(:foo, klass)
63
63
  end
64
64
 
65
- it('fails the validation if nil') do
66
- instance = klass.new(foo: nil)
67
-
68
- expect(instance.valid?)
69
- .to(be(false))
70
- expect(instance.errors.map(&:type))
71
- .to(eq(%i(inclusion)))
72
- end
73
-
74
65
  it('fails the validation if not allowed') do
75
66
  instance = klass.new(foo: 0)
76
67
 
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.9
4
+ version: 0.1.11
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