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 +4 -4
- data/README.md +1 -1
- data/lib/json_model/properties.rb +6 -0
- data/lib/json_model/schema.rb +16 -2
- 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/schema_spec.rb +15 -0
- data/spec/type_spec/const_spec.rb +1 -1
- 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: 452a7c1ae9d8d3c1df881d768520e439d92dce607b3e4fa609b8cafdce702b70
|
|
4
|
+
data.tar.gz: eba5d91dfd64620a0d64fd82e610837644d8e019fb3d7e4978eed050ea009487
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: e9f23593b4a6d8647d0854be6c50e2e62ea5929571273bbe5229cf1f67f069d38638bd8cd2acc066fb69c513e48da929eed72782ba195bfd3d39f592391e9471
|
|
7
|
+
data.tar.gz: 777dd3fe984c380aabddd1d199e69bbaeafc94a5ebca9e6bf6a889138095133c8bb0ae002efcc944b733123a103b6adf06ab9e82b06d1654c86c9b23d137f2e6
|
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
|
@@ -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
|
-
|
|
33
|
-
|
|
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]
|
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/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
|
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.12
|
|
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
|