jat 0.0.6 → 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (72) hide show
  1. checksums.yaml +4 -4
  2. data/lib/jat/attribute.rb +5 -18
  3. data/lib/jat/config.rb +5 -20
  4. data/lib/jat/plugins/{common/_activerecord_preloads/_activerecord_preloads.rb → base/base_activerecord_preloads/base_activerecord_preloads.rb} +3 -3
  5. data/lib/jat/plugins/{common/_activerecord_preloads → base/base_activerecord_preloads}/lib/preloader.rb +3 -1
  6. data/lib/jat/plugins/{common/_lower_camel_case/_lower_camel_case.rb → base/base_lower_camel_case/base_lower_camel_case.rb} +3 -3
  7. data/lib/jat/plugins/{common/_preloads/_preloads.rb → base/base_preloads/base_preloads.rb} +3 -3
  8. data/lib/jat/plugins/{common/_preloads → base/base_preloads}/lib/format_user_preloads.rb +1 -1
  9. data/lib/jat/plugins/{common/_preloads → base/base_preloads}/lib/preloads_with_path.rb +1 -1
  10. data/lib/jat/plugins/json_api/lib/fields_param_parser.rb +1 -10
  11. data/lib/jat/plugins/json_api/lib/include_param_parser.rb +1 -10
  12. data/lib/jat/plugins/json_api/lib/map.rb +5 -15
  13. data/lib/jat/plugins/json_api/lib/response.rb +2 -15
  14. data/lib/jat/plugins/json_api/lib/response_piece.rb +10 -19
  15. data/lib/jat/plugins/json_api/plugins/json_api_activerecord/json_api_activerecord.rb +1 -1
  16. data/lib/jat/plugins/json_api/plugins/json_api_lower_camel_case/json_api_lower_camel_case.rb +1 -1
  17. data/lib/jat/plugins/json_api/plugins/json_api_preloads/json_api_preloads.rb +1 -1
  18. data/lib/jat/plugins/json_api/plugins/json_api_validate_params/lib/validate_include_param.rb +1 -3
  19. data/lib/jat/plugins/presenter/presenter.rb +79 -0
  20. data/lib/jat/plugins/simple_api/lib/fields_param_parser.rb +21 -46
  21. data/lib/jat/plugins/simple_api/lib/map.rb +5 -15
  22. data/lib/jat/plugins/simple_api/lib/response.rb +2 -15
  23. data/lib/jat/plugins/simple_api/lib/response_piece.rb +2 -15
  24. data/lib/jat/plugins/simple_api/plugins/simple_api_activerecord/simple_api_activerecord.rb +1 -1
  25. data/lib/jat/plugins/simple_api/plugins/simple_api_lower_camel_case/simple_api_lower_camel_case.rb +1 -1
  26. data/lib/jat/plugins/simple_api/plugins/simple_api_preloads/simple_api_preloads.rb +1 -1
  27. data/lib/jat/plugins/simple_api/plugins/simple_api_validate_params/simple_api_validate_params.rb +1 -1
  28. data/lib/jat/plugins/simple_api/simple_api.rb +1 -1
  29. data/lib/jat/plugins/types/types.rb +55 -0
  30. data/lib/jat/plugins.rb +38 -25
  31. data/lib/jat/utils/jat_class.rb +21 -0
  32. data/lib/jat.rb +9 -6
  33. data/test/lib/jat/attribute_test.rb +8 -0
  34. data/test/lib/jat/plugins/activerecord/activerecord_test.rb +27 -0
  35. data/test/lib/jat/plugins/{_activerecord_preloads/_activerecord_preloads_test.rb → base_activerecord_preloads/base_activerecord_preloads_test.rb} +3 -3
  36. data/test/lib/jat/plugins/{_activerecord_preloads → base_activerecord_preloads}/lib/preloader_test.rb +2 -2
  37. data/test/lib/jat/plugins/{_camel_lower/_camel_lower_test.rb → base_lower_camel_case/base_lower_camel_case_test.rb} +2 -2
  38. data/test/lib/jat/plugins/{_preloads/_preloads_test.rb → base_preloads/base_preloads_test.rb} +2 -2
  39. data/test/lib/jat/plugins/{_preloads → base_preloads}/lib/format_user_preloads_test.rb +3 -3
  40. data/test/lib/jat/plugins/{_preloads → base_preloads}/lib/preloads_with_path_test.rb +2 -2
  41. data/test/lib/jat/plugins/json_api/json_api_test.rb +1 -1
  42. data/test/lib/jat/plugins/json_api/lib/fields_param_parser_test.rb +20 -12
  43. data/test/lib/jat/plugins/json_api/lib/include_param_parser_test.rb +25 -11
  44. data/test/lib/jat/plugins/json_api/lib/map_test.rb +132 -124
  45. data/test/lib/jat/plugins/json_api/lib/response_piece_test.rb +13 -0
  46. data/test/lib/jat/plugins/json_api/lib/response_test.rb +6 -1
  47. data/test/lib/jat/plugins/json_api_activerecord/json_api_activerecord_test.rb +1 -1
  48. data/test/lib/jat/plugins/{json_api_camel_lower/json_api_camel_lower_test.rb → json_api_lower_camel_case/json_api_lower_camel_case_test.rb} +6 -0
  49. data/test/lib/jat/plugins/json_api_preloads/json_api_preloads_test.rb +16 -2
  50. data/test/lib/jat/plugins/json_api_preloads/lib/preloads_test.rb +0 -3
  51. data/test/lib/jat/plugins/json_api_validate_params/json_api_validate_params_test.rb +30 -6
  52. data/test/lib/jat/plugins/lower_camel_case/lower_camel_case_test.rb +27 -0
  53. data/test/lib/jat/plugins/maps_cache/maps_cache_test.rb +27 -0
  54. data/test/lib/jat/plugins/preloads/preloads_test.rb +27 -0
  55. data/test/lib/jat/plugins/presenter/presenter_test.rb +215 -0
  56. data/test/lib/jat/plugins/simple_api/lib/fields_param_parser_test.rb +57 -43
  57. data/test/lib/jat/plugins/simple_api/lib/map_test.rb +90 -82
  58. data/test/lib/jat/plugins/simple_api/lib/response_piece_test.rb +13 -0
  59. data/test/lib/jat/plugins/simple_api/lib/response_test.rb +17 -11
  60. data/test/lib/jat/plugins/simple_api/simple_api_test.rb +36 -1
  61. data/test/lib/jat/plugins/simple_api_activerecord/simple_api_activerecord_test.rb +1 -1
  62. data/test/lib/jat/plugins/{simple_api_camel_lower/simple_api_camel_lower_test.rb → simple_api_lower_camel_case/simple_api_lower_camel_case_test.rb} +7 -1
  63. data/test/lib/jat/plugins/simple_api_preloads/lib/preloads_test.rb +1 -6
  64. data/test/lib/jat/plugins/simple_api_preloads/simple_api_preloads_test.rb +16 -2
  65. data/test/lib/jat/plugins/simple_api_validate_params/simple_api_validate_params_test.rb +12 -0
  66. data/test/lib/jat/plugins/types/types_test.rb +83 -0
  67. data/test/lib/jat/plugins/validate_params/validate_params_test.rb +27 -0
  68. data/test/lib/jat/plugins_test.rb +89 -0
  69. data/test/test_helper.rb +14 -4
  70. metadata +72 -67
  71. data/test/lib/plugin_test.rb +0 -49
  72. data/test/test_plugin.rb +0 -56
