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
@@ -1,18 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
class Jat
|
4
|
-
module Plugins
|
5
|
-
module CamelLower
|
6
|
-
module AttributeMethods
|
7
|
-
def name
|
8
|
-
first_word, *other = original_name.to_s.split("_")
|
9
|
-
last_words = other.map!(&:capitalize).join
|
10
|
-
|
11
|
-
:"#{first_word}#{last_words}"
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
register_plugin(:camel_lower, CamelLower)
|
17
|
-
end
|
18
|
-
end
|
@@ -1,91 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
##
|
4
|
-
# Combines all serializer and its realtions exposed fields into one hash.
|
5
|
-
# Returns Hash
|
6
|
-
# {
|
7
|
-
# type1 => {
|
8
|
-
# serializer: ser1,
|
9
|
-
# attributes: [attr1, attr2, ...],
|
10
|
-
# relationships: [attr1, attr2, ...]
|
11
|
-
# },
|
12
|
-
#
|
13
|
-
class Jat
|
14
|
-
module Plugins
|
15
|
-
module JsonApi
|
16
|
-
class ConstructTraversalMap
|
17
|
-
attr_reader :jat_class, :result, :exposed, :manually_exposed
|
18
|
-
|
19
|
-
EXPOSED_VALUES = {all: :all, exposed: :exposed, manual: :manual}.freeze
|
20
|
-
|
21
|
-
def initialize(jat_class, exposed, manually_exposed: {})
|
22
|
-
@jat_class = jat_class
|
23
|
-
@exposed = EXPOSED_VALUES.fetch(exposed)
|
24
|
-
@manually_exposed = manually_exposed
|
25
|
-
end
|
26
|
-
|
27
|
-
def to_h
|
28
|
-
@result = {}
|
29
|
-
append(jat_class)
|
30
|
-
result
|
31
|
-
end
|
32
|
-
|
33
|
-
private
|
34
|
-
|
35
|
-
def append(jat_class)
|
36
|
-
type = jat_class.type
|
37
|
-
return result if result.key?(type)
|
38
|
-
|
39
|
-
result[type] = {
|
40
|
-
serializer: jat_class,
|
41
|
-
attributes: [],
|
42
|
-
relationships: []
|
43
|
-
}
|
44
|
-
|
45
|
-
fill(jat_class)
|
46
|
-
end
|
47
|
-
|
48
|
-
def fill(jat_class)
|
49
|
-
type = jat_class.type
|
50
|
-
type_result = result[type]
|
51
|
-
|
52
|
-
jat_class.attributes.each_value do |attribute|
|
53
|
-
next unless expose?(type, attribute)
|
54
|
-
|
55
|
-
fill_attr(type_result, attribute)
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
def fill_attr(type_result, attribute)
|
60
|
-
name = attribute.name
|
61
|
-
|
62
|
-
if attribute.relation?
|
63
|
-
type_result[:relationships] << name
|
64
|
-
append(attribute.serializer.call)
|
65
|
-
else
|
66
|
-
type_result[:attributes] << name
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
def expose?(type, attribute)
|
71
|
-
return false if attribute.name == :id
|
72
|
-
|
73
|
-
case exposed
|
74
|
-
when :all then true
|
75
|
-
when :manual then manually_exposed?(type, attribute)
|
76
|
-
else attribute.exposed? || manually_exposed?(type, attribute)
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
# Return `attribute.exposed?` when type is not provided
|
81
|
-
# Checks type is in exposed attributes
|
82
|
-
def manually_exposed?(type, attribute)
|
83
|
-
return attribute.exposed? unless manually_exposed.key?(type)
|
84
|
-
|
85
|
-
exposed_attrs = manually_exposed[type]
|
86
|
-
exposed_attrs.include?(attribute.name)
|
87
|
-
end
|
88
|
-
end
|
89
|
-
end
|
90
|
-
end
|
91
|
-
end
|
@@ -1,48 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
class Jat
|
4
|
-
module Presenters
|
5
|
-
class DocumentLinksPresenter
|
6
|
-
module InstanceMethods
|
7
|
-
# Presented object
|
8
|
-
attr_reader :object
|
9
|
-
|
10
|
-
# Presented context
|
11
|
-
attr_reader :context
|
12
|
-
|
13
|
-
def initialize(object, context)
|
14
|
-
@object = object
|
15
|
-
@context = context
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
module ClassMethods
|
20
|
-
attr_accessor :jat_class
|
21
|
-
|
22
|
-
def inspect
|
23
|
-
"#{jat_class.inspect}::Presenters::DocumentLinksPresenter"
|
24
|
-
end
|
25
|
-
|
26
|
-
def add_method(name, block)
|
27
|
-
# Warning-free method redefinition
|
28
|
-
remove_method(name) if method_defined?(name, false)
|
29
|
-
define_method(name, &block_without_args(block))
|
30
|
-
end
|
31
|
-
|
32
|
-
private
|
33
|
-
|
34
|
-
def block_without_args(block)
|
35
|
-
case block.parameters.count
|
36
|
-
when 0 then block
|
37
|
-
when 1 then -> { instance_exec(object, &block) }
|
38
|
-
when 2 then -> { instance_exec(object, context, &block) }
|
39
|
-
else raise Error, "Invalid block arguments count"
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
extend ClassMethods
|
45
|
-
include InstanceMethods
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
@@ -1,48 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
class Jat
|
4
|
-
module Presenters
|
5
|
-
class DocumentMetaPresenter
|
6
|
-
module InstanceMethods
|
7
|
-
# Presented object
|
8
|
-
attr_reader :object
|
9
|
-
|
10
|
-
# Presented context
|
11
|
-
attr_reader :context
|
12
|
-
|
13
|
-
def initialize(object, context)
|
14
|
-
@object = object
|
15
|
-
@context = context
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
module ClassMethods
|
20
|
-
attr_accessor :jat_class
|
21
|
-
|
22
|
-
def inspect
|
23
|
-
"#{jat_class.inspect}::Presenters::DocumentMetaPresenter"
|
24
|
-
end
|
25
|
-
|
26
|
-
def add_method(name, block)
|
27
|
-
# Warning-free method redefinition
|
28
|
-
remove_method(name) if method_defined?(name, false)
|
29
|
-
define_method(name, &block_without_args(block))
|
30
|
-
end
|
31
|
-
|
32
|
-
private
|
33
|
-
|
34
|
-
def block_without_args(block)
|
35
|
-
case block.parameters.count
|
36
|
-
when 0 then block
|
37
|
-
when 1 then -> { instance_exec(object, &block) }
|
38
|
-
when 2 then -> { instance_exec(object, context, &block) }
|
39
|
-
else raise Error, "Invalid block arguments count"
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
extend ClassMethods
|
45
|
-
include InstanceMethods
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
@@ -1,48 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
class Jat
|
4
|
-
module Presenters
|
5
|
-
class JsonapiPresenter
|
6
|
-
module InstanceMethods
|
7
|
-
# Presented object
|
8
|
-
attr_reader :object
|
9
|
-
|
10
|
-
# Presented context
|
11
|
-
attr_reader :context
|
12
|
-
|
13
|
-
def initialize(object, context)
|
14
|
-
@object = object
|
15
|
-
@context = context
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
module ClassMethods
|
20
|
-
attr_accessor :jat_class
|
21
|
-
|
22
|
-
def inspect
|
23
|
-
"#{jat_class.inspect}::Presenters::JsonapiPresenter"
|
24
|
-
end
|
25
|
-
|
26
|
-
def add_method(name, block)
|
27
|
-
# Warning-free method redefinition
|
28
|
-
remove_method(name) if method_defined?(name, false)
|
29
|
-
define_method(name, &block_without_args(block))
|
30
|
-
end
|
31
|
-
|
32
|
-
private
|
33
|
-
|
34
|
-
def block_without_args(block)
|
35
|
-
case block.parameters.count
|
36
|
-
when 0 then block
|
37
|
-
when 1 then -> { instance_exec(object, &block) }
|
38
|
-
when 2 then -> { instance_exec(object, context, &block) }
|
39
|
-
else raise Error, "Invalid block arguments count"
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
extend ClassMethods
|
45
|
-
include InstanceMethods
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
@@ -1,48 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
class Jat
|
4
|
-
module Presenters
|
5
|
-
class LinksPresenter
|
6
|
-
module InstanceMethods
|
7
|
-
# Presented object
|
8
|
-
attr_reader :object
|
9
|
-
|
10
|
-
# Presented context
|
11
|
-
attr_reader :context
|
12
|
-
|
13
|
-
def initialize(object, context)
|
14
|
-
@object = object
|
15
|
-
@context = context
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
module ClassMethods
|
20
|
-
attr_accessor :jat_class
|
21
|
-
|
22
|
-
def inspect
|
23
|
-
"#{jat_class.inspect}::Presenters::LinksPresenter"
|
24
|
-
end
|
25
|
-
|
26
|
-
def add_method(name, block)
|
27
|
-
# Warning-free method redefinition
|
28
|
-
remove_method(name) if method_defined?(name, false)
|
29
|
-
define_method(name, &block_without_args(block))
|
30
|
-
end
|
31
|
-
|
32
|
-
private
|
33
|
-
|
34
|
-
def block_without_args(block)
|
35
|
-
case block.parameters.count
|
36
|
-
when 0 then block
|
37
|
-
when 1 then -> { instance_exec(object, &block) }
|
38
|
-
when 2 then -> { instance_exec(object, context, &block) }
|
39
|
-
else raise Error, "Invalid block arguments count"
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
extend ClassMethods
|
45
|
-
include InstanceMethods
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
@@ -1,48 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
class Jat
|
4
|
-
module Presenters
|
5
|
-
class MetaPresenter
|
6
|
-
module InstanceMethods
|
7
|
-
# Presented object
|
8
|
-
attr_reader :object
|
9
|
-
|
10
|
-
# Presented context
|
11
|
-
attr_reader :context
|
12
|
-
|
13
|
-
def initialize(object, context)
|
14
|
-
@object = object
|
15
|
-
@context = context
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
module ClassMethods
|
20
|
-
attr_accessor :jat_class
|
21
|
-
|
22
|
-
def inspect
|
23
|
-
"#{jat_class.inspect}::Presenters::MetaPresenter"
|
24
|
-
end
|
25
|
-
|
26
|
-
def add_method(name, block)
|
27
|
-
# Warning-free method redefinition
|
28
|
-
remove_method(name) if method_defined?(name, false)
|
29
|
-
define_method(name, &block_without_args(block))
|
30
|
-
end
|
31
|
-
|
32
|
-
private
|
33
|
-
|
34
|
-
def block_without_args(block)
|
35
|
-
case block.parameters.count
|
36
|
-
when 0 then block
|
37
|
-
when 1 then -> { instance_exec(object, &block) }
|
38
|
-
when 2 then -> { instance_exec(object, context, &block) }
|
39
|
-
else raise Error, "Invalid block arguments count"
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
extend ClassMethods
|
45
|
-
include InstanceMethods
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
@@ -1,53 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
class Jat
|
4
|
-
module Presenters
|
5
|
-
class RelationshipLinksPresenter
|
6
|
-
module InstanceMethods
|
7
|
-
# Relationship parent object
|
8
|
-
attr_reader :parent_object
|
9
|
-
|
10
|
-
# Presented relationship
|
11
|
-
attr_reader :object
|
12
|
-
|
13
|
-
# Presented context
|
14
|
-
attr_reader :context
|
15
|
-
|
16
|
-
def initialize(parent_object, object, context)
|
17
|
-
@parent_object = parent_object
|
18
|
-
@object = object
|
19
|
-
@context = context
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
module ClassMethods
|
24
|
-
attr_accessor :jat_class
|
25
|
-
|
26
|
-
def inspect
|
27
|
-
"#{jat_class.inspect}::Presenters::RelationshipLinksPresenter"
|
28
|
-
end
|
29
|
-
|
30
|
-
def add_method(name, block)
|
31
|
-
# Warning-free method redefinition
|
32
|
-
remove_method(name) if method_defined?(name, false)
|
33
|
-
define_method(name, &block_without_args(block))
|
34
|
-
end
|
35
|
-
|
36
|
-
private
|
37
|
-
|
38
|
-
def block_without_args(block)
|
39
|
-
case block.parameters.count
|
40
|
-
when 0 then block
|
41
|
-
when 1 then -> { instance_exec(parent_object, &block) }
|
42
|
-
when 2 then -> { instance_exec(parent_object, object, &block) }
|
43
|
-
when 3 then -> { instance_exec(parent_object, object, context, &block) }
|
44
|
-
else raise Error, "Invalid block arguments count"
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
extend ClassMethods
|
50
|
-
include InstanceMethods
|
51
|
-
end
|
52
|
-
end
|
53
|
-
end
|
@@ -1,53 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
class Jat
|
4
|
-
module Presenters
|
5
|
-
class RelationshipMetaPresenter
|
6
|
-
module InstanceMethods
|
7
|
-
# Relationship parent object
|
8
|
-
attr_reader :parent_object
|
9
|
-
|
10
|
-
# Presented relationship
|
11
|
-
attr_reader :object
|
12
|
-
|
13
|
-
# Presented context
|
14
|
-
attr_reader :context
|
15
|
-
|
16
|
-
def initialize(parent_object, object, context)
|
17
|
-
@parent_object = parent_object
|
18
|
-
@object = object
|
19
|
-
@context = context
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
module ClassMethods
|
24
|
-
attr_accessor :jat_class
|
25
|
-
|
26
|
-
def inspect
|
27
|
-
"#{jat_class.inspect}::Presenters::RelationshipMetaPresenter"
|
28
|
-
end
|
29
|
-
|
30
|
-
def add_method(name, block)
|
31
|
-
# Warning-free method redefinition
|
32
|
-
remove_method(name) if method_defined?(name, false)
|
33
|
-
define_method(name, &block_without_args(block))
|
34
|
-
end
|
35
|
-
|
36
|
-
private
|
37
|
-
|
38
|
-
def block_without_args(block)
|
39
|
-
case block.parameters.count
|
40
|
-
when 0 then block
|
41
|
-
when 1 then -> { instance_exec(parent_object, &block) }
|
42
|
-
when 2 then -> { instance_exec(parent_object, object, &block) }
|
43
|
-
when 3 then -> { instance_exec(parent_object, object, context, &block) }
|
44
|
-
else raise Error, "Invalid block arguments count"
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
extend ClassMethods
|
50
|
-
include InstanceMethods
|
51
|
-
end
|
52
|
-
end
|
53
|
-
end
|
@@ -1,34 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative "./construct_traversal_map"
|
4
|
-
require_relative "./map"
|
5
|
-
|
6
|
-
class Jat
|
7
|
-
module Plugins
|
8
|
-
module JsonApi
|
9
|
-
class TraversalMap
|
10
|
-
module InstanceMethods
|
11
|
-
attr_reader :jat
|
12
|
-
|
13
|
-
def initialize(jat)
|
14
|
-
@jat = jat
|
15
|
-
end
|
16
|
-
|
17
|
-
def full
|
18
|
-
@full ||= ConstructTraversalMap.new(jat.class, :all).to_h
|
19
|
-
end
|
20
|
-
|
21
|
-
def exposed
|
22
|
-
@exposed ||= ConstructTraversalMap.new(jat.class, :exposed).to_h
|
23
|
-
end
|
24
|
-
|
25
|
-
def current
|
26
|
-
@current ||= Map.call(jat)
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
include InstanceMethods
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
@@ -1,45 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
# Combines all serializer and its relations exposed fields into one hash.
|
4
|
-
class Jat
|
5
|
-
module Plugins
|
6
|
-
module SimpleApi
|
7
|
-
class ConstructTraversalMap
|
8
|
-
attr_reader :jat_class, :exposed, :manually_exposed
|
9
|
-
|
10
|
-
def initialize(jat_class, exposed, manually_exposed: nil)
|
11
|
-
@jat_class = jat_class
|
12
|
-
@exposed = exposed.to_sym
|
13
|
-
@manually_exposed = manually_exposed || {}
|
14
|
-
end
|
15
|
-
|
16
|
-
def to_h
|
17
|
-
jat_class.attributes.each_with_object({}) do |(name, attribute), result|
|
18
|
-
next unless expose?(attribute)
|
19
|
-
|
20
|
-
result[name] =
|
21
|
-
if attribute.relation?
|
22
|
-
self.class.new(attribute.serializer.call, exposed, manually_exposed: manually_exposed[name]).to_h
|
23
|
-
else
|
24
|
-
{}
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
private
|
30
|
-
|
31
|
-
def expose?(attribute)
|
32
|
-
case exposed
|
33
|
-
when :all then true
|
34
|
-
when :none then manually_exposed?(attribute)
|
35
|
-
else attribute.exposed? || manually_exposed?(attribute)
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
def manually_exposed?(attribute)
|
40
|
-
manually_exposed.include?(attribute.name)
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
@@ -1,68 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
class Jat
|
4
|
-
module Plugins
|
5
|
-
module SimpleApi
|
6
|
-
module Params
|
7
|
-
class Parse
|
8
|
-
module InstanceMethods
|
9
|
-
COMMA = ","
|
10
|
-
OPEN_BRACKET = "("
|
11
|
-
CLOSE_BRACKET = ")"
|
12
|
-
|
13
|
-
def initialize(fields)
|
14
|
-
@fields = fields
|
15
|
-
@map = []
|
16
|
-
@res = {}
|
17
|
-
end
|
18
|
-
|
19
|
-
# user => { user: {} }
|
20
|
-
# user(id) => { user: { id: true } }
|
21
|
-
# user(id, name) => { user: { id: true, name: true } }
|
22
|
-
# user, comments => { user: {}, comments: {} }
|
23
|
-
# user(comments(text)) => { user: {}, comments: {} }
|
24
|
-
def parse
|
25
|
-
return {} unless fields
|
26
|
-
|
27
|
-
current_attr = nil
|
28
|
-
|
29
|
-
fields.each_char do |char|
|
30
|
-
case char
|
31
|
-
when COMMA
|
32
|
-
add_attribute(current_attr)
|
33
|
-
current_attr = nil
|
34
|
-
when CLOSE_BRACKET
|
35
|
-
add_attribute(current_attr)
|
36
|
-
map.pop
|
37
|
-
current_attr = nil
|
38
|
-
when OPEN_BRACKET
|
39
|
-
add_attribute(current_attr)
|
40
|
-
map << current_attr.to_sym if current_attr
|
41
|
-
current_attr = nil
|
42
|
-
else
|
43
|
-
current_attr = current_attr ? current_attr.insert(-1, char) : char
|
44
|
-
end
|
45
|
-
end
|
46
|
-
add_attribute(current_attr)
|
47
|
-
|
48
|
-
res
|
49
|
-
end
|
50
|
-
|
51
|
-
private
|
52
|
-
|
53
|
-
attr_reader :fields, :map, :res
|
54
|
-
|
55
|
-
def add_attribute(current_attr)
|
56
|
-
return unless current_attr
|
57
|
-
|
58
|
-
current_resource = map.empty? ? res : res.dig(*map)
|
59
|
-
current_resource[current_attr.to_sym] = {}
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
include InstanceMethods
|
64
|
-
end
|
65
|
-
end
|
66
|
-
end
|
67
|
-
end
|
68
|
-
end
|
data/lib/jat/presenter.rb
DELETED
@@ -1,51 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
class Jat
|
4
|
-
# Core class that encapsulates attributes presentation logic
|
5
|
-
class Presenter
|
6
|
-
module InstanceMethods
|
7
|
-
# Presented object
|
8
|
-
attr_reader :object
|
9
|
-
|
10
|
-
# Presented context
|
11
|
-
attr_reader :context
|
12
|
-
|
13
|
-
def initialize(object, context)
|
14
|
-
@object = object
|
15
|
-
@context = context
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
module ClassMethods
|
20
|
-
# Returns the Jat class that this presenter class is namespaced under.
|
21
|
-
attr_accessor :jat_class
|
22
|
-
|
23
|
-
# Since Presenter is anonymously subclassed when Jat is subclassed,
|
24
|
-
# and then assigned to a constant of the Jat subclass, make inspect
|
25
|
-
# reflect the likely name for the class.
|
26
|
-
def inspect
|
27
|
-
"#{jat_class.inspect}::Presenter"
|
28
|
-
end
|
29
|
-
|
30
|
-
def add_method(name, block)
|
31
|
-
# Warning-free method redefinition
|
32
|
-
remove_method(name) if method_defined?(name, false)
|
33
|
-
define_method(name, &block_without_args(block))
|
34
|
-
end
|
35
|
-
|
36
|
-
private
|
37
|
-
|
38
|
-
def block_without_args(block)
|
39
|
-
case block.parameters.count
|
40
|
-
when 0 then block
|
41
|
-
when 1 then -> { instance_exec(object, &block) }
|
42
|
-
when 2 then -> { instance_exec(object, context, &block) }
|
43
|
-
else raise Error, "Invalid block arguments count"
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
extend ClassMethods
|
49
|
-
include InstanceMethods
|
50
|
-
end
|
51
|
-
end
|
@@ -1,40 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require "test_helper"
|
4
|
-
|
5
|
-
describe "Jat::Plugins::ActiverecordPreloads" do
|
6
|
-
let(:jat_class) { Class.new(Jat) { plugin(:_activerecord_preloads) } }
|
7
|
-
let(:preloader) { Jat::Plugins::ActiverecordPreloads::Preloader }
|
8
|
-
|
9
|
-
describe "InstanceMethods" do
|
10
|
-
it "adds preloads to object in initialize call" do
|
11
|
-
obj = "OBJ"
|
12
|
-
preloads = "PRELOADS"
|
13
|
-
jat_class.expects(:jat_preloads).with(kind_of(jat_class)).returns(preloads)
|
14
|
-
preloader.expects(:preload).with(obj, preloads).returns("OBJ_WITH_PRELOADS")
|
15
|
-
|
16
|
-
jat = jat_class.new(obj, {})
|
17
|
-
assert_equal("OBJ_WITH_PRELOADS", jat.object)
|
18
|
-
end
|
19
|
-
|
20
|
-
it "skips preloadings for nil" do
|
21
|
-
object = nil
|
22
|
-
jat = jat_class.new(object, {})
|
23
|
-
assert_same object, jat.object
|
24
|
-
end
|
25
|
-
|
26
|
-
it "skips preloadings for empty array" do
|
27
|
-
object = []
|
28
|
-
jat = jat_class.new(object, {})
|
29
|
-
assert_same object, jat.object
|
30
|
-
end
|
31
|
-
|
32
|
-
it "skips preloadings when nothing to preload" do
|
33
|
-
object = "OBJECT"
|
34
|
-
jat_class.expects(:jat_preloads).with(kind_of(jat_class)).returns({})
|
35
|
-
|
36
|
-
jat = jat_class.new(object, {})
|
37
|
-
assert_same object, jat.object
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|