jat 0.0.3 → 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/jat/attribute.rb +26 -5
- data/lib/jat/config.rb +4 -7
- data/lib/jat/plugins/activerecord/activerecord.rb +23 -0
- data/lib/jat/plugins/base/base_activerecord_preloads/base_activerecord_preloads.rb +38 -0
- data/lib/jat/plugins/{_activerecord_preloads → base/base_activerecord_preloads}/lib/preloader.rb +22 -16
- data/lib/jat/plugins/base/base_lower_camel_case/base_lower_camel_case.rb +36 -0
- data/lib/jat/plugins/{_preloads/_preloads.rb → base/base_preloads/base_preloads.rb} +14 -4
- data/lib/jat/plugins/{_preloads → base/base_preloads}/lib/format_user_preloads.rb +1 -1
- data/lib/jat/plugins/{_preloads → base/base_preloads}/lib/preloads_with_path.rb +1 -1
- data/lib/jat/plugins/cache/cache.rb +14 -6
- data/lib/jat/plugins/json_api/json_api.rb +150 -106
- data/lib/jat/plugins/json_api/lib/fields_param_parser.rb +40 -0
- data/lib/jat/plugins/json_api/lib/include_param_parser.rb +84 -0
- data/lib/jat/plugins/json_api/lib/map.rb +92 -27
- data/lib/jat/plugins/json_api/lib/params/fields/validate.rb +8 -5
- data/lib/jat/plugins/json_api/lib/response.rb +81 -197
- data/lib/jat/plugins/json_api/lib/response_piece.rb +175 -0
- data/lib/jat/plugins/json_api/plugins/json_api_activerecord/json_api_activerecord.rb +23 -0
- data/lib/jat/plugins/json_api/plugins/json_api_lower_camel_case/json_api_lower_camel_case.rb +34 -0
- data/lib/jat/plugins/json_api/plugins/json_api_maps_cache/json_api_maps_cache.rb +58 -0
- data/lib/jat/plugins/json_api/plugins/json_api_preloads/json_api_preloads.rb +38 -0
- data/lib/jat/plugins/{_json_api_activerecord → json_api/plugins/json_api_preloads}/lib/preloads.rb +17 -25
- data/lib/jat/plugins/json_api/plugins/json_api_validate_params/json_api_validate_params.rb +61 -0
- data/lib/jat/plugins/json_api/plugins/json_api_validate_params/lib/params_error.rb +6 -0
- data/lib/jat/plugins/json_api/plugins/json_api_validate_params/lib/validate_fields_param.rb +59 -0
- data/lib/jat/plugins/json_api/plugins/json_api_validate_params/lib/validate_include_param.rb +33 -0
- data/lib/jat/plugins/lower_camel_case/lower_camel_case.rb +23 -0
- data/lib/jat/plugins/maps_cache/maps_cache.rb +23 -0
- data/lib/jat/plugins/preloads/preloads.rb +23 -0
- data/lib/jat/plugins/simple_api/lib/fields_param_parser.rb +97 -0
- data/lib/jat/plugins/simple_api/lib/map.rb +80 -10
- data/lib/jat/plugins/simple_api/lib/response.rb +75 -90
- data/lib/jat/plugins/simple_api/lib/response_piece.rb +80 -0
- data/lib/jat/plugins/simple_api/plugins/simple_api_activerecord/simple_api_activerecord.rb +23 -0
- data/lib/jat/plugins/simple_api/plugins/simple_api_lower_camel_case/simple_api_lower_camel_case.rb +34 -0
- data/lib/jat/plugins/simple_api/plugins/simple_api_maps_cache/simple_api_maps_cache.rb +50 -0
- data/lib/jat/plugins/simple_api/plugins/simple_api_preloads/lib/preloads.rb +55 -0
- data/lib/jat/plugins/simple_api/plugins/simple_api_preloads/simple_api_preloads.rb +38 -0
- data/lib/jat/plugins/simple_api/plugins/simple_api_validate_params/lib/fields_error.rb +6 -0
- data/lib/jat/plugins/simple_api/plugins/simple_api_validate_params/lib/validate_fields_param.rb +45 -0
- data/lib/jat/plugins/simple_api/plugins/simple_api_validate_params/simple_api_validate_params.rb +49 -0
- data/lib/jat/plugins/simple_api/simple_api.rb +87 -27
- data/lib/jat/plugins/to_str/to_str.rb +15 -5
- data/lib/jat/plugins/types/types.rb +54 -0
- data/lib/jat/plugins/validate_params/validate_params.rb +23 -0
- data/lib/jat/plugins.rb +42 -30
- data/lib/jat.rb +40 -35
- data/test/lib/jat/attribute_test.rb +22 -4
- data/test/lib/jat/plugins/activerecord/activerecord_test.rb +27 -0
- data/test/lib/jat/plugins/base_activerecord_preloads/base_activerecord_preloads_test.rb +59 -0
- data/test/lib/jat/plugins/{_activerecord_preloads → base_activerecord_preloads}/lib/preloader_test.rb +11 -25
- data/test/lib/jat/plugins/base_lower_camel_case/base_lower_camel_case_test.rb +26 -0
- data/test/lib/jat/plugins/{_preloads/_preloads_test.rb → base_preloads/base_preloads_test.rb} +2 -2
- data/test/lib/jat/plugins/{_preloads → base_preloads}/lib/format_user_preloads_test.rb +3 -3
- data/test/lib/jat/plugins/{_preloads → base_preloads}/lib/preloads_with_path_test.rb +2 -2
- data/test/lib/jat/plugins/cache/cache_test.rb +11 -11
- data/test/lib/jat/plugins/json_api/json_api_test.rb +57 -49
- data/test/lib/jat/plugins/json_api/lib/fields_param_parser_test.rb +46 -0
- data/test/lib/jat/plugins/json_api/lib/include_param_parser_test.rb +57 -0
- data/test/lib/jat/plugins/json_api/lib/map_test.rb +171 -92
- data/test/lib/jat/plugins/json_api/lib/response_piece_test.rb +13 -0
- data/test/lib/jat/plugins/json_api/lib/response_test.rb +38 -32
- data/test/lib/jat/plugins/json_api_activerecord/json_api_activerecord_test.rb +24 -0
- data/test/lib/jat/plugins/json_api_lower_camel_case/json_api_lower_camel_case_test.rb +85 -0
- data/test/lib/jat/plugins/json_api_maps_cache/json_api_maps_cache_test.rb +107 -0
- data/test/lib/jat/plugins/json_api_preloads/json_api_preloads_test.rb +51 -0
- data/test/lib/jat/plugins/{_json_api_activerecord → json_api_preloads}/lib/preloads_test.rb +19 -13
- data/test/lib/jat/plugins/json_api_validate_params/json_api_validate_params_test.rb +108 -0
- data/test/lib/jat/plugins/lower_camel_case/lower_camel_case_test.rb +27 -0
- data/test/lib/jat/plugins/maps_cache/maps_cache_test.rb +27 -0
- data/test/lib/jat/plugins/preloads/preloads_test.rb +27 -0
- data/test/lib/jat/plugins/simple_api/lib/fields_param_parser_test.rb +87 -0
- data/test/lib/jat/plugins/simple_api/lib/map_test.rb +118 -33
- data/test/lib/jat/plugins/simple_api/lib/response_piece_test.rb +13 -0
- data/test/lib/jat/plugins/simple_api/lib/response_test.rb +90 -76
- data/test/lib/jat/plugins/simple_api/simple_api_test.rb +118 -25
- data/test/lib/jat/plugins/simple_api_activerecord/simple_api_activerecord_test.rb +24 -0
- data/test/lib/jat/plugins/simple_api_lower_camel_case/simple_api_lower_camel_case_test.rb +54 -0
- data/test/lib/jat/plugins/simple_api_maps_cache/simple_api_maps_cache_test.rb +95 -0
- data/test/lib/jat/plugins/simple_api_preloads/lib/preloads_test.rb +140 -0
- data/test/lib/jat/plugins/simple_api_preloads/simple_api_preloads_test.rb +51 -0
- data/test/lib/jat/plugins/simple_api_validate_params/simple_api_validate_params_test.rb +101 -0
- data/test/lib/jat/plugins/to_str/to_str_test.rb +3 -3
- data/test/lib/jat/plugins/types/types_test.rb +84 -0
- data/test/lib/jat/plugins/validate_params/validate_params_test.rb +27 -0
- data/test/lib/jat/plugins_test.rb +88 -0
- data/test/lib/jat_test.rb +47 -24
- data/test/lib/plugin_test.rb +3 -3
- data/test/test_helper.rb +7 -6
- data/test/test_plugin.rb +9 -12
- metadata +107 -88
- data/CHANGELOG.md +0 -7
- data/README.md +0 -21
- data/jat.gemspec +0 -37
- data/lib/jat/plugins/_activerecord_preloads/_activerecord_preloads.rb +0 -29
- data/lib/jat/plugins/_json_api_activerecord/_json_api_activerecord.rb +0 -22
- data/lib/jat/plugins/camel_lower/camel_lower.rb +0 -18
- data/lib/jat/plugins/json_api/lib/construct_traversal_map.rb +0 -91
- data/lib/jat/plugins/json_api/lib/presenters/document_links_presenter.rb +0 -48
- data/lib/jat/plugins/json_api/lib/presenters/document_meta_presenter.rb +0 -48
- data/lib/jat/plugins/json_api/lib/presenters/jsonapi_presenter.rb +0 -48
- data/lib/jat/plugins/json_api/lib/presenters/links_presenter.rb +0 -48
- data/lib/jat/plugins/json_api/lib/presenters/meta_presenter.rb +0 -48
- data/lib/jat/plugins/json_api/lib/presenters/relationship_links_presenter.rb +0 -53
- data/lib/jat/plugins/json_api/lib/presenters/relationship_meta_presenter.rb +0 -53
- data/lib/jat/plugins/json_api/lib/traversal_map.rb +0 -34
- data/lib/jat/plugins/simple_api/lib/construct_traversal_map.rb +0 -45
- data/lib/jat/plugins/simple_api/lib/params/parse.rb +0 -68
- data/lib/jat/presenter.rb +0 -51
- data/test/lib/jat/plugins/_activerecord_preloads/_activerecord_preloads_test.rb +0 -40
- data/test/lib/jat/plugins/_json_api_activerecord/_json_api_activerecord_test.rb +0 -29
- data/test/lib/jat/plugins/camel_lower/camel_lower_test.rb +0 -78
- data/test/lib/jat/plugins/json_api/lib/construct_traversal_map_test.rb +0 -119
- data/test/lib/jat/plugins/json_api/lib/params/fields/parse_test.rb +0 -24
- data/test/lib/jat/plugins/json_api/lib/params/fields/validate_test.rb +0 -47
- data/test/lib/jat/plugins/json_api/lib/params/fields_test.rb +0 -37
- data/test/lib/jat/plugins/json_api/lib/params/include/parse_test.rb +0 -46
- data/test/lib/jat/plugins/json_api/lib/params/include/validate_test.rb +0 -51
- data/test/lib/jat/plugins/json_api/lib/params/include_test.rb +0 -41
- data/test/lib/jat/plugins/json_api/lib/presenters/document_links_presenter_test.rb +0 -69
- data/test/lib/jat/plugins/json_api/lib/presenters/document_meta_presenter_test.rb +0 -69
- data/test/lib/jat/plugins/json_api/lib/presenters/jsonapi_presenter_test.rb +0 -69
- data/test/lib/jat/plugins/json_api/lib/presenters/links_presenter_test.rb +0 -69
- data/test/lib/jat/plugins/json_api/lib/presenters/meta_presenter_test.rb +0 -69
- data/test/lib/jat/plugins/json_api/lib/presenters/relationship_links_presenter_test.rb +0 -75
- data/test/lib/jat/plugins/json_api/lib/presenters/relationship_meta_presenter_test.rb +0 -75
- data/test/lib/jat/plugins/json_api/lib/traversal_map_test.rb +0 -58
- data/test/lib/jat/plugins/simple_api/lib/construct_traversal_map_test.rb +0 -100
- data/test/lib/jat/plugins/simple_api/lib/params/parse_test.rb +0 -71
- data/test/lib/jat/presenter_test.rb +0 -61
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c266d5784440b62adbb20bb09902f0134ad5e6a06245974f9b15a0557733528d
|
4
|
+
data.tar.gz: 989344e5918b193a2db72a863337c7209aa34ee9e1b28b7bf68f8bd9a9749b9d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: eba6f92cd51c734ef0583447b2300971fd4707e0e79cece169858d6551e75fba688ce6155dae9317184d423b519e1398d8f583b643174cf4d4450700d73bf3c9
|
7
|
+
data.tar.gz: bf14496368b37ec614f0f79b76c217df8c60a8401a54d48e30925fad6c7501fdba7533758b9b5bde24035b645aae2c204b9fc05740282a4ff23ce5e80968e529
|
data/lib/jat/attribute.rb
CHANGED
@@ -9,6 +9,8 @@ class Jat
|
|
9
9
|
attr_reader :params, :opts
|
10
10
|
|
11
11
|
def initialize(name:, opts: {}, block: nil)
|
12
|
+
check_block_valid(block)
|
13
|
+
|
12
14
|
@opts = EnumDeepDup.call(opts)
|
13
15
|
@params = EnumDeepFreeze.call(name: name, opts: @opts, block: block)
|
14
16
|
end
|
@@ -59,11 +61,30 @@ class Jat
|
|
59
61
|
end
|
60
62
|
|
61
63
|
def block
|
62
|
-
@block
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
64
|
+
return @block if instance_variable_defined?(:@block)
|
65
|
+
|
66
|
+
current_block = params.fetch(:block)
|
67
|
+
current_block ||= keyword_block
|
68
|
+
|
69
|
+
@block = current_block
|
70
|
+
end
|
71
|
+
|
72
|
+
def value(object, context)
|
73
|
+
block.call(object, context)
|
74
|
+
end
|
75
|
+
|
76
|
+
private
|
77
|
+
|
78
|
+
def keyword_block
|
79
|
+
key_method_name = key
|
80
|
+
proc { |object| object.public_send(key_method_name) }
|
81
|
+
end
|
82
|
+
|
83
|
+
def check_block_valid(block)
|
84
|
+
return unless block
|
85
|
+
|
86
|
+
params = block.parameters
|
87
|
+
raise Error, "Block can have 0-2 parameters" if params.count > 2
|
67
88
|
end
|
68
89
|
end
|
69
90
|
|
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
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Jat
|
4
|
+
module Plugins
|
5
|
+
module Activerecord
|
6
|
+
def self.plugin_name
|
7
|
+
:activerecord
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.load(jat_class, **opts)
|
11
|
+
if jat_class.plugin_used?(:json_api)
|
12
|
+
jat_class.plugin :json_api_activerecord, **opts
|
13
|
+
elsif jat_class.plugin_used?(:simple_api)
|
14
|
+
jat_class.plugin :simple_api_activerecord, **opts
|
15
|
+
else
|
16
|
+
raise Error, "Please load :json_api or :simple_api plugin first"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
register_plugin(Activerecord.plugin_name, Activerecord)
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "./lib/preloader"
|
4
|
+
|
5
|
+
class Jat
|
6
|
+
module Plugins
|
7
|
+
module BaseActiverecordPreloads
|
8
|
+
def self.plugin_name
|
9
|
+
:base_activerecord_preloads
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.load(jat_class, **_opts)
|
13
|
+
jat_class.include(InstanceMethods)
|
14
|
+
end
|
15
|
+
|
16
|
+
module InstanceMethods
|
17
|
+
def to_h(object)
|
18
|
+
object = add_preloads(object)
|
19
|
+
super
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def add_preloads(obj)
|
25
|
+
return obj if obj.nil? || (obj.is_a?(Array) && obj.empty?)
|
26
|
+
|
27
|
+
# preloads() method comes from simple_api_activerecord or json_api_activerecord plugin
|
28
|
+
preloads = preloads()
|
29
|
+
return obj if preloads.empty?
|
30
|
+
|
31
|
+
Preloader.preload(obj, preloads)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
register_plugin(BaseActiverecordPreloads.plugin_name, BaseActiverecordPreloads)
|
37
|
+
end
|
38
|
+
end
|
data/lib/jat/plugins/{_activerecord_preloads → base/base_activerecord_preloads}/lib/preloader.rb
RENAMED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
class Jat
|
4
4
|
module Plugins
|
5
|
-
module
|
5
|
+
module BaseActiverecordPreloads
|
6
6
|
class Preloader
|
7
7
|
module ClassMethods
|
8
8
|
def preload(object, preloads)
|
@@ -20,6 +20,18 @@ class Jat
|
|
20
20
|
extend ClassMethods
|
21
21
|
end
|
22
22
|
|
23
|
+
class Loader
|
24
|
+
# :nocov: We can check only one version of activerecord
|
25
|
+
def self.call(records, associations)
|
26
|
+
if ActiveRecord::VERSION::MAJOR >= 7
|
27
|
+
ActiveRecord::Associations::Preloader.new(records: records, associations: associations).call
|
28
|
+
else
|
29
|
+
ActiveRecord::Associations::Preloader.new.preload(records, associations)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
# :nocov:
|
33
|
+
end
|
34
|
+
|
23
35
|
class ActiverecordObject
|
24
36
|
module ClassMethods
|
25
37
|
def fit?(object)
|
@@ -27,11 +39,7 @@ class Jat
|
|
27
39
|
end
|
28
40
|
|
29
41
|
def preload(object, preloads)
|
30
|
-
|
31
|
-
# ActiveRecord::Associations::Preloader
|
32
|
-
preloads.each_key { |key| object.association(key).reset }
|
33
|
-
ActiveRecord::Associations::Preloader.new.preload(object, preloads)
|
34
|
-
|
42
|
+
Loader.call([object], preloads)
|
35
43
|
object
|
36
44
|
end
|
37
45
|
end
|
@@ -46,7 +54,13 @@ class Jat
|
|
46
54
|
end
|
47
55
|
|
48
56
|
def preload(objects, preloads)
|
49
|
-
objects.
|
57
|
+
if objects.loaded?
|
58
|
+
array_objects = objects.to_a
|
59
|
+
Loader.call(array_objects, preloads)
|
60
|
+
objects
|
61
|
+
else
|
62
|
+
objects.preload(preloads).load
|
63
|
+
end
|
50
64
|
end
|
51
65
|
end
|
52
66
|
|
@@ -62,20 +76,12 @@ class Jat
|
|
62
76
|
end
|
63
77
|
|
64
78
|
def preload(objects, preloads)
|
65
|
-
|
66
|
-
# ActiveRecord::Associations::Preloader
|
67
|
-
preloads.each_key { |key| reset_association(objects, key) }
|
68
|
-
ActiveRecord::Associations::Preloader.new.preload(objects, preloads)
|
69
|
-
|
79
|
+
Loader.call(objects, preloads)
|
70
80
|
objects
|
71
81
|
end
|
72
82
|
|
73
83
|
private
|
74
84
|
|
75
|
-
def reset_association(objects, key)
|
76
|
-
objects.each { |object| object.association(key).reset }
|
77
|
-
end
|
78
|
-
|
79
85
|
def same_kind?(objects)
|
80
86
|
first_object_class = objects.first.class
|
81
87
|
objects.all? { |object| object.instance_of?(first_object_class) }
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Jat
|
4
|
+
module Plugins
|
5
|
+
module BaseLowerCamelCase
|
6
|
+
def self.plugin_name
|
7
|
+
:base_lower_camel_case
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.load(jat_class, **_opts)
|
11
|
+
jat_class::Attribute.include(AttributeInstanceMethods)
|
12
|
+
end
|
13
|
+
|
14
|
+
module AttributeInstanceMethods
|
15
|
+
def name
|
16
|
+
LowerCamelCaseTransformation.call(original_name)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
register_plugin(BaseLowerCamelCase.plugin_name, BaseLowerCamelCase)
|
22
|
+
end
|
23
|
+
|
24
|
+
class LowerCamelCaseTransformation
|
25
|
+
SEPARATOR = "_"
|
26
|
+
|
27
|
+
def self.call(string)
|
28
|
+
first_word, *others = string.to_s.split(SEPARATOR)
|
29
|
+
|
30
|
+
first_word[0] = first_word[0].downcase
|
31
|
+
last_words = others.each(&:capitalize!).join
|
32
|
+
|
33
|
+
:"#{first_word}#{last_words}"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -6,7 +6,15 @@ require_relative "./lib/preloads_with_path"
|
|
6
6
|
# This plugin adds attribute methods #preloads, #preloads_path
|
7
7
|
class Jat
|
8
8
|
module Plugins
|
9
|
-
module
|
9
|
+
module BasePreloads
|
10
|
+
def self.plugin_name
|
11
|
+
:base_preloads
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.load(jat_class, **_opts)
|
15
|
+
jat_class::Attribute.include(AttributeMethods)
|
16
|
+
end
|
17
|
+
|
10
18
|
module AttributeMethods
|
11
19
|
NULL_PRELOADS = [nil, [].freeze].freeze
|
12
20
|
|
@@ -28,7 +36,7 @@ class Jat
|
|
28
36
|
# we don't know which entity is main (:user or :profile in this example) but
|
29
37
|
# we need to know main value to add nested preloads to it.
|
30
38
|
# User can specify main preloaded entity by adding "!" suffix
|
31
|
-
# ({ user!: [:profile] } for example),
|
39
|
+
# ({ user!: [:profile] } for example), otherwise the latest key will be considered main.
|
32
40
|
def get_preloads_with_path
|
33
41
|
preloads_provided = opts.key?(:preload)
|
34
42
|
preloads =
|
@@ -43,11 +51,13 @@ class Jat
|
|
43
51
|
return NULL_PRELOADS if preloads_provided && !preloads
|
44
52
|
|
45
53
|
preloads = FormatUserPreloads.to_hash(preloads)
|
46
|
-
PreloadsWithPath.call(preloads)
|
54
|
+
preloads, path = PreloadsWithPath.call(preloads)
|
55
|
+
|
56
|
+
[EnumDeepFreeze.call(preloads), path.freeze]
|
47
57
|
end
|
48
58
|
end
|
49
59
|
end
|
50
60
|
|
51
|
-
register_plugin(
|
61
|
+
register_plugin(BasePreloads.plugin_name, BasePreloads)
|
52
62
|
end
|
53
63
|
end
|
@@ -3,29 +3,37 @@
|
|
3
3
|
class Jat
|
4
4
|
module Plugins
|
5
5
|
module Cache
|
6
|
+
def self.plugin_name
|
7
|
+
:cache
|
8
|
+
end
|
9
|
+
|
6
10
|
def self.before_load(jat_class, **opts)
|
7
11
|
jat_class.plugin :to_str, **opts
|
8
12
|
end
|
9
13
|
|
14
|
+
def self.load(jat_class, **_opts)
|
15
|
+
jat_class.include(InstanceMethods)
|
16
|
+
end
|
17
|
+
|
10
18
|
module InstanceMethods
|
11
19
|
FORMAT_TO_STR = :to_str
|
12
20
|
FORMAT_TO_H = :to_h
|
13
21
|
|
14
|
-
def to_h
|
22
|
+
def to_h(object)
|
15
23
|
return super if context[:_format] == FORMAT_TO_STR
|
16
24
|
|
17
25
|
context[:_format] = FORMAT_TO_H
|
18
|
-
cached { super }
|
26
|
+
cached(object) { super }
|
19
27
|
end
|
20
28
|
|
21
|
-
def to_str
|
29
|
+
def to_str(object)
|
22
30
|
context[:_format] = FORMAT_TO_STR
|
23
|
-
cached { super }
|
31
|
+
cached(object) { super }
|
24
32
|
end
|
25
33
|
|
26
34
|
private
|
27
35
|
|
28
|
-
def cached(&block)
|
36
|
+
def cached(object, &block)
|
29
37
|
cache = context[:cache]
|
30
38
|
return yield unless cache
|
31
39
|
|
@@ -34,6 +42,6 @@ class Jat
|
|
34
42
|
end
|
35
43
|
end
|
36
44
|
|
37
|
-
register_plugin(
|
45
|
+
register_plugin(Cache.plugin_name, Cache)
|
38
46
|
end
|
39
47
|
end
|