jat 0.0.6 → 0.0.7
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/lib/jat/config.rb +4 -7
- data/lib/jat/plugins/json_api/lib/response.rb +1 -5
- data/lib/jat/plugins/json_api/lib/response_piece.rb +7 -7
- data/lib/jat/plugins/simple_api/lib/response.rb +1 -5
- data/lib/jat/plugins/simple_api/lib/response_piece.rb +1 -5
- data/lib/jat/plugins/types/types.rb +54 -0
- data/test/lib/jat/plugins/types/types_test.rb +79 -0
- metadata +7 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e4b14981546d524f665e1d268857bd8b2caafec9af07157a1f57fd2ca8f9c83c
|
4
|
+
data.tar.gz: 1335c436d0e4ae1f70fb70a9ef5757610e09ce3b5a03b87ba356d31625af0ab3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fcaccbe3cb5e5a27cd5700e3677886fb9966ef8ce2775bf2be6638578489747b8a488acbd3f6f7f55752323c40bc92410af1d079dfeefe2d4b41d4f768fbfbfb
|
7
|
+
data.tar.gz: 23a2cbda699f9e8f2a9588dc8cfb4350bbd1a1994603577bd3649a356148ad88d2bd754529bb63905aba59a993cc747c2e5ad762038fb571d5d83b1ecc6d40b4
|
data/lib/jat/config.rb
CHANGED
@@ -1,23 +1,20 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require "forwardable"
|
3
4
|
require_relative "utils/enum_deep_dup"
|
4
5
|
|
5
6
|
class Jat
|
6
7
|
class Config
|
7
8
|
module InstanceMethods
|
9
|
+
extend Forwardable
|
10
|
+
|
8
11
|
attr_reader :opts
|
9
12
|
|
10
13
|
def initialize(opts = {})
|
11
14
|
@opts = EnumDeepDup.call(opts)
|
12
15
|
end
|
13
16
|
|
14
|
-
|
15
|
-
opts[key] = value
|
16
|
-
end
|
17
|
-
|
18
|
-
def [](key)
|
19
|
-
opts[key]
|
20
|
-
end
|
17
|
+
def_delegators :@opts, :[], :[]=, :fetch
|
21
18
|
end
|
22
19
|
|
23
20
|
module ClassMethods
|
@@ -87,7 +87,7 @@ class Jat
|
|
87
87
|
attributes.each do |name, attribute|
|
88
88
|
next if data.key?(name)
|
89
89
|
|
90
|
-
value =
|
90
|
+
value = attribute.value(object, context)
|
91
91
|
|
92
92
|
unless value.nil?
|
93
93
|
data = data.dup if data.equal?(FROZEN_EMPTY_HASH)
|
@@ -98,10 +98,6 @@ class Jat
|
|
98
98
|
data
|
99
99
|
end
|
100
100
|
|
101
|
-
def attribute_value(attribute)
|
102
|
-
attribute.block.call(object, context)
|
103
|
-
end
|
104
|
-
|
105
101
|
def context_jsonapi
|
106
102
|
context_attr_transform(:jsonapi)
|
107
103
|
end
|
@@ -60,7 +60,7 @@ class Jat
|
|
60
60
|
|
61
61
|
attributes_names.each_with_object({}) do |name, attrs|
|
62
62
|
attribute = jat_class.attributes[name]
|
63
|
-
attrs[name] = attribute.
|
63
|
+
attrs[name] = attribute.value(object, context)
|
64
64
|
end
|
65
65
|
end
|
66
66
|
|
@@ -70,7 +70,7 @@ class Jat
|
|
70
70
|
|
71
71
|
relationships_names.each_with_object({}) do |name, rels|
|
72
72
|
rel_attribute = jat_class.attributes[name]
|
73
|
-
rel_object = rel_attribute.
|
73
|
+
rel_object = rel_attribute.value(object, context)
|
74
74
|
|
75
75
|
rel_serializer = rel_attribute.serializer.call
|
76
76
|
rel_links = get_relationship_links(rel_serializer, rel_object)
|
@@ -123,14 +123,14 @@ class Jat
|
|
123
123
|
def get_links
|
124
124
|
jat_class
|
125
125
|
.object_links
|
126
|
-
.transform_values { |attr| attr.
|
126
|
+
.transform_values { |attr| attr.value(object, context) }
|
127
127
|
.tap(&:compact!)
|
128
128
|
end
|
129
129
|
|
130
130
|
def get_meta
|
131
131
|
jat_class
|
132
132
|
.added_object_meta
|
133
|
-
.transform_values { |attr| attr.
|
133
|
+
.transform_values { |attr| attr.value(object, context) }
|
134
134
|
.tap(&:compact!)
|
135
135
|
end
|
136
136
|
|
@@ -141,7 +141,7 @@ class Jat
|
|
141
141
|
context[:parent_object] = object
|
142
142
|
|
143
143
|
links
|
144
|
-
.transform_values { |attr| attr.
|
144
|
+
.transform_values { |attr| attr.value(rel_object, context) }
|
145
145
|
.tap(&:compact!)
|
146
146
|
.tap { context.delete(:parent_object) }
|
147
147
|
end
|
@@ -153,7 +153,7 @@ class Jat
|
|
153
153
|
context[:parent_object] = object
|
154
154
|
|
155
155
|
meta
|
156
|
-
.transform_values { |attr| attr.
|
156
|
+
.transform_values { |attr| attr.value(rel_object, context) }
|
157
157
|
.tap(&:compact!)
|
158
158
|
.tap { context.delete(:parent_object) }
|
159
159
|
end
|
@@ -163,7 +163,7 @@ class Jat
|
|
163
163
|
end
|
164
164
|
|
165
165
|
def id
|
166
|
-
@id ||= jat_class.get_id.
|
166
|
+
@id ||= jat_class.get_id.value(object, context)
|
167
167
|
end
|
168
168
|
end
|
169
169
|
|
@@ -95,7 +95,7 @@ class Jat
|
|
95
95
|
meta.each do |name, attribute|
|
96
96
|
next if data.key?(name)
|
97
97
|
|
98
|
-
value =
|
98
|
+
value = attribute.value(object, context)
|
99
99
|
|
100
100
|
unless value.nil?
|
101
101
|
data = data.dup if data.equal?(FROZEN_EMPTY_HASH)
|
@@ -109,10 +109,6 @@ class Jat
|
|
109
109
|
def context_metadata
|
110
110
|
context[:meta]&.transform_keys(&:to_sym) || FROZEN_EMPTY_HASH
|
111
111
|
end
|
112
|
-
|
113
|
-
def attribute_value(attribute)
|
114
|
-
attribute.block.call(object, context)
|
115
|
-
end
|
116
112
|
end
|
117
113
|
|
118
114
|
extend ClassMethods
|
@@ -37,7 +37,7 @@ class Jat
|
|
37
37
|
|
38
38
|
map.each do |key, inner_map|
|
39
39
|
attribute = jat_class.attributes.fetch(key)
|
40
|
-
value =
|
40
|
+
value = attribute.value(object, context)
|
41
41
|
|
42
42
|
result[key] =
|
43
43
|
if attribute.relation?
|
@@ -56,10 +56,6 @@ class Jat
|
|
56
56
|
|
57
57
|
private
|
58
58
|
|
59
|
-
def attribute_value(attribute)
|
60
|
-
attribute.block.call(object, context)
|
61
|
-
end
|
62
|
-
|
63
59
|
def inner_piece(attribute, value, inner_map)
|
64
60
|
serializer = attribute.serializer.call
|
65
61
|
serializer::ResponsePiece.to_h(value, context, inner_map)
|
@@ -0,0 +1,54 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Jat
|
4
|
+
module Plugins
|
5
|
+
module Types
|
6
|
+
def self.plugin_name
|
7
|
+
:types
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.load(jat_class, **_opts)
|
11
|
+
jat_class::Attribute.include(InstanceMethods)
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.after_load(jat_class, **_opts)
|
15
|
+
jat_class.config[:types] = {
|
16
|
+
array: ->(value) { Array(value) },
|
17
|
+
bool: ->(value) { !!value },
|
18
|
+
float: ->(value) { Float(value) },
|
19
|
+
hash: ->(value) { Hash(value) },
|
20
|
+
int: ->(value) { Integer(value) },
|
21
|
+
str: ->(value) { String(value) }
|
22
|
+
}
|
23
|
+
end
|
24
|
+
|
25
|
+
module InstanceMethods
|
26
|
+
def block
|
27
|
+
return @block if instance_variable_defined?(:@block)
|
28
|
+
|
29
|
+
original_block = super
|
30
|
+
type = opts[:type]
|
31
|
+
return original_block unless type
|
32
|
+
|
33
|
+
@block = typed_block(type, original_block)
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def typed_block(type, original_block)
|
39
|
+
proc do |object, context|
|
40
|
+
value = original_block.call(object, context)
|
41
|
+
|
42
|
+
# Type conversion
|
43
|
+
case type
|
44
|
+
when Symbol then self.class.jat_class.config.fetch(:types).fetch(type).call(value)
|
45
|
+
else type.call(value)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
register_plugin(Types.plugin_name, Types)
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "test_helper"
|
4
|
+
|
5
|
+
describe "Jat::Plugins::Types" do
|
6
|
+
let(:struct) { Struct.new(:attr) }
|
7
|
+
let(:obj) { struct.new(nil) }
|
8
|
+
|
9
|
+
let(:jat_class) do
|
10
|
+
new_class = Class.new(Jat)
|
11
|
+
new_class.plugin(:types)
|
12
|
+
new_class
|
13
|
+
end
|
14
|
+
|
15
|
+
def attr_value(original_value)
|
16
|
+
obj = struct.new(original_value)
|
17
|
+
jat_class.attributes[:attr].value(obj, nil)
|
18
|
+
end
|
19
|
+
|
20
|
+
it "has predefined :int type" do
|
21
|
+
jat_class.attribute(:attr, type: :int)
|
22
|
+
|
23
|
+
assert_equal(0, attr_value("0"))
|
24
|
+
assert_equal(8, attr_value("010")) # octal system
|
25
|
+
assert_equal(10, attr_value("0xa")) # hexadecimal system
|
26
|
+
end
|
27
|
+
|
28
|
+
it "has predefined :bool type" do
|
29
|
+
jat_class.attribute(:attr, type: :bool)
|
30
|
+
|
31
|
+
assert_equal(false, attr_value(nil))
|
32
|
+
assert_equal(false, attr_value(false))
|
33
|
+
assert_equal(true, attr_value(1))
|
34
|
+
end
|
35
|
+
|
36
|
+
it "has predefined :float type" do
|
37
|
+
jat_class.attribute(:attr, type: :float)
|
38
|
+
|
39
|
+
assert_same(0.0, attr_value("0"))
|
40
|
+
assert_same(0.001, attr_value("1e-3"))
|
41
|
+
assert_same(1.234, attr_value("1.234"))
|
42
|
+
end
|
43
|
+
|
44
|
+
it "has predefined :array type" do
|
45
|
+
jat_class.attribute(:attr, type: :array)
|
46
|
+
|
47
|
+
assert_equal([1], attr_value(1))
|
48
|
+
assert_equal([1], attr_value([1]))
|
49
|
+
end
|
50
|
+
|
51
|
+
it "has predefined :hash type" do
|
52
|
+
jat_class.attribute(:attr, type: :hash)
|
53
|
+
|
54
|
+
assert_equal({}, attr_value([]))
|
55
|
+
assert_equal({}, attr_value({}))
|
56
|
+
assert_equal({key: :value}, attr_value(key: :value))
|
57
|
+
end
|
58
|
+
|
59
|
+
it "has predefined :str type" do
|
60
|
+
jat_class.attribute(:attr, type: :str)
|
61
|
+
|
62
|
+
assert_equal("", attr_value(nil))
|
63
|
+
assert_equal("123", attr_value(123))
|
64
|
+
end
|
65
|
+
|
66
|
+
it "allows to use callable method as type" do
|
67
|
+
jat_class.attribute(:attr, type: ->(obj) { obj ? :yes : :no })
|
68
|
+
|
69
|
+
assert_equal(:no, attr_value(nil))
|
70
|
+
assert_equal(:yes, attr_value(1))
|
71
|
+
end
|
72
|
+
|
73
|
+
it "allows to configure custom type" do
|
74
|
+
jat_class.config[:types][:year] = ->(obj) { obj.strftime("%Y") }
|
75
|
+
jat_class.attribute(:attr, type: :year)
|
76
|
+
|
77
|
+
assert_equal("2020", attr_value(Time.new(2020, 10, 10)))
|
78
|
+
end
|
79
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jat
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrey Glushkov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-01-
|
11
|
+
date: 2022-01-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: minitest
|
@@ -86,14 +86,14 @@ dependencies:
|
|
86
86
|
requirements:
|
87
87
|
- - "~>"
|
88
88
|
- !ruby/object:Gem::Version
|
89
|
-
version: '
|
89
|
+
version: '7.0'
|
90
90
|
type: :development
|
91
91
|
prerelease: false
|
92
92
|
version_requirements: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
94
|
- - "~>"
|
95
95
|
- !ruby/object:Gem::Version
|
96
|
-
version: '
|
96
|
+
version: '7.0'
|
97
97
|
- !ruby/object:Gem::Dependency
|
98
98
|
name: sqlite3
|
99
99
|
requirement: !ruby/object:Gem::Requirement
|
@@ -166,6 +166,7 @@ files:
|
|
166
166
|
- lib/jat/plugins/simple_api/plugins/simple_api_validate_params/simple_api_validate_params.rb
|
167
167
|
- lib/jat/plugins/simple_api/simple_api.rb
|
168
168
|
- lib/jat/plugins/to_str/to_str.rb
|
169
|
+
- lib/jat/plugins/types/types.rb
|
169
170
|
- lib/jat/plugins/validate_params/validate_params.rb
|
170
171
|
- lib/jat/utils/enum_deep_dup.rb
|
171
172
|
- lib/jat/utils/enum_deep_freeze.rb
|
@@ -200,6 +201,7 @@ files:
|
|
200
201
|
- test/lib/jat/plugins/simple_api_preloads/simple_api_preloads_test.rb
|
201
202
|
- test/lib/jat/plugins/simple_api_validate_params/simple_api_validate_params_test.rb
|
202
203
|
- test/lib/jat/plugins/to_str/to_str_test.rb
|
204
|
+
- test/lib/jat/plugins/types/types_test.rb
|
203
205
|
- test/lib/jat/utils/enum_deep_dup_test.rb
|
204
206
|
- test/lib/jat/utils/enum_deep_freeze_test.rb
|
205
207
|
- test/lib/jat_test.rb
|
@@ -265,6 +267,7 @@ test_files:
|
|
265
267
|
- test/lib/jat/plugins/simple_api_preloads/simple_api_preloads_test.rb
|
266
268
|
- test/lib/jat/plugins/simple_api_validate_params/simple_api_validate_params_test.rb
|
267
269
|
- test/lib/jat/plugins/to_str/to_str_test.rb
|
270
|
+
- test/lib/jat/plugins/types/types_test.rb
|
268
271
|
- test/lib/jat/utils/enum_deep_dup_test.rb
|
269
272
|
- test/lib/jat/utils/enum_deep_freeze_test.rb
|
270
273
|
- test/lib/jat_test.rb
|