@@ -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 => { ... } },
@@ -74,11 +64,10 @@ class Jat
74
64
  private
75
65
 
76
66
  def expose?(attribute, fields)
77
- case exposed
78
- when :all then true
79
- when :none then manually_exposed?(attribute, fields)
80
- else attribute.exposed? || manually_exposed?(attribute, fields)
81
- end
67
+ return true if exposed == :all
68
+ return manually_exposed?(attribute, fields) if exposed == :none
69
+
70
+ attribute.exposed? || manually_exposed?(attribute, fields)
82
71
  end
83
72
 
84
73
  def manually_exposed?(attribute, fields)
@@ -91,6 +80,7 @@ class Jat
91
80
  end
92
81
  end
93
82
 
83
+ extend Jat::JatClass
94
84
  extend ClassMethods
95
85
  include InstanceMethods
96
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
@@ -95,7 +85,7 @@ class Jat
95
85
  meta.each do |name, attribute|
96
86
  next if data.key?(name)
97
87
 
98
- value = attribute_value(attribute)
88
+ value = attribute.value(object, context)
99
89
 
100
90
  unless value.nil?
101
91
  data = data.dup if data.equal?(FROZEN_EMPTY_HASH)
@@ -109,12 +99,9 @@ class Jat
109
99
  def context_metadata
