jat 0.0.9 → 0.0.10
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 +1 -13
- data/lib/jat/config.rb +1 -13
- data/lib/jat/plugins/json_api/lib/fields_param_parser.rb +1 -10
- data/lib/jat/plugins/json_api/lib/include_param_parser.rb +1 -10
- data/lib/jat/plugins/json_api/lib/map.rb +1 -10
- data/lib/jat/plugins/json_api/lib/response.rb +1 -10
- data/lib/jat/plugins/json_api/lib/response_piece.rb +1 -10
- data/lib/jat/plugins/presenter/presenter.rb +79 -0
- data/lib/jat/plugins/simple_api/lib/fields_param_parser.rb +1 -10
- data/lib/jat/plugins/simple_api/lib/map.rb +1 -10
- data/lib/jat/plugins/simple_api/lib/response.rb +1 -10
- data/lib/jat/plugins/simple_api/lib/response_piece.rb +1 -10
- data/lib/jat/utils/jat_class.rb +21 -0
- data/lib/jat.rb +1 -0
- data/test/lib/jat/plugins/presenter/presenter_test.rb +215 -0
- data/test/lib/jat/plugins_test.rb +1 -0
- data/test/test_helper.rb +6 -0
- metadata +6 -6
- data/test/lib/plugin_test.rb +0 -49
- data/test/test_plugin.rb +0 -56
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d7b5ab9fe35df564d6282bd57f3008293bbc38e72c7dbf0ade8ef27734aafb10
|
4
|
+
data.tar.gz: c2bd6717c5555e862bdaafab2bdd16f510d7535be1922e0327253fee7d8cb248
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2979f947fc8b2052632549e92990ef0f9039c582915cd405d97954f2a7aa6d7c6bf88627aea7e708f8417107a89e2ea089dfd3f0005425147cca297eb138e3f0
|
7
|
+
data.tar.gz: f90de936bc75392e315c4e8c5e8bf3e191aacaca84dd7b0a3dd924d93976e753bccf31425cc9b8588c406dbde0a0690cf0f13788d1e7ffb5871dfdcb553152a0
|
data/lib/jat/attribute.rb
CHANGED
@@ -88,19 +88,7 @@ class Jat
|
|
88
88
|
end
|
89
89
|
end
|
90
90
|
|
91
|
-
|
92
|
-
# Returns the Jat class that this Attribute class is namespaced under.
|
93
|
-
attr_accessor :jat_class
|
94
|
-
|
95
|
-
# Since Attribute is anonymously subclassed when Jat is subclassed,
|
96
|
-
# and then assigned to a constant of the Jat subclass, make inspect
|
97
|
-
# reflect the likely name for the class.
|
98
|
-
def inspect
|
99
|
-
"#{jat_class.inspect}::Attribute"
|
100
|
-
end
|
101
|
-
end
|
102
|
-
|
103
|
-
extend ClassMethods
|
91
|
+
extend Jat::JatClass
|
104
92
|
include InstanceMethods
|
105
93
|
end
|
106
94
|
end
|
data/lib/jat/config.rb
CHANGED
@@ -17,19 +17,7 @@ class Jat
|
|
17
17
|
def_delegators :@opts, :[], :[]=, :fetch
|
18
18
|
end
|
19
19
|
|
20
|
-
module ClassMethods
|
21
|
-
# Returns the Jat class that this config class is namespaced under.
|
22
|
-
attr_accessor :jat_class
|
23
|
-
|
24
|
-
# Since Config is anonymously subclassed when Jat is subclassed,
|
25
|
-
# and then assigned to a constant of the Jat subclass, make inspect
|
26
|
-
# reflect the likely name for the class.
|
27
|
-
def inspect
|
28
|
-
"#{jat_class.inspect}::Config"
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
20
|
include InstanceMethods
|
33
|
-
extend
|
21
|
+
extend Jat::JatClass
|
34
22
|
end
|
35
23
|
end
|
@@ -7,16 +7,6 @@ class Jat
|
|
7
7
|
module ClassMethods
|
8
8
|
COMMA = ","
|
9
9
|
|
10
|
-
# Returns the Jat class that this FieldsParamParser class is namespaced under.
|
11
|
-
attr_accessor :jat_class
|
12
|
-
|
13
|
-
# Since FieldsParamParser is anonymously subclassed when Jat is subclassed,
|
14
|
-
# and then assigned to a constant of the Jat subclass, make inspect
|
15
|
-
# reflect the likely name for the class.
|
16
|
-
def inspect
|
17
|
-
"#{jat_class.inspect}::FieldsParamParser"
|
18
|
-
end
|
19
|
-
|
20
10
|
def parse(fields)
|
21
11
|
return FROZEN_EMPTY_HASH unless fields
|
22
12
|
|
@@ -33,6 +23,7 @@ class Jat
|
|
33
23
|
end
|
34
24
|
end
|
35
25
|
|
26
|
+
extend Jat::JatClass
|
36
27
|
extend ClassMethods
|
37
28
|
end
|
38
29
|
end
|
@@ -8,16 +8,6 @@ class Jat
|
|
8
8
|
COMMA = ","
|
9
9
|
DOT = "."
|
10
10
|
|
11
|
-
# Returns the Jat class that this IncludeParamParser class is namespaced under.
|
12
|
-
attr_accessor :jat_class
|
13
|
-
|
14
|
-
# Since IncludeParamParser is anonymously subclassed when Jat is subclassed,
|
15
|
-
# and then assigned to a constant of the Jat subclass, make inspect
|
16
|
-
# reflect the likely name for the class.
|
17
|
-
def inspect
|
18
|
-
"#{jat_class.inspect}::IncludeParamParser"
|
19
|
-
end
|
20
|
-
|
21
11
|
def parse(includes_string_param)
|
22
12
|
return {} unless includes_string_param
|
23
13
|
|
@@ -77,6 +67,7 @@ class Jat
|
|
77
67
|
end
|
78
68
|
end
|
79
69
|
|
70
|
+
extend Jat::JatClass
|
80
71
|
extend ClassMethods
|
81
72
|
end
|
82
73
|
end
|
@@ -5,16 +5,6 @@ class Jat
|
|
5
5
|
module JsonApi
|
6
6
|
class Map
|
7
7
|
module ClassMethods
|
8
|
-
# Returns the Jat class that this Map class is namespaced under.
|
9
|
-
attr_accessor :jat_class
|
10
|
-
|
11
|
-
# Since Map is anonymously subclassed when Jat is subclassed,
|
12
|
-
# and then assigned to a constant of the Jat subclass, make inspect
|
13
|
-
# reflect the likely name for the class.
|
14
|
-
def inspect
|
15
|
-
"#{jat_class.inspect}::Map"
|
16
|
-
end
|
17
|
-
|
18
8
|
# Returns structure like
|
19
9
|
# {
|
20
10
|
# type1 => {
|
@@ -110,6 +100,7 @@ class Jat
|
|
110
100
|
end
|
111
101
|
end
|
112
102
|
|
103
|
+
extend Jat::JatClass
|
113
104
|
extend ClassMethods
|
114
105
|
include InstanceMethods
|
115
106
|
end
|
@@ -6,16 +6,6 @@ class Jat
|
|
6
6
|
module JsonApi
|
7
7
|
class Response
|
8
8
|
module ClassMethods
|
9
|
-
# Returns the Jat class that this Response class is namespaced under.
|
10
|
-
attr_accessor :jat_class
|
11
|
-
|
12
|
-
# Since Response is anonymously subclassed when Jat is subclassed,
|
13
|
-
# and then assigned to a constant of the Jat subclass, make inspect
|
14
|
-
# reflect the likely name for the class.
|
15
|
-
def inspect
|
16
|
-
"#{jat_class.inspect}::Response"
|
17
|
-
end
|
18
|
-
|
19
9
|
def call(object, context)
|
20
10
|
new(object, context).to_h
|
21
11
|
end
|
@@ -115,6 +105,7 @@ class Jat
|
|
115
105
|
end
|
116
106
|
end
|
117
107
|
|
108
|
+
extend Jat::JatClass
|
118
109
|
extend ClassMethods
|
119
110
|
include InstanceMethods
|
120
111
|
end
|
@@ -5,16 +5,6 @@ class Jat
|
|
5
5
|
module JsonApi
|
6
6
|
class ResponsePiece
|
7
7
|
module ClassMethods
|
8
|
-
# Returns the Jat class that this ResponsePiece class is namespaced under.
|
9
|
-
attr_accessor :jat_class
|
10
|
-
|
11
|
-
# Since ResponsePiece is anonymously subclassed when Jat is subclassed,
|
12
|
-
# and then assigned to a constant of the Jat subclass, make inspect
|
13
|
-
# reflect the likely name for the class.
|
14
|
-
def inspect
|
15
|
-
"#{jat_class.inspect}::ResponsePiece"
|
16
|
-
end
|
17
|
-
|
18
8
|
def call(object, context, map, includes)
|
19
9
|
new(object, context, map, includes).to_h
|
20
10
|
end
|
@@ -167,6 +157,7 @@ class Jat
|
|
167
157
|
end
|
168
158
|
end
|
169
159
|
|
160
|
+
extend Jat::JatClass
|
170
161
|
extend ClassMethods
|
171
162
|
include InstanceMethods
|
172
163
|
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "delegate"
|
4
|
+
require "forwardable"
|
5
|
+
|
6
|
+
class Jat
|
7
|
+
module Plugins
|
8
|
+
module Presenter
|
9
|
+
def self.plugin_name
|
10
|
+
:presenter
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.before_load(jat_class, **opts)
|
14
|
+
if jat_class.plugin_used?(:json_api)
|
15
|
+
jat_class.plugin :json_api_preloads, **opts
|
16
|
+
elsif jat_class.plugin_used?(:simple_api)
|
17
|
+
jat_class.plugin :simple_api_preloads, **opts
|
18
|
+
else
|
19
|
+
raise Error, "Please load :json_api or :simple_api plugin first"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.load(jat_class, **_opts)
|
24
|
+
jat_class.extend(ClassMethods)
|
25
|
+
jat_class::ResponsePiece.include(ResponsePieceInstanceMethods)
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.after_load(jat_class, **_opts)
|
29
|
+
presenter_class = Class.new(Presenter)
|
30
|
+
presenter_class.jat_class = jat_class
|
31
|
+
jat_class.const_set(:Presenter, presenter_class)
|
32
|
+
end
|
33
|
+
|
34
|
+
class Presenter < SimpleDelegator
|
35
|
+
module InstanceMethods
|
36
|
+
# Delegates all missing methods to current object.
|
37
|
+
# We create real methods after first missing method.
|
38
|
+
# rubocop:disable Style/MissingRespondToMissing (base SimpleDelegator class has this method)
|
39
|
+
def method_missing(name, *args, &block)
|
40
|
+
super(name, *args, &block).tap do
|
41
|
+
self.class.def_delegator :__getobj__, name
|
42
|
+
end
|
43
|
+
end
|
44
|
+
# rubocop:enable Style/MissingRespondToMissing
|
45
|
+
end
|
46
|
+
|
47
|
+
extend Forwardable
|
48
|
+
extend Jat::JatClass
|
49
|
+
include InstanceMethods
|
50
|
+
end
|
51
|
+
|
52
|
+
module ClassMethods
|
53
|
+
def inherited(subclass)
|
54
|
+
presenter_class = Class.new(self::Presenter)
|
55
|
+
presenter_class.jat_class = subclass
|
56
|
+
subclass.const_set(:Presenter, presenter_class)
|
57
|
+
|
58
|
+
super
|
59
|
+
end
|
60
|
+
|
61
|
+
def attribute(name, **opts, &block)
|
62
|
+
# Define attr_accessor in presenter automatically
|
63
|
+
super.tap do |attribute|
|
64
|
+
self::Presenter.def_delegator(:__getobj__, attribute.key) unless attribute.params[:block]
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
module ResponsePieceInstanceMethods
|
70
|
+
def initialize(*)
|
71
|
+
super
|
72
|
+
@object = jat_class::Presenter.new(object)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
register_plugin(Presenter.plugin_name, Presenter)
|
78
|
+
end
|
79
|
+
end
|
@@ -5,16 +5,6 @@ class Jat
|
|
5
5
|
module SimpleApi
|
6
6
|
class FieldsParamParser
|
7
7
|
module ClassMethods
|
8
|
-
# Returns the Jat class that this FieldsParamParser class is namespaced under.
|
9
|
-
attr_accessor :jat_class
|
10
|
-
|
11
|
-
# Since FieldsParamParser is anonymously subclassed when Jat is subclassed,
|
12
|
-
# and then assigned to a constant of the Jat subclass, make inspect
|
13
|
-
# reflect the likely name for the class.
|
14
|
-
def inspect
|
15
|
-
"#{jat_class.inspect}::FieldsParamParser"
|
16
|
-
end
|
17
|
-
|
18
8
|
def parse(fields)
|
19
9
|
return FROZEN_EMPTY_HASH unless fields
|
20
10
|
|
@@ -73,6 +63,7 @@ class Jat
|
|
73
63
|
end
|
74
64
|
end
|
75
65
|
|
66
|
+
extend Jat::JatClass
|
76
67
|
extend ClassMethods
|
77
68
|
include InstanceMethods
|
78
69
|
end
|
@@ -5,16 +5,6 @@ class Jat
|
|
5
5
|
module SimpleApi
|
6
6
|
class Map
|
7
7
|
module ClassMethods
|
8
|
-
# Returns the Jat class that this Map class is namespaced under.
|
9
|
-
attr_accessor :jat_class
|
10
|
-
|
11
|
-
# Since Map is anonymously subclassed when Jat is subclassed,
|
12
|
-
# and then assigned to a constant of the Jat subclass, make inspect
|
13
|
-
# reflect the likely name for the class.
|
14
|
-
def inspect
|
15
|
-
"#{jat_class.inspect}::Map"
|
16
|
-
end
|
17
|
-
|
18
8
|
# Returns structure like
|
19
9
|
# {
|
20
10
|
# key1 => { key11 => {}, key12 => { ... } },
|
@@ -90,6 +80,7 @@ class Jat
|
|
90
80
|
end
|
91
81
|
end
|
92
82
|
|
83
|
+
extend Jat::JatClass
|
93
84
|
extend ClassMethods
|
94
85
|
include InstanceMethods
|
95
86
|
end
|
@@ -6,16 +6,6 @@ class Jat
|
|
6
6
|
module SimpleApi
|
7
7
|
class Response
|
8
8
|
module ClassMethods
|
9
|
-
# Returns the Jat class that this Response class is namespaced under.
|
10
|
-
attr_accessor :jat_class
|
11
|
-
|
12
|
-
# Since Response is anonymously subclassed when Jat is subclassed,
|
13
|
-
# and then assigned to a constant of the Jat subclass, make inspect
|
14
|
-
# reflect the likely name for the class.
|
15
|
-
def inspect
|
16
|
-
"#{jat_class.inspect}::Response"
|
17
|
-
end
|
18
|
-
|
19
9
|
def call(object, context)
|
20
10
|
new(object, context).to_h
|
21
11
|
end
|
@@ -111,6 +101,7 @@ class Jat
|
|
111
101
|
end
|
112
102
|
end
|
113
103
|
|
104
|
+
extend Jat::JatClass
|
114
105
|
extend ClassMethods
|
115
106
|
include InstanceMethods
|
116
107
|
end
|
@@ -6,16 +6,6 @@ class Jat
|
|
6
6
|
module SimpleApi
|
7
7
|
class ResponsePiece
|
8
8
|
module ClassMethods
|
9
|
-
# Returns the Jat class that this ResponsePiece class is namespaced under.
|
10
|
-
attr_accessor :jat_class
|
11
|
-
|
12
|
-
# Since ResponsePiece is anonymously subclassed when Jat is subclassed,
|
13
|
-
# and then assigned to a constant of the Jat subclass, make inspect
|
14
|
-
# reflect the likely name for the class.
|
15
|
-
def inspect
|
16
|
-
"#{jat_class.inspect}::ResponsePiece"
|
17
|
-
end
|
18
|
-
|
19
9
|
def to_h(object, context, map)
|
20
10
|
new(object, context).piece(map)
|
21
11
|
end
|
@@ -72,6 +62,7 @@ class Jat
|
|
72
62
|
end
|
73
63
|
end
|
74
64
|
|
65
|
+
extend Jat::JatClass
|
75
66
|
extend ClassMethods
|
76
67
|
include InstanceMethods
|
77
68
|
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Jat
|
4
|
+
module JatClass
|
5
|
+
# Returns the Jat class that this class is namespaced under.
|
6
|
+
attr_accessor :jat_class
|
7
|
+
|
8
|
+
# Since class that uses current module is anonymously subclassed when Jat is
|
9
|
+
# subclassed, and then assigned to a constant of the Jat subclass, make
|
10
|
+
# inspect reflect the likely name for the class.
|
11
|
+
def inspect
|
12
|
+
return super unless jat_class
|
13
|
+
|
14
|
+
path = superclass.inspect
|
15
|
+
index = path.rindex("::") + 2
|
16
|
+
class_name = path[index, path.length - index]
|
17
|
+
|
18
|
+
"#{jat_class.inspect}::#{class_name}"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/lib/jat.rb
CHANGED
@@ -0,0 +1,215 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "test_helper"
|
4
|
+
|
5
|
+
describe "Jat::Plugins::Presenter" do
|
6
|
+
describe "before load" do
|
7
|
+
it "raises error if no response plugin is loaded" do
|
8
|
+
jat_class = Class.new(Jat)
|
9
|
+
err = assert_raises(Jat::Error) { jat_class.plugin :presenter }
|
10
|
+
assert_equal "Please load :json_api or :simple_api plugin first", err.message
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "ClassMethods" do
|
15
|
+
let(:jat_class) do
|
16
|
+
jat_class = Class.new(Jat)
|
17
|
+
jat_class.plugin :simple_api
|
18
|
+
jat_class.plugin :presenter
|
19
|
+
jat_class
|
20
|
+
end
|
21
|
+
|
22
|
+
let(:presenter_class) { jat_class::Presenter }
|
23
|
+
|
24
|
+
describe ".jat_class=" do
|
25
|
+
it "assigns @jat_class" do
|
26
|
+
presenter_class.jat_class = :foo
|
27
|
+
assert_equal :foo, presenter_class.instance_variable_get(:@jat_class)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe ".jat_class" do
|
32
|
+
it "returns self @jat_class" do
|
33
|
+
assert_same jat_class, presenter_class.instance_variable_get(:@jat_class)
|
34
|
+
assert_same jat_class, presenter_class.jat_class
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
describe ".inspect" do
|
39
|
+
it "returns likely name of a class" do
|
40
|
+
assert_equal "#{jat_class}::Presenter", presenter_class.inspect
|
41
|
+
end
|
42
|
+
|
43
|
+
it "returns self name for top level presenter class" do
|
44
|
+
Jat::Plugins.find_plugin(:presenter)
|
45
|
+
assert_equal "Jat::Plugins::Presenter::Presenter", Jat::Plugins::Presenter::Presenter.inspect
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
describe "with simple_api" do
|
51
|
+
let(:serializer) do
|
52
|
+
Class.new(Jat) do |base|
|
53
|
+
base.plugin :simple_api
|
54
|
+
base.plugin :presenter
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
describe ".inherited" do
|
59
|
+
let(:parent) { serializer }
|
60
|
+
|
61
|
+
it "inherits presenter class" do
|
62
|
+
child = Class.new(parent)
|
63
|
+
assert_equal parent::Presenter, child::Presenter.superclass
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
it "adds presenter methods when adding attribute" do
|
68
|
+
serializer.attribute :length
|
69
|
+
assert_includes serializer::Presenter.instance_methods, :length
|
70
|
+
end
|
71
|
+
|
72
|
+
it "adds presenter methods when adding attribute with key" do
|
73
|
+
serializer.attribute :length, key: :size
|
74
|
+
assert_includes serializer::Presenter.instance_methods, :size
|
75
|
+
end
|
76
|
+
|
77
|
+
it "does not add presenter methods when adding attribute with block" do
|
78
|
+
serializer.attribute(:length) {}
|
79
|
+
refute_includes serializer::Presenter.instance_methods, :length
|
80
|
+
end
|
81
|
+
|
82
|
+
it "adds presenter methods used in block after first serialization" do
|
83
|
+
serializer.attribute(:length) { |obj| obj.size }
|
84
|
+
|
85
|
+
refute_includes serializer::Presenter.instance_methods, :size
|
86
|
+
serializer.to_h("")
|
87
|
+
assert_includes serializer::Presenter.instance_methods, :size
|
88
|
+
end
|
89
|
+
|
90
|
+
it "allows to use custom methods defined directly in Presenter class" do
|
91
|
+
serializer::Presenter.class_exec do
|
92
|
+
def rev
|
93
|
+
reverse
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
serializer.attribute(:rev) { |obj| obj.rev }
|
98
|
+
rev = serializer.to_h("jingle")[:rev]
|
99
|
+
assert_equal "elgnij", rev
|
100
|
+
end
|
101
|
+
|
102
|
+
it "allows to override attribute methods" do
|
103
|
+
serializer.attribute :qrcode1
|
104
|
+
serializer.attribute :qrcode2
|
105
|
+
|
106
|
+
assert_includes serializer::Presenter.instance_methods, :qrcode1
|
107
|
+
assert_includes serializer::Presenter.instance_methods, :qrcode2
|
108
|
+
serializer::Presenter.class_exec do
|
109
|
+
def qrcode1
|
110
|
+
common
|
111
|
+
end
|
112
|
+
|
113
|
+
def qrcode2
|
114
|
+
common
|
115
|
+
end
|
116
|
+
|
117
|
+
private
|
118
|
+
|
119
|
+
def common
|
120
|
+
@qrcode = "QRCODE"
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
res = serializer.to_h(nil)
|
125
|
+
qrcode1 = res[:qrcode1]
|
126
|
+
qrcode2 = res[:qrcode2]
|
127
|
+
assert_equal "QRCODE", qrcode1
|
128
|
+
assert_equal "QRCODE", qrcode2
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
describe "with json_api" do
|
133
|
+
let(:serializer) do
|
134
|
+
Class.new(Jat) do |base|
|
135
|
+
base.plugin :json_api
|
136
|
+
base.plugin :presenter
|
137
|
+
base.type :test
|
138
|
+
base.id { |obj| obj }
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
describe ".inherited" do
|
143
|
+
let(:parent) { serializer }
|
144
|
+
|
145
|
+
it "inherits presenter class" do
|
146
|
+
child = Class.new(parent)
|
147
|
+
assert_equal parent::Presenter, child::Presenter.superclass
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
it "adds presenter methods when adding attribute" do
|
152
|
+
serializer.attribute :length
|
153
|
+
assert_includes serializer::Presenter.instance_methods, :length
|
154
|
+
end
|
155
|
+
|
156
|
+
it "adds presenter methods when adding attribute with key" do
|
157
|
+
serializer.attribute :length, key: :size
|
158
|
+
assert_includes serializer::Presenter.instance_methods, :size
|
159
|
+
end
|
160
|
+
|
161
|
+
it "does not add presenter methods when adding attribute with block" do
|
162
|
+
serializer.attribute(:length) {}
|
163
|
+
refute_includes serializer::Presenter.instance_methods, :length
|
164
|
+
end
|
165
|
+
|
166
|
+
it "adds presenter methods used in block after first serialization" do
|
167
|
+
serializer.attribute(:length) { |obj| obj.size }
|
168
|
+
|
169
|
+
refute_includes serializer::Presenter.instance_methods, :size
|
170
|
+
serializer.to_h("")
|
171
|
+
assert_includes serializer::Presenter.instance_methods, :size
|
172
|
+
end
|
173
|
+
|
174
|
+
it "allows to use custom methods defined directly in Presenter class" do
|
175
|
+
serializer::Presenter.class_exec do
|
176
|
+
def rev
|
177
|
+
reverse
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
serializer.attribute(:rev) { |obj| obj.rev }
|
182
|
+
rev = serializer.to_h("jingle")[:data][:attributes][:rev]
|
183
|
+
assert_equal "elgnij", rev
|
184
|
+
end
|
185
|
+
|
186
|
+
it "allows to override attribute methods" do
|
187
|
+
serializer.attribute :qrcode1
|
188
|
+
serializer.attribute :qrcode2
|
189
|
+
|
190
|
+
assert_includes serializer::Presenter.instance_methods, :qrcode1
|
191
|
+
assert_includes serializer::Presenter.instance_methods, :qrcode2
|
192
|
+
serializer::Presenter.class_exec do
|
193
|
+
def qrcode1
|
194
|
+
common
|
195
|
+
end
|
196
|
+
|
197
|
+
def qrcode2
|
198
|
+
common
|
199
|
+
end
|
200
|
+
|
201
|
+
private
|
202
|
+
|
203
|
+
def common
|
204
|
+
@qrcode = "QRCODE"
|
205
|
+
end
|
206
|
+
end
|
207
|
+
|
208
|
+
res = serializer.to_h(nil)[:data][:attributes]
|
209
|
+
qrcode1 = res[:qrcode1]
|
210
|
+
qrcode2 = res[:qrcode2]
|
211
|
+
assert_equal "QRCODE", qrcode1
|
212
|
+
assert_equal "QRCODE", qrcode2
|
213
|
+
end
|
214
|
+
end
|
215
|
+
end
|
@@ -36,6 +36,7 @@ describe Jat::Plugins do
|
|
36
36
|
assert_equal "Jat::Plugins::LowerCamelCase", described_module.find_plugin(:lower_camel_case).name
|
37
37
|
assert_equal "Jat::Plugins::MapsCache", described_module.find_plugin(:maps_cache).name
|
38
38
|
assert_equal "Jat::Plugins::Preloads", described_module.find_plugin(:preloads).name
|
39
|
+
assert_equal "Jat::Plugins::Presenter", described_module.find_plugin(:presenter).name
|
39
40
|
assert_equal "Jat::Plugins::SimpleApi", described_module.find_plugin(:simple_api).name
|
40
41
|
assert_equal "Jat::Plugins::ToStr", described_module.find_plugin(:to_str).name
|
41
42
|
assert_equal "Jat::Plugins::Types", described_module.find_plugin(:types).name
|
data/test/test_helper.rb
CHANGED
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.10
|
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-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: minitest
|
@@ -138,6 +138,7 @@ files:
|
|
138
138
|
- lib/jat/plugins/lower_camel_case/lower_camel_case.rb
|
139
139
|
- lib/jat/plugins/maps_cache/maps_cache.rb
|
140
140
|
- lib/jat/plugins/preloads/preloads.rb
|
141
|
+
- lib/jat/plugins/presenter/presenter.rb
|
141
142
|
- lib/jat/plugins/simple_api/lib/fields_param_parser.rb
|
142
143
|
- lib/jat/plugins/simple_api/lib/map.rb
|
143
144
|
- lib/jat/plugins/simple_api/lib/response.rb
|
@@ -156,6 +157,7 @@ files:
|
|
156
157
|
- lib/jat/plugins/validate_params/validate_params.rb
|
157
158
|
- lib/jat/utils/enum_deep_dup.rb
|
158
159
|
- lib/jat/utils/enum_deep_freeze.rb
|
160
|
+
- lib/jat/utils/jat_class.rb
|
159
161
|
- test/lib/jat/attribute_test.rb
|
160
162
|
- test/lib/jat/config_test.rb
|
161
163
|
- test/lib/jat/plugins/activerecord/activerecord_test.rb
|
@@ -181,6 +183,7 @@ files:
|
|
181
183
|
- test/lib/jat/plugins/lower_camel_case/lower_camel_case_test.rb
|
182
184
|
- test/lib/jat/plugins/maps_cache/maps_cache_test.rb
|
183
185
|
- test/lib/jat/plugins/preloads/preloads_test.rb
|
186
|
+
- test/lib/jat/plugins/presenter/presenter_test.rb
|
184
187
|
- test/lib/jat/plugins/simple_api/lib/fields_param_parser_test.rb
|
185
188
|
- test/lib/jat/plugins/simple_api/lib/map_test.rb
|
186
189
|
- test/lib/jat/plugins/simple_api/lib/response_piece_test.rb
|
@@ -199,10 +202,8 @@ files:
|
|
199
202
|
- test/lib/jat/utils/enum_deep_dup_test.rb
|
200
203
|
- test/lib/jat/utils/enum_deep_freeze_test.rb
|
201
204
|
- test/lib/jat_test.rb
|
202
|
-
- test/lib/plugin_test.rb
|
203
205
|
- test/support/activerecord.rb
|
204
206
|
- test/test_helper.rb
|
205
|
-
- test/test_plugin.rb
|
206
207
|
homepage: https://github.com/aglushkov/jat
|
207
208
|
licenses:
|
208
209
|
- MIT
|
@@ -255,6 +256,7 @@ test_files:
|
|
255
256
|
- test/lib/jat/plugins/lower_camel_case/lower_camel_case_test.rb
|
256
257
|
- test/lib/jat/plugins/maps_cache/maps_cache_test.rb
|
257
258
|
- test/lib/jat/plugins/preloads/preloads_test.rb
|
259
|
+
- test/lib/jat/plugins/presenter/presenter_test.rb
|
258
260
|
- test/lib/jat/plugins/simple_api/lib/fields_param_parser_test.rb
|
259
261
|
- test/lib/jat/plugins/simple_api/lib/map_test.rb
|
260
262
|
- test/lib/jat/plugins/simple_api/lib/response_piece_test.rb
|
@@ -273,7 +275,5 @@ test_files:
|
|
273
275
|
- test/lib/jat/utils/enum_deep_dup_test.rb
|
274
276
|
- test/lib/jat/utils/enum_deep_freeze_test.rb
|
275
277
|
- test/lib/jat_test.rb
|
276
|
-
- test/lib/plugin_test.rb
|
277
278
|
- test/support/activerecord.rb
|
278
279
|
- test/test_helper.rb
|
279
|
-
- test/test_plugin.rb
|
data/test/lib/plugin_test.rb
DELETED
@@ -1,49 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require "test_helper"
|
4
|
-
require "test_plugin"
|
5
|
-
|
6
|
-
describe "plugin system" do
|
7
|
-
describe "Jat.plugin" do
|
8
|
-
let(:jat_class) { Class.new(Jat) }
|
9
|
-
let(:components) { [jat_class, jat_class::Attribute, jat_class::Config] }
|
10
|
-
|
11
|
-
before do
|
12
|
-
components.each do |component|
|
13
|
-
component::InstanceMethods.send(:define_method, :foo) { :foo }
|
14
|
-
component::ClassMethods.send(:define_method, :foo) { :foo }
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
after do
|
19
|
-
components.each do |component|
|
20
|
-
component::InstanceMethods.send(:undef_method, :foo)
|
21
|
-
component::ClassMethods.send(:undef_method, :foo)
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
it "allows the plugin to override base methods of core classes" do
|
26
|
-
components.each do |component|
|
27
|
-
assert_equal :foo, component.foo
|
28
|
-
assert_equal :foo, component.allocate.foo
|
29
|
-
end
|
30
|
-
|
31
|
-
jat_class.plugin(TestPlugin)
|
32
|
-
|
33
|
-
components.each do |component|
|
34
|
-
assert_equal :plugin_foo, component.foo
|
35
|
-
assert_equal :plugin_foo, component.allocate.foo
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
it "calls before_load before loading plugin" do
|
40
|
-
jat_class.plugin(TestPlugin)
|
41
|
-
assert_equal :foo, jat_class.config[:before_load]
|
42
|
-
end
|
43
|
-
|
44
|
-
it "calls after_load after loading plugin" do
|
45
|
-
jat_class.plugin(TestPlugin)
|
46
|
-
assert_equal :plugin_foo, jat_class.config[:after_load]
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
data/test/test_plugin.rb
DELETED
@@ -1,56 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module TestPlugin
|
4
|
-
def self.before_load(jat_class, *)
|
5
|
-
jat_class.config[:before_load] = jat_class.foo
|
6
|
-
end
|
7
|
-
|
8
|
-
def self.load(jat_class, **_opts)
|
9
|
-
jat_class.extend(ClassMethods)
|
10
|
-
jat_class.include(InstanceMethods)
|
11
|
-
jat_class::Attribute.extend(AttributeClassMethods)
|
12
|
-
jat_class::Attribute.include(AttributeMethods)
|
13
|
-
jat_class::Config.extend(ConfigClassMethods)
|
14
|
-
jat_class::Config.include(ConfigMethods)
|
15
|
-
end
|
16
|
-
|
17
|
-
def self.after_load(jat_class, *)
|
18
|
-
jat_class.config[:after_load] = jat_class.foo
|
19
|
-
end
|
20
|
-
|
21
|
-
module ClassMethods
|
22
|
-
def foo
|
23
|
-
:plugin_foo
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
module InstanceMethods
|
28
|
-
def foo
|
29
|
-
:plugin_foo
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
module AttributeClassMethods
|
34
|
-
def foo
|
35
|
-
:plugin_foo
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
module AttributeMethods
|
40
|
-
def foo
|
41
|
-
:plugin_foo
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
module ConfigClassMethods
|
46
|
-
def foo
|
47
|
-
:plugin_foo
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
module ConfigMethods
|
52
|
-
def foo
|
53
|
-
:plugin_foo
|
54
|
-
end
|
55
|
-
end
|
56
|
-
end
|