jat 0.0.3 → 0.0.5
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 +36 -4
- data/lib/jat/plugins/_activerecord_preloads/_activerecord_preloads.rb +7 -3
- data/lib/jat/plugins/_activerecord_preloads/lib/preloader.rb +7 -13
- data/lib/jat/plugins/_lower_camel_case/_lower_camel_case.rb +32 -0
- data/lib/jat/plugins/_preloads/_preloads.rb +8 -2
- data/lib/jat/plugins/cache/cache.rb +9 -5
- data/lib/jat/plugins/json_api/json_api.rb +145 -105
- 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 +84 -196
- data/lib/jat/plugins/json_api/lib/response_piece.rb +166 -0
- data/lib/jat/plugins/json_api_activerecord/json_api_activerecord.rb +31 -0
- data/lib/jat/plugins/{_json_api_activerecord → json_api_activerecord}/lib/preloads.rb +16 -24
- data/lib/jat/plugins/json_api_lower_camel_case/json_api_lower_camel_case.rb +30 -0
- data/lib/jat/plugins/json_api_maps_cache/json_api_maps_cache.rb +54 -0
- data/lib/jat/plugins/json_api_validate_params/json_api_validate_params.rb +57 -0
- data/lib/jat/plugins/json_api_validate_params/lib/params_error.rb +6 -0
- data/lib/jat/plugins/json_api_validate_params/lib/validate_fields_param.rb +59 -0
- data/lib/jat/plugins/json_api_validate_params/lib/validate_include_param.rb +33 -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 +78 -89
- data/lib/jat/plugins/simple_api/lib/response_piece.rb +84 -0
- data/lib/jat/plugins/simple_api/simple_api.rb +83 -24
- data/lib/jat/plugins/simple_api_activerecord/lib/preloads.rb +55 -0
- data/lib/jat/plugins/simple_api_activerecord/simple_api_activerecord.rb +31 -0
- data/lib/jat/plugins/simple_api_lower_camel_case/simple_api_lower_camel_case.rb +30 -0
- data/lib/jat/plugins/simple_api_maps_cache/simple_api_maps_cache.rb +48 -0
- data/lib/jat/plugins/simple_api_validate_params/lib/fields_error.rb +6 -0
- data/lib/jat/plugins/simple_api_validate_params/lib/validate_fields_param.rb +45 -0
- data/lib/jat/plugins/simple_api_validate_params/simple_api_validate_params.rb +45 -0
- data/lib/jat/plugins/to_str/to_str.rb +10 -4
- data/lib/jat/plugins.rb +3 -16
- data/lib/jat.rb +28 -30
- data/test/lib/jat/attribute_test.rb +15 -5
- data/test/lib/jat/plugins/_activerecord_preloads/_activerecord_preloads_test.rb +34 -15
- data/test/lib/jat/plugins/_activerecord_preloads/lib/preloader_test.rb +10 -24
- data/test/lib/jat/plugins/_camel_lower/_camel_lower_test.rb +26 -0
- data/test/lib/jat/plugins/_preloads/lib/format_user_preloads_test.rb +1 -1
- data/test/lib/jat/plugins/_preloads/lib/preloads_with_path_test.rb +1 -1
- data/test/lib/jat/plugins/cache/cache_test.rb +11 -11
- data/test/lib/jat/plugins/json_api/json_api_test.rb +63 -47
- data/test/lib/jat/plugins/json_api/lib/{params/fields_test.rb → fields_param_parser_test.rb} +7 -6
- data/test/lib/jat/plugins/json_api/lib/{params/include_test.rb → include_param_parser_test.rb} +4 -4
- data/test/lib/jat/plugins/json_api/lib/map_test.rb +150 -79
- data/test/lib/jat/plugins/json_api/lib/response_test.rb +32 -32
- data/test/lib/jat/plugins/{_json_api_activerecord/_json_api_activerecord_test.rb → json_api_activerecord/json_api_activerecord_test.rb} +14 -5
- data/test/lib/jat/plugins/{_json_api_activerecord → json_api_activerecord}/lib/preloads_test.rb +11 -10
- data/test/lib/jat/plugins/json_api_camel_lower/json_api_camel_lower_test.rb +79 -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_validate_params/json_api_validate_params_test.rb +84 -0
- data/test/lib/jat/plugins/simple_api/lib/{params/parse_test.rb → fields_param_parser_test.rb} +10 -4
- data/test/lib/jat/plugins/simple_api/lib/map_test.rb +111 -34
- data/test/lib/jat/plugins/simple_api/lib/response_test.rb +80 -74
- data/test/lib/jat/plugins/simple_api/simple_api_test.rb +91 -25
- data/test/lib/jat/plugins/simple_api_activerecord/lib/preloads_test.rb +135 -0
- data/test/lib/jat/plugins/simple_api_activerecord/simple_api_activerecord_test.rb +38 -0
- data/test/lib/jat/plugins/simple_api_camel_lower/simple_api_camel_lower_test.rb +48 -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_validate_params/simple_api_validate_params_test.rb +89 -0
- data/test/lib/jat/plugins/to_str/to_str_test.rb +3 -3
- data/test/lib/jat_test.rb +47 -24
- data/test/lib/plugin_test.rb +3 -3
- data/test/test_helper.rb +0 -3
- data/test/test_plugin.rb +9 -12
- metadata +60 -71
- data/CHANGELOG.md +0 -7
- data/README.md +0 -21
- data/jat.gemspec +0 -37
- 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/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/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/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/presenter_test.rb +0 -61
@@ -0,0 +1,48 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Jat
|
4
|
+
module Plugins
|
5
|
+
module SimpleApiMapsCache
|
6
|
+
def self.before_load(jat_class, **_opts)
|
7
|
+
return if jat_class.plugin_used?(:simple_api)
|
8
|
+
|
9
|
+
raise Error, "Please load :simple_api plugin first"
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.load(jat_class, **_opts)
|
13
|
+
jat_class::Map.extend(MapsCacheClassMethods)
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.after_load(jat_class, **opts)
|
17
|
+
jat_class.config[:cached_maps_count] = opts[:cached_maps_count] || 100
|
18
|
+
end
|
19
|
+
|
20
|
+
module MapsCacheClassMethods
|
21
|
+
# Caches up to `:cached_maps_count` maps for each serializer.
|
22
|
+
# Removes earliest value if new value exceeds limit.
|
23
|
+
def maps_cache
|
24
|
+
@maps_cache ||= Hash.new do |cache, cache_key|
|
25
|
+
cache.shift if cache.length >= jat_class.config[:cached_maps_count] # protect from memory leak
|
26
|
+
cache[cache_key] = EnumDeepFreeze.call(yield)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def construct_map(exposed, fields)
|
33
|
+
cache = maps_cache { super }
|
34
|
+
cache_key = cache_key(exposed, fields)
|
35
|
+
cache[cache_key]
|
36
|
+
end
|
37
|
+
|
38
|
+
def cache_key(exposed, fields)
|
39
|
+
key = "exposed:#{exposed}:"
|
40
|
+
key += "fields:#{fields}:" if fields
|
41
|
+
key
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
register_plugin(:simple_api_maps_cache, SimpleApiMapsCache)
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Jat
|
4
|
+
module Plugins
|
5
|
+
module SimpleApiValidateParams
|
6
|
+
class ValidateFieldsParam
|
7
|
+
class << self
|
8
|
+
def call(jat_class, fields, prev_names = [])
|
9
|
+
fields.each do |name, nested_fields|
|
10
|
+
attribute = jat_class.attributes[name]
|
11
|
+
|
12
|
+
raise_error(name, prev_names) unless attribute
|
13
|
+
next if nested_fields.empty?
|
14
|
+
|
15
|
+
raise_nested_error(name, prev_names, nested_fields) unless attribute.relation?
|
16
|
+
nested_serializer = attribute.serializer.call
|
17
|
+
call(nested_serializer, nested_fields, prev_names + [name])
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def raise_error(name, prev_names)
|
24
|
+
field_name = field_name(name, prev_names)
|
25
|
+
|
26
|
+
raise SimpleApiFieldsError, "Field #{field_name} not exists"
|
27
|
+
end
|
28
|
+
|
29
|
+
def raise_nested_error(name, prev_names, nested_fields)
|
30
|
+
field_name = field_name(name, prev_names)
|
31
|
+
first_nested = nested_fields.keys.first
|
32
|
+
|
33
|
+
raise SimpleApiFieldsError, "Field #{field_name} is not a relationship to add '#{first_nested}' attribute"
|
34
|
+
end
|
35
|
+
|
36
|
+
def field_name(name, prev_names)
|
37
|
+
res = "'#{name}'"
|
38
|
+
res += " ('#{prev_names.join(".")}.#{name}')" if prev_names.any?
|
39
|
+
res
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "./lib/fields_error"
|
4
|
+
require_relative "./lib/validate_fields_param"
|
5
|
+
|
6
|
+
class Jat
|
7
|
+
module Plugins
|
8
|
+
module SimpleApiValidateParams
|
9
|
+
def self.before_load(jat_class, **_opts)
|
10
|
+
return if jat_class.plugin_used?(:simple_api)
|
11
|
+
raise Error, "Please load :simple_api plugin first"
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.load(jat_class, **_opts)
|
15
|
+
jat_class.extend(ClassMethods)
|
16
|
+
jat_class.include(InstanceMethods)
|
17
|
+
|
18
|
+
jat_class::FieldsParamParser.include(FieldsParamParserMethods)
|
19
|
+
end
|
20
|
+
|
21
|
+
module InstanceMethods
|
22
|
+
def validate
|
23
|
+
@validate ||= self.class.validate(context)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
module ClassMethods
|
28
|
+
def validate(context)
|
29
|
+
# Generate map for current context.
|
30
|
+
# Params are valid if no errors were raised
|
31
|
+
map(context)
|
32
|
+
true
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
module FieldsParamParserMethods
|
37
|
+
def parse
|
38
|
+
super.tap { |result| ValidateFieldsParam.call(self.class.jat_class, result) }
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
register_plugin(:simple_api_validate_params, SimpleApiValidateParams)
|
44
|
+
end
|
45
|
+
end
|
@@ -3,19 +3,25 @@
|
|
3
3
|
class Jat
|
4
4
|
module Plugins
|
5
5
|
module ToStr
|
6
|
+
def self.load(jat_class, **_opts)
|
7
|
+
jat_class.include(InstanceMethods)
|
8
|
+
jat_class.extend(ClassMethods)
|
9
|
+
end
|
10
|
+
|
6
11
|
def self.after_load(jat_class, **opts)
|
7
12
|
jat_class.config[:to_str] = opts[:to_str] || ->(data) { ToStrJSON.dump(data) }
|
8
13
|
end
|
9
14
|
|
10
15
|
module ClassMethods
|
11
|
-
def to_str(object, context =
|
12
|
-
new(
|
16
|
+
def to_str(object, context = nil)
|
17
|
+
new(context || FROZEN_EMPTY_HASH).to_str(object)
|
13
18
|
end
|
14
19
|
end
|
15
20
|
|
16
21
|
module InstanceMethods
|
17
|
-
def to_str
|
18
|
-
|
22
|
+
def to_str(object)
|
23
|
+
hash = to_h(object)
|
24
|
+
config[:to_str].call(hash)
|
19
25
|
end
|
20
26
|
end
|
21
27
|
|
data/lib/jat/plugins.rb
CHANGED
@@ -12,28 +12,15 @@ class Jat
|
|
12
12
|
@plugins[name] = mod
|
13
13
|
end
|
14
14
|
|
15
|
-
# If the registered plugin already exists, use it. Otherwise, require
|
15
|
+
# If the registered plugin already exists, use it. Otherwise, require
|
16
16
|
# and return it. This raises a LoadError if such a plugin doesn't exist,
|
17
17
|
# or a Jat::Error if it exists but it does not register itself
|
18
18
|
# correctly.
|
19
|
-
def self.
|
19
|
+
def self.find_plugin(name)
|
20
|
+
return name if name.is_a?(Module)
|
20
21
|
require "jat/plugins/#{name}/#{name}" unless @plugins.key?(name)
|
21
22
|
|
22
23
|
@plugins[name] || raise(Error, "plugin #{name} did not register itself correctly in Jat::Plugins")
|
23
24
|
end
|
24
|
-
|
25
|
-
# Delegate call to the plugin in a way that works across Ruby versions.
|
26
|
-
def self.before_load(plugin, jat_class, **opts)
|
27
|
-
return unless plugin.respond_to?(:before_load)
|
28
|
-
|
29
|
-
plugin.before_load(jat_class, **opts)
|
30
|
-
end
|
31
|
-
|
32
|
-
# Delegate call to the plugin in a way that works across Ruby versions.
|
33
|
-
def self.after_load(plugin, jat_class, **opts)
|
34
|
-
return unless plugin.respond_to?(:after_load)
|
35
|
-
|
36
|
-
plugin.after_load(jat_class, **opts)
|
37
|
-
end
|
38
25
|
end
|
39
26
|
end
|
data/lib/jat.rb
CHANGED
@@ -1,16 +1,18 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require_relative "jat/attribute"
|
4
|
-
require_relative "jat/presenter"
|
5
4
|
require_relative "jat/config"
|
6
5
|
require_relative "jat/plugins"
|
7
6
|
|
8
7
|
# Main namespace
|
9
8
|
class Jat
|
9
|
+
FROZEN_EMPTY_HASH = {}.freeze
|
10
|
+
FROZEN_EMPTY_ARRAY = [].freeze
|
11
|
+
|
10
12
|
# A generic exception used by Jat.
|
11
13
|
class Error < StandardError; end
|
12
14
|
|
13
|
-
@config = Config.new
|
15
|
+
@config = Config.new({plugins: []})
|
14
16
|
|
15
17
|
module ClassMethods
|
16
18
|
attr_reader :config
|
@@ -27,11 +29,6 @@ class Jat
|
|
27
29
|
attribute_class.jat_class = subclass
|
28
30
|
subclass.const_set(:Attribute, attribute_class)
|
29
31
|
|
30
|
-
# Initialize object presenter
|
31
|
-
presenter_class = Class.new(self::Presenter)
|
32
|
-
presenter_class.jat_class = subclass
|
33
|
-
subclass.const_set(:Presenter, presenter_class)
|
34
|
-
|
35
32
|
# Assign same attributes
|
36
33
|
attributes.each_value do |attribute|
|
37
34
|
params = attribute.params
|
@@ -41,37 +38,37 @@ class Jat
|
|
41
38
|
super
|
42
39
|
end
|
43
40
|
|
44
|
-
def plugin(
|
45
|
-
if
|
46
|
-
raise Error, "Plugin class must be a Symbol or a Module, #{plugin.inspect} given"
|
47
|
-
end
|
48
|
-
|
49
|
-
plugin = Plugins.load_plugin(plugin) if plugin.is_a?(Symbol)
|
41
|
+
def plugin(name, **opts)
|
42
|
+
return if plugin_used?(name)
|
50
43
|
|
51
|
-
Plugins.
|
44
|
+
plugin = Plugins.find_plugin(name)
|
52
45
|
|
53
|
-
|
54
|
-
|
46
|
+
# Before load usually used to check plugin can be attached or to load additional plugins
|
47
|
+
plugin.before_load(self, **opts) if plugin.respond_to?(:before_load)
|
55
48
|
|
56
|
-
|
57
|
-
self
|
49
|
+
# Usually used to include/extend plugin modules
|
50
|
+
plugin.load(self, **opts) if plugin.respond_to?(:load)
|
58
51
|
|
59
|
-
|
60
|
-
|
52
|
+
# Store attached plugins, so we can check them later
|
53
|
+
config[:plugins] << plugin
|
61
54
|
|
62
|
-
|
63
|
-
self
|
55
|
+
# Set some config options for current plugin or load additional plugins
|
56
|
+
plugin.after_load(self, **opts) if plugin.respond_to?(:after_load)
|
64
57
|
|
65
|
-
Plugins.after_load(plugin, self, **opts)
|
66
58
|
plugin
|
67
59
|
end
|
68
60
|
|
61
|
+
def plugin_used?(name)
|
62
|
+
plugin = Plugins.find_plugin(name)
|
63
|
+
config[:plugins].include?(plugin)
|
64
|
+
end
|
65
|
+
|
69
66
|
def call
|
70
67
|
self
|
71
68
|
end
|
72
69
|
|
73
70
|
def to_h(object, context = nil)
|
74
|
-
new(
|
71
|
+
new(context || {}).to_h(object)
|
75
72
|
end
|
76
73
|
|
77
74
|
def attributes
|
@@ -81,20 +78,21 @@ class Jat
|
|
81
78
|
def attribute(name, **opts, &block)
|
82
79
|
new_attr = self::Attribute.new(name: name, opts: opts, block: block)
|
83
80
|
attributes[new_attr.name] = new_attr
|
84
|
-
|
85
|
-
|
81
|
+
end
|
82
|
+
|
83
|
+
def relationship(name, serializer:, **opts, &block)
|
84
|
+
attribute(name, serializer: serializer, **opts, &block)
|
86
85
|
end
|
87
86
|
end
|
88
87
|
|
89
88
|
module InstanceMethods
|
90
|
-
attr_reader :
|
89
|
+
attr_reader :context
|
91
90
|
|
92
|
-
def initialize(
|
93
|
-
@object = object
|
91
|
+
def initialize(context = {})
|
94
92
|
@context = context
|
95
93
|
end
|
96
94
|
|
97
|
-
def to_h
|
95
|
+
def to_h(_object)
|
98
96
|
raise Error, "Method #to_h must be implemented by plugin"
|
99
97
|
end
|
100
98
|
|
@@ -127,16 +127,26 @@ describe Jat::Attribute do
|
|
127
127
|
|
128
128
|
describe "#block" do
|
129
129
|
it "returns provided block" do
|
130
|
-
block =
|
130
|
+
block = proc { |object, context| [object, context] }
|
131
131
|
attribute = attribute_class.new(name: "foo", block: block)
|
132
|
-
assert_equal
|
132
|
+
assert_equal ["OBJECT", "CONTEXT"], attribute.value("OBJECT", "CONTEXT")
|
133
|
+
end
|
134
|
+
|
135
|
+
it "returns provided block when block was without params" do
|
136
|
+
block = proc { "RESULT" }
|
137
|
+
attribute = attribute_class.new(name: "foo", block: block)
|
138
|
+
assert_equal "RESULT", attribute.value("OBJECT", "CONTEXT")
|
139
|
+
end
|
140
|
+
|
141
|
+
it "returns provided block when block was with one param" do
|
142
|
+
block = proc { |object| object }
|
143
|
+
attribute = attribute_class.new(name: "foo", block: block)
|
144
|
+
assert_equal "OBJECT", attribute.value("OBJECT", "CONTEXT")
|
133
145
|
end
|
134
146
|
|
135
147
|
it "constructs block that calls current key method on object" do
|
136
148
|
attribute = attribute_class.new(name: "foo", opts: {key: :length})
|
137
|
-
|
138
|
-
|
139
|
-
assert_equal 3, presenter.instance_exec(&attribute.block)
|
149
|
+
assert_equal 3, attribute.value([1, 2, 3], nil)
|
140
150
|
end
|
141
151
|
end
|
142
152
|
end
|
@@ -3,38 +3,57 @@
|
|
3
3
|
require "test_helper"
|
4
4
|
|
5
5
|
describe "Jat::Plugins::ActiverecordPreloads" do
|
6
|
-
let(:jat_class)
|
7
|
-
|
6
|
+
let(:jat_class) do
|
7
|
+
api_test = api_test()
|
8
|
+
|
9
|
+
Class.new(Jat) do
|
10
|
+
include api_test
|
11
|
+
plugin(:_activerecord_preloads)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
let(:api_test) do
|
16
|
+
Module.new do
|
17
|
+
def to_h(object)
|
18
|
+
object
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
8
22
|
|
9
23
|
describe "InstanceMethods" do
|
10
|
-
it "adds preloads to object
|
11
|
-
|
24
|
+
it "adds preloads to object when calling to_h" do
|
25
|
+
object = "OBJ"
|
12
26
|
preloads = "PRELOADS"
|
13
|
-
jat_class.
|
14
|
-
|
27
|
+
jat = jat_class.new
|
28
|
+
jat.expects(:preloads).returns(preloads)
|
29
|
+
|
30
|
+
Jat::Plugins::ActiverecordPreloads::Preloader
|
31
|
+
.expects(:preload)
|
32
|
+
.with(object, preloads).returns("OBJ_WITH_PRELOADS")
|
15
33
|
|
16
|
-
jat
|
17
|
-
assert_equal("OBJ_WITH_PRELOADS", jat.object)
|
34
|
+
assert_equal("OBJ_WITH_PRELOADS", jat.to_h(object))
|
18
35
|
end
|
19
36
|
|
20
37
|
it "skips preloadings for nil" do
|
21
38
|
object = nil
|
22
|
-
jat = jat_class.new
|
23
|
-
|
39
|
+
jat = jat_class.new
|
40
|
+
|
41
|
+
assert_same object, jat.to_h(object)
|
24
42
|
end
|
25
43
|
|
26
44
|
it "skips preloadings for empty array" do
|
27
45
|
object = []
|
28
|
-
jat = jat_class.new
|
29
|
-
|
46
|
+
jat = jat_class.new
|
47
|
+
|
48
|
+
assert_same object, jat.to_h(object)
|
30
49
|
end
|
31
50
|
|
32
51
|
it "skips preloadings when nothing to preload" do
|
33
52
|
object = "OBJECT"
|
34
|
-
jat_class.
|
53
|
+
jat = jat_class.new
|
54
|
+
jat.expects(:preloads).returns({})
|
35
55
|
|
36
|
-
|
37
|
-
assert_same object, jat.object
|
56
|
+
assert_same object, jat.to_h(object)
|
38
57
|
end
|
39
58
|
end
|
40
59
|
end
|
@@ -4,7 +4,7 @@ require "test_helper"
|
|
4
4
|
require "support/activerecord"
|
5
5
|
|
6
6
|
describe "Jat::Plugins::ActiverecordPreloads" do
|
7
|
-
before { Jat::Plugins.
|
7
|
+
before { Jat::Plugins.find_plugin(:_activerecord_preloads) }
|
8
8
|
|
9
9
|
describe "Preloader" do
|
10
10
|
let(:described_class) { plugin::Preloader }
|
@@ -52,18 +52,17 @@ describe "Jat::Plugins::ActiverecordPreloads" do
|
|
52
52
|
assert_equal true, user.association(:comments).loaded?
|
53
53
|
end
|
54
54
|
|
55
|
-
it "
|
55
|
+
it "preloads data to activerecord array" do
|
56
56
|
user = AR::User.create!
|
57
|
-
comment = user.comments.create!
|
58
|
-
user.comments.to_a && comment.delete # preload comments manually and delete comment
|
59
57
|
|
60
|
-
|
58
|
+
users = [user]
|
59
|
+
result = described_class.preload(users, {comments: {}})
|
61
60
|
|
62
|
-
|
63
|
-
assert_equal []
|
61
|
+
assert_same result, users
|
62
|
+
assert_equal true, result[0].association(:comments).loaded?
|
64
63
|
end
|
65
64
|
|
66
|
-
it "preloads data to activerecord relation
|
65
|
+
it "preloads data to activerecord relation" do
|
67
66
|
user = AR::User.create!
|
68
67
|
|
69
68
|
result = described_class.preload(AR::User.where(id: user.id), {comments: {}})
|
@@ -72,26 +71,13 @@ describe "Jat::Plugins::ActiverecordPreloads" do
|
|
72
71
|
assert_equal true, result[0].association(:comments).loaded?
|
73
72
|
end
|
74
73
|
|
75
|
-
it "preloads data to activerecord
|
74
|
+
it "preloads data to loaded activerecord relation" do
|
76
75
|
user = AR::User.create!
|
77
76
|
|
78
|
-
|
79
|
-
result = described_class.preload(users, {comments: {}})
|
80
|
-
|
81
|
-
assert_same result, users
|
82
|
-
assert_equal true, result[0].association(:comments).loaded?
|
83
|
-
end
|
84
|
-
|
85
|
-
it "resets activerecord array preloaded relations" do
|
86
|
-
user = AR::User.create!
|
87
|
-
comment = user.comments.create!
|
88
|
-
user.comments.to_a && comment.delete # preload comments manually and delete comment
|
89
|
-
|
90
|
-
users = [user]
|
91
|
-
result = described_class.preload(users, {comments: {}})
|
77
|
+
result = described_class.preload(AR::User.where(id: user.id).load, {comments: {}})
|
92
78
|
|
79
|
+
assert_equal result, [user]
|
93
80
|
assert_equal true, result[0].association(:comments).loaded?
|
94
|
-
assert_equal [], result[0].comments
|
95
81
|
end
|
96
82
|
end
|
97
83
|
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "test_helper"
|
4
|
+
|
5
|
+
describe "Jat::Plugins::LowerCamelCase" do
|
6
|
+
let(:jat_class) do
|
7
|
+
new_class = Class.new(Jat)
|
8
|
+
new_class.plugin(:_lower_camel_case)
|
9
|
+
new_class
|
10
|
+
end
|
11
|
+
|
12
|
+
describe "Attribute" do
|
13
|
+
describe "#name" do
|
14
|
+
it "returns name in lower_camel_case case" do
|
15
|
+
attribute = jat_class.attribute(:foo)
|
16
|
+
assert :foo, attribute.name
|
17
|
+
|
18
|
+
attribute = jat_class.attribute(:foo_bar)
|
19
|
+
assert :fooBar, attribute.name
|
20
|
+
|
21
|
+
attribute = jat_class.attribute(:foo_bar_bazz)
|
22
|
+
assert :fooBarBazz, attribute.name
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -6,7 +6,7 @@ describe "Jat::Plugins::Cache" do
|
|
6
6
|
let(:jat_class) do
|
7
7
|
new_class = Class.new(Jat)
|
8
8
|
to_h_mod = Module.new do
|
9
|
-
def to_h
|
9
|
+
def to_h(object)
|
10
10
|
"RES_#{object}"
|
11
11
|
end
|
12
12
|
end
|
@@ -28,8 +28,8 @@ describe "Jat::Plugins::Cache" do
|
|
28
28
|
describe "InstanceMethods" do
|
29
29
|
describe "#to_h" do
|
30
30
|
it "takes result from cache" do
|
31
|
-
result1 = jat_class.new("OBJECT"
|
32
|
-
result2 = jat_class.new("OBJECT"
|
31
|
+
result1 = jat_class.new(context).to_h("OBJECT")
|
32
|
+
result2 = jat_class.new(context).to_h("OBJECT")
|
33
33
|
|
34
34
|
assert_equal "RES_OBJECT", result1
|
35
35
|
assert_equal "RES_OBJECT", result2
|
@@ -37,8 +37,8 @@ describe "Jat::Plugins::Cache" do
|
|
37
37
|
end
|
38
38
|
|
39
39
|
it "does not take cached result when cache keys are different" do
|
40
|
-
result1 = jat_class.new("OBJECT"
|
41
|
-
result2 = jat_class.new(
|
40
|
+
result1 = jat_class.new(context).to_h("OBJECT")
|
41
|
+
result2 = jat_class.new(context.merge(foo: :bazz)).to_h("OBJECT")
|
42
42
|
|
43
43
|
assert_equal "RES_OBJECT", result1
|
44
44
|
assert_equal "RES_OBJECT", result2
|
@@ -46,7 +46,7 @@ describe "Jat::Plugins::Cache" do
|
|
46
46
|
end
|
47
47
|
|
48
48
|
it "does not saves cache when no context[:cache] provided" do
|
49
|
-
jat_class.new(
|
49
|
+
jat_class.new({}).to_str("OBJECT")
|
50
50
|
|
51
51
|
assert_equal [], hash_storage.keys
|
52
52
|
end
|
@@ -54,8 +54,8 @@ describe "Jat::Plugins::Cache" do
|
|
54
54
|
|
55
55
|
describe "#to_str" do
|
56
56
|
it "takes result from cache" do
|
57
|
-
result1 = jat_class.new("OBJECT"
|
58
|
-
result2 = jat_class.new("OBJECT"
|
57
|
+
result1 = jat_class.new(context).to_str("OBJECT")
|
58
|
+
result2 = jat_class.new(context).to_str("OBJECT")
|
59
59
|
|
60
60
|
assert_equal '"RES_OBJECT"', result1
|
61
61
|
assert_equal '"RES_OBJECT"', result2
|
@@ -63,8 +63,8 @@ describe "Jat::Plugins::Cache" do
|
|
63
63
|
end
|
64
64
|
|
65
65
|
it "does not take cached result when cache keys are different" do
|
66
|
-
result1 = jat_class.new("OBJECT"
|
67
|
-
result2 = jat_class.new(
|
66
|
+
result1 = jat_class.new(context).to_str("OBJECT")
|
67
|
+
result2 = jat_class.new(context.merge(foo: :bazz)).to_str("OBJECT")
|
68
68
|
|
69
69
|
assert_equal '"RES_OBJECT"', result1
|
70
70
|
assert_equal '"RES_OBJECT"', result2
|
@@ -73,7 +73,7 @@ describe "Jat::Plugins::Cache" do
|
|
73
73
|
|
74
74
|
it "does not saves cache for #to_h" do
|
75
75
|
context[:foo] = :bar
|
76
|
-
jat_class.new("OBJECT"
|
76
|
+
jat_class.new(context).to_str("OBJECT")
|
77
77
|
|
78
78
|
assert_equal ["OBJECT.bar.to_str"], hash_storage.keys
|
79
79
|
end
|