110
100
  context[:meta]&.transform_keys(&:to_sym) || FROZEN_EMPTY_HASH
111
101
  end
112
-
113
- def attribute_value(attribute)
114
- attribute.block.call(object, context)
115
- end
116
102
  end
117
103
 
104
+ extend Jat::JatClass
118
105
  extend ClassMethods
119
106
  include InstanceMethods
120
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
@@ -37,7 +27,7 @@ class Jat
37
27
 
38
28
  map.each do |key, inner_map|
39
29
  attribute = jat_class.attributes.fetch(key)
40
- value = attribute_value(attribute)
30
+ value = attribute.value(object, context)
41
31
 
42
32
  result[key] =
43
33
  if attribute.relation?
@@ -56,10 +46,6 @@ class Jat
56
46
 
57
47
  private
58
48
 
59
- def attribute_value(attribute)
60
- attribute.block.call(object, context)
61
- end
62
-
63
49
  def inner_piece(attribute, value, inner_map)
64
50
  serializer = attribute.serializer.call
65
51
  serializer::ResponsePiece.to_h(value, context, inner_map)
@@ -76,6 +62,7 @@ class Jat
76
62
  end
77
63
  end
78
64
 
65
+ extend Jat::JatClass
79
66
  extend ClassMethods
80
67
  include InstanceMethods
81
68
  end
@@ -14,7 +14,7 @@ class Jat
14
14
 
15
15
  def self.load(jat_class, **opts)
16
16
  jat_class.plugin :simple_api_preloads, **opts
17
- jat_class.plugin :_activerecord_preloads, **opts
17
+ jat_class.plugin :base_activerecord_preloads, **opts
18
18
  end
19
19
  end
20
20
 
@@ -10,7 +10,7 @@ class Jat
10
10
  def self.before_load(jat_class, **_opts)
11
11
  raise Error, "Please load :simple_api plugin first" unless jat_class.plugin_used?(:simple_api)
12
12
 
13
- jat_class.plugin :_lower_camel_case
13
+ jat_class.plugin :base_lower_camel_case
14
14
  end
15
15
 
16
16
  def self.load(jat_class, **_opts)
@@ -12,7 +12,7 @@ class Jat
12
12
  def self.before_load(jat_class, **opts)
13
13
  raise Error, "Please load :simple_api plugin first" unless jat_class.plugin_used?(:simple_api)
14
14
 
15
- jat_class.plugin :_preloads, **opts
15
+ jat_class.plugin :base_preloads, **opts
16
16
  end
17
17
 
18
18
  def self.load(jat_class, **_opts)
@@ -38,7 +38,7 @@ class Jat
38
38
  end
39
39
 
40
40
  module FieldsParamParserMethods
41
- def parse
41
+ def parse(*)
42
42
  super.tap { |result| ValidateFieldsParam.call(self.class.jat_class, result) }
43
43
  end
44
44
  end
@@ -79,7 +79,7 @@ class Jat
79
79
  # Assign same meta
80
80
  added_meta.each_value do |attribute|
81
81
  params = attribute.params
82
- subclass.attribute(params[:name], **params[:opts], &params[:block])
82
+ subclass.meta(params[:name], **params[:opts], &params[:block])
83
83
  end
