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 +4 -4
- data/README.md +1 -1
- data/lib/json_model/properties.rb +6 -0
- data/lib/json_model/schema.rb +7 -0
- data/lib/json_model/type_spec/const.rb +1 -1
- data/lib/json_model/type_spec/enum.rb +1 -1
- data/lib/json_model/type_spec/object.rb +1 -1
- data/lib/json_model/version.rb +1 -1
- data/spec/examples/component_spec.rb +15 -11
- data/spec/type_spec/const_spec.rb +1 -1
- data/spec/type_spec/enum_spec.rb +1 -10
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 5c8abfd3efdffc114b9968dc02c151d4f79cd49bb87aff04553641c284814f12
|
|
4
|
+
data.tar.gz: 34ccde981e5e99ff2df38e89bb25a240c4036782dc00b6af930121884894d813
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 9918e40ee08f267dcef942ea23ef74c8f2006325b83c845f966212d946de25f41f7d8c5bdace36ced950ba624c24c9eaf94e16701b8b51b50858a3eed94ca705
|
|
7
|
+
data.tar.gz: b9624a3dc52e2a1b902629233b11953393052f9242a9da5182361932a4eda86665e337f433bb1ec2f804e32c58e6d5a83779a9193a8d6b8beed3ad034b5f8823
|
data/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# JSON Model
|
|
2
2
|
|
|
3
|
-
[](https://badge.fury.io/rb/json_model_rb)
|
|
4
4
|
[](https://github.com/gillesbergerp/json_model_rb/actions/workflows/ci.yml)
|
|
5
5
|
[](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
|
data/lib/json_model/schema.rb
CHANGED
|
@@ -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]
|
data/lib/json_model/version.rb
CHANGED
|
@@ -4,41 +4,45 @@ require('spec_helper')
|
|
|
4
4
|
|
|
5
5
|
RSpec.describe('User schema') do
|
|
6
6
|
it('todo') do
|
|
7
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
34
|
-
|
|
35
|
-
|
|
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/type_spec/enum_spec.rb
CHANGED
|
@@ -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:
|
|
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.
|
|
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-
|
|
10
|
+
date: 2025-12-30 00:00:00.000000000 Z
|
|
11
11
|
dependencies:
|
|
12
12
|
- !ruby/object:Gem::Dependency
|
|
13
13
|
name: rake
|