84
84
  end
85
85
 
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Jat
4
+ module Plugins
5
+ module Types
6
+ def self.plugin_name
7
+ :types
8
+ end
9
+
10
+ def self.load(jat_class, **_opts)
11
+ jat_class::Attribute.include(InstanceMethods)
12
+ end
13
+
14
+ def self.after_load(jat_class, **_opts)
15
+ jat_class.config[:types] = {
16
+ array: ->(value) { Array(value) },
17
+ bool: ->(value) { !!value },
18
+ float: ->(value) { Float(value) },
19
+ hash: ->(value) { Hash(value) },
20
+ int: ->(value) { Integer(value) },
21
+ str: ->(value) { String(value) }
22
+ }
23
+ end
24
+
25
+ module InstanceMethods
26
+ def block
27
+ return @block if instance_variable_defined?(:@block)
28
+
29
+ original_block = super
30
+ type = opts[:type]
31
+ return original_block unless type
32
+
33
+ @block = typed_block(type, original_block)
34
+ end
35
+
36
+ private
37
+
38
+ def typed_block(type, original_block)
39
+ proc do |object, context|
40
+ value = original_block.call(object, context)
41
+
42
+ # Type conversion
43
+ if type.is_a?(Symbol)
44
+ self.class.jat_class.config.fetch(:types).fetch(type).call(value)
45
+ else
46
+ type.call(value)
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
52
+
53
+ register_plugin(Types.plugin_name, Types)
54
+ end
55
+ end
data/lib/jat/plugins.rb CHANGED
@@ -1,39 +1,52 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Jat
4
+ class PluginLoadError < Error; end
5
+
4
6
  module Plugins
5
7
  @plugins = {}
6
8
 
7
- # Register the given plugin with Jat, so that it can be loaded using
8
- # `Jat.plugin` with a symbol. Should be used by plugin files. Example:
9
- #
10
- # Jat::Plugins.register_plugin(:plugin_name, PluginModule)
11
- def self.register_plugin(name, mod)
12
- @plugins[name] = mod
13
- end
9
+ class << self
10
+ # Registers given plugin to be able to load it using symbol name.
11
+ #
12
+ # Example: Jat::Plugins.register_plugin(:plugin_name, PluginModule)
13
+ def register_plugin(name, mod)
14
+ @plugins[name] = mod
15
+ end
14
16
 
15
- # If the registered plugin already exists, use it. Otherwise, require
16
- # and return it. This raises a LoadError if such a plugin doesn't exist,
17
- # or a Jat::Error if it exists but it does not register itself
18
- # correctly.
19
- def self.find_plugin(name)
20
- return name if name.is_a?(Module)
17
+ # If the registered plugin already exists, use it. Otherwise, require
18
+ # and return it. This raises a LoadError if such a plugin doesn't exist,
19
+ # or a Jat::Error if it exists but it does not register itself
20
+ # correctly.
21
+ def find_plugin(name)
22
+ return name if name.is_a?(Module)
23
+ return @plugins[name] if @plugins.key?(name)
21
24
 
22
- begin
23
- require "jat/plugins/#{name}/#{name}"
24
- rescue LoadError
25
- if name.start_with?("json_api")
26
- require "jat/plugins/json_api/plugins/#{name}/#{name}"
27
- elsif name.start_with?("simple_api")
28
- require "jat/plugins/simple_api/plugins/#{name}/#{name}"
29
- elsif name.start_with?("_")
30
- require "jat/plugins/common/#{name}/#{name}"
31
- else
32
- raise
25
+ begin
26
+ require_plugin(name)
27
+ rescue PluginLoadError
28
+ name_str = name.to_s
29
+ if name_str.start_with?("json_api")
30
+ require_plugin(name, "/json_api/plugins")
31
+ elsif name_str.start_with?("simple_api")
32
+ require_plugin(name, "/simple_api/plugins")
33
+ elsif name_str.start_with?("base")
34
+ require_plugin(name, "/base")
35
+ else
36
+ raise
37
+ end
33
38
  end
39
+
40
+ @plugins[name] || raise(PluginLoadError, "Plugin '#{name}' did not register itself correctly")
34
41
  end
35
42
 
36
- @plugins[name] || raise(Error, "plugin #{name} did not register itself correctly in Jat::Plugins")
43
+ private
44
+
45
+ def require_plugin(name, prefix = nil)
46
+ require "jat/plugins#{prefix}/#{name}/#{name}"
47
+ rescue LoadError
48
+ raise PluginLoadError, "Plugin '#{name}' does not exist"
49
+ end
37
50
  end
38
51
  end
39
52
  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
@@ -1,17 +1,20 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "jat/attribute"
4
- require_relative "jat/config"
5
- require_relative "jat/plugins"
6
-
7
3
  # Main namespace
8
4
  class Jat
5
+ # A generic exception used by Jat.
6
+ class Error < StandardError; end
7
+
9
8
  FROZEN_EMPTY_HASH = {}.freeze
10
9
  FROZEN_EMPTY_ARRAY = [].freeze
10
+ end
11
11
 
12
- # A generic exception used by Jat.
13
- class Error < StandardError; end
12
+ require_relative "jat/utils/jat_class"
13
+ require_relative "jat/attribute"
14
+ require_relative "jat/config"
15
+ require_relative "jat/plugins"
14
16
 
17
+ class Jat
15
18
  @config = Config.new({plugins: []})
16
19
 
17
20
  module ClassMethods
@@ -148,5 +148,13 @@ describe Jat::Attribute do
148
148
  attribute = attribute_class.new(name: "foo", opts: {key: :length})
149
149
  assert_equal 3, attribute.value([1, 2, 3], nil)
150
150
  end
151
+
152
+ it "raises error if provide block with more than 2 params" do
153
+ jat_class.attribute(:foo) {}
154
+ jat_class.attribute(:foo) { |_| }
155
+ jat_class.attribute(:foo) { |_, _| }
156
+ err = assert_raises(Jat::Error) { jat_class.attribute(:foo) { |_, _, _| } }
157
+ assert_equal "Block can have 0-2 parameters", err.message
158
+ end
151
159
  end
152
160
  end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+
5
+ describe "Jat::Plugins::Activerecord" do
6
+ it "raises error if no response plugin is loaded" do
7
+ new_class = Class.new(Jat)
8
+ err = assert_raises(Jat::Error) { new_class.plugin :activerecord }
9
+ assert_equal "Please load :json_api or :simple_api plugin first", err.message
10
+ end
11
+
12
+ it "loads simple_api compatible plugin" do
13
+ new_class = Class.new(Jat)
14
+ new_class.plugin :simple_api
15
+ new_class.plugin :activerecord
16
+
17
+ assert new_class.plugin_used?(:simple_api_activerecord)
18
+ end
19
+
20
+ it "loads json_api compatible plugin" do
21
+ new_class = Class.new(Jat)
22
+ new_class.plugin :json_api
23
+ new_class.plugin :activerecord
24
+
25
+ assert new_class.plugin_used?(:json_api_activerecord)
26
+ end
27
+ end
@@ -2,13 +2,13 @@
2
2
 
3
3
  require "test_helper"
4
4
 
5
- describe "Jat::Plugins::ActiverecordPreloads" do
5
+ describe "Jat::Plugins::BaseActiverecordPreloads" do
6
6
  let(:jat_class) do
7
7
  api_test = api_test()
8
8
 
9
9
  Class.new(Jat) do
10
10
  include api_test
11
- plugin(:_activerecord_preloads)
11
+ plugin(:base_activerecord_preloads)
12
12
  end
13
13
  end
14
14
 
@@ -27,7 +27,7 @@ describe "Jat::Plugins::ActiverecordPreloads" do
27
27
  jat = jat_class.new
28
28
  jat.expects(:preloads).returns(preloads)
29
29
 
30
- Jat::Plugins::ActiverecordPreloads::Preloader
30
+ Jat::Plugins::BaseActiverecordPreloads::Preloader
31
31
  .expects(:preload)
32
32
  .with(object, preloads).returns("OBJ_WITH_PRELOADS")
33
33
 
@@ -4,11 +4,11 @@ require "test_helper"
4
4
  require "support/activerecord"
5
5
 
6
6
  describe "Jat::Plugins::ActiverecordPreloads" do
7
- before { Jat::Plugins.find_plugin(:_activerecord_preloads) }
7
+ before { Jat::Plugins.find_plugin(:base_activerecord_preloads) }
8
8
 
9
9
  describe "Preloader" do
10
10
  let(:described_class) { plugin::Preloader }
11
- let(:plugin) { Jat::Plugins::ActiverecordPreloads }
11
+ let(:plugin) { Jat::Plugins::BaseActiverecordPreloads }
12
12
 
13
13
  describe ".handlers" do
14
14
  it "returns memorized array of handlers" do
@@ -2,10 +2,10 @@
2
2
 
3
3
  require "test_helper"
4
4
 
5
- describe "Jat::Plugins::LowerCamelCase" do
5
+ describe "Jat::Plugins::BaseLowerCamelCase" do
6
6
  let(:jat_class) do
7
7
  new_class = Class.new(Jat)
8
- new_class.plugin(:_lower_camel_case)
8
+ new_class.plugin(:base_lower_camel_case)
9
9
  new_class
10
10
  end
11
11
 
@@ -2,9 +2,9 @@
2
2
 
3
3
  require "test_helper"
4
4
 
5
- describe "Jat::Plugins::Preloads" do
5
+ describe "Jat::Plugins::BasePreloads" do
6
6
  let(:jat_class) do
7
- Class.new(Jat) { plugin(:_preloads) }
7
+ Class.new(Jat) { plugin(:base_preloads) }
8
8
  end
9
9
 
10
10
  def attribute(name, opts)
@@ -2,10 +2,10 @@
2
2
 
3
3
  require "test_helper"
4
4
 
5
- describe "Jat::Plugins::Preloads::FormatUserPreloads" do
6
- before { Jat::Plugins.find_plugin(:_preloads) }
5
+ describe "Jat::Plugins::BasePreloads::FormatUserPreloads" do
6
+ before { Jat::Plugins.find_plugin(:base_preloads) }
7
7
 
8
- let(:format) { Jat::Plugins::Preloads::FormatUserPreloads }
8
+ let(:format) { Jat::Plugins::BasePreloads::FormatUserPreloads }
9
9
 
10
10
  it "transforms nil to empty hash" do
11
11
  preloads = nil
@@ -3,9 +3,9 @@
3
3
  require "test_helper"
4
4
 
5
5
  describe "Jat::Plugins::Preloads::PreloadsWithPath" do
6
- before { Jat::Plugins.find_plugin(:_preloads) }
6
+ before { Jat::Plugins.find_plugin(:base_preloads) }
7
7
 
8
- let(:described_class) { Jat::Plugins::Preloads::PreloadsWithPath }
8
+ let(:described_class) { Jat::Plugins::BasePreloads::PreloadsWithPath }
9
9
 
10
10
  it "returns provided preloads and path to last value" do
11
11
  preloads = {a: {b: {c: {}}, d: {}}, e: {}}
@@ -46,7 +46,7 @@ describe "Jat::Plugins::JsonApi" do
46
46
  describe "#map" do
47
47
  it "returns map for provided context" do
48
48
  jat_class::Map.expects(:call).with("CONTEXT").returns "MAP"
49
- assert_equal "MAP", jat_class.map("CONTEXT")
49
+ assert_equal "MAP", jat_class.new("CONTEXT").map
50
50
  end
51
51
  end
52
52
 
@@ -18,21 +18,29 @@ describe "Jat::Plugins::JsonApi::FieldsParamParser" do
18
18
 
19
19
  let(:described_class) { jat_class::FieldsParamParser }
20
20
 
21
- it "returns empty hash when parameters not provided" do
22
- result = described_class.parse(nil)
23
-
24
- assert_equal({}, result)
21
+ describe ".inspect" do
22
+ it "returns self name" do
23
+ assert_equal "#{jat_class}::FieldsParamParser", described_class.inspect
24
+ end
25
25
  end
26
26
 
27
- it "returns parsed attributes" do
28
- result = described_class.parse(a: "a1,a2")
27
+ describe ".parse" do
28
+ it "returns empty hash when parameters not provided" do
29
+ result = described_class.parse(nil)
29
30
 
30
- assert_equal({a: %i[a1 a2]}, result)
31
- end
31
+ assert_equal({}, result)
32
+ end
32
33
 
33
- it "validates provided attributes" do
34
- jat_class.plugin :json_api_validate_params
35
- error = assert_raises(Jat::Error) { described_class.parse(a: "a1,a2,a3,a4") }
36
- assert_match(/a4/, error.message)
34
+ it "returns parsed attributes" do
35
+ result = described_class.parse(a: "a1,a2")
36
+
37
+ assert_equal({a: %i[a1 a2]}, result)
38
+ end
39
+
40
+ it "validates provided attributes" do
41
+ jat_class.plugin :json_api_validate_params
42
+ error = assert_raises(Jat::Error) { described_class.parse(a: "a1,a2,a3,a4") }
43
+ assert_match(/a4/, error.message)
44
+ end
37
45
  end
38
46
  end
@@ -3,11 +3,10 @@
3
3
  require "test_helper"
4
4
 
5
5
  describe "Jat::Plugins::JsonApi::Params::Include" do
6
- before { Jat::Plugins.find_plugin(:json_api) }
7
-
8
6
  let(:base_class) { Class.new(Jat) { plugin :json_api } }
9
7
  let(:a_serializer) { Class.new(base_class) }
10
8
  let(:b_serializer) { Class.new(base_class) }
9
+ let(:c_serializer) { Class.new(base_class) }
11
10
 
12
11
  before do
13
12
  ser = a_serializer
@@ -22,20 +21,35 @@ describe "Jat::Plugins::JsonApi::Params::Include" do
22
21
  ser = b_serializer
23
22
  ser.type :b
24
23
  ser.attribute :b1
25
- ser.relationship :b2, serializer: a_serializer
26
- ser.relationship :b3, serializer: a_serializer
27
- ser.relationship :b4, serializer: a_serializer
24
+ ser.relationship :b2, serializer: c_serializer
25
+ ser.relationship :b3, serializer: c_serializer
26
+ ser.relationship :b4, serializer: c_serializer
27
+
28
+ ser = c_serializer
29
+ ser.type :c
30
+ ser.attribute :c1
31
+ ser.relationship :c2, serializer: a_serializer
32
+ ser.relationship :c3, serializer: a_serializer
33
+ ser.relationship :c4, serializer: a_serializer
28
34
  end
29
35
 
30
36
  let(:described_class) { a_serializer::IncludeParamParser }
31
37
 
32
- it "returns empty hash when param not provided" do
33
- result = described_class.parse(nil)
34
- assert_equal({}, result)
38
+ describe ".inspect" do
39
+ it "returns self name" do
40
+ assert_equal "#{a_serializer}::IncludeParamParser", described_class.inspect
41
+ end
35
42
  end
36
43
 
37
- it "returns typed keys" do
38
- result = described_class.parse("a2.b2,a2.b3,a3.b2.a5,a4")
39
- assert_equal({a: %i[a2 a3 a5 a4], b: %i[b2 b3]}, result)
44
+ describe ".parse" do
45
+ it "returns empty hash when param not provided" do
46
+ result = described_class.parse(nil)
47
+ assert_equal({}, result)
48
+ end
49
+
50
+ it "returns typed keys" do
51
+ result = described_class.parse("a2.b2.c2,a2.b2.c3,a3.b2.c4.a5,a4")
52
+ assert_equal({a: %i[a2 a3 a5 a4], b: %i[b2], c: %i[c2 c3 c4]}, result)
53
+ end
40
54
  end
41
55
  end