jat 0.0.3 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (131) hide show
  1. checksums.yaml +4 -4
  2. data/lib/jat/attribute.rb +26 -5
  3. data/lib/jat/config.rb +4 -7
  4. data/lib/jat/plugins/activerecord/activerecord.rb +23 -0
  5. data/lib/jat/plugins/base/base_activerecord_preloads/base_activerecord_preloads.rb +38 -0
  6. data/lib/jat/plugins/{_activerecord_preloads → base/base_activerecord_preloads}/lib/preloader.rb +22 -16
  7. data/lib/jat/plugins/base/base_lower_camel_case/base_lower_camel_case.rb +36 -0
  8. data/lib/jat/plugins/{_preloads/_preloads.rb → base/base_preloads/base_preloads.rb} +14 -4
  9. data/lib/jat/plugins/{_preloads → base/base_preloads}/lib/format_user_preloads.rb +1 -1
  10. data/lib/jat/plugins/{_preloads → base/base_preloads}/lib/preloads_with_path.rb +1 -1
  11. data/lib/jat/plugins/cache/cache.rb +14 -6
  12. data/lib/jat/plugins/json_api/json_api.rb +150 -106
  13. data/lib/jat/plugins/json_api/lib/fields_param_parser.rb +40 -0
  14. data/lib/jat/plugins/json_api/lib/include_param_parser.rb +84 -0
  15. data/lib/jat/plugins/json_api/lib/map.rb +92 -27
  16. data/lib/jat/plugins/json_api/lib/params/fields/validate.rb +8 -5
  17. data/lib/jat/plugins/json_api/lib/response.rb +81 -197
  18. data/lib/jat/plugins/json_api/lib/response_piece.rb +175 -0
  19. data/lib/jat/plugins/json_api/plugins/json_api_activerecord/json_api_activerecord.rb +23 -0
  20. data/lib/jat/plugins/json_api/plugins/json_api_lower_camel_case/json_api_lower_camel_case.rb +34 -0
  21. data/lib/jat/plugins/json_api/plugins/json_api_maps_cache/json_api_maps_cache.rb +58 -0
  22. data/lib/jat/plugins/json_api/plugins/json_api_preloads/json_api_preloads.rb +38 -0
  23. data/lib/jat/plugins/{_json_api_activerecord → json_api/plugins/json_api_preloads}/lib/preloads.rb +17 -25
  24. data/lib/jat/plugins/json_api/plugins/json_api_validate_params/json_api_validate_params.rb +61 -0
  25. data/lib/jat/plugins/json_api/plugins/json_api_validate_params/lib/params_error.rb +6 -0
  26. data/lib/jat/plugins/json_api/plugins/json_api_validate_params/lib/validate_fields_param.rb +59 -0
  27. data/lib/jat/plugins/json_api/plugins/json_api_validate_params/lib/validate_include_param.rb +33 -0
  28. data/lib/jat/plugins/lower_camel_case/lower_camel_case.rb +23 -0
  29. data/lib/jat/plugins/maps_cache/maps_cache.rb +23 -0
  30. data/lib/jat/plugins/preloads/preloads.rb +23 -0
  31. data/lib/jat/plugins/simple_api/lib/fields_param_parser.rb +97 -0
  32. data/lib/jat/plugins/simple_api/lib/map.rb +80 -10
  33. data/lib/jat/plugins/simple_api/lib/response.rb +75 -90
  34. data/lib/jat/plugins/simple_api/lib/response_piece.rb +80 -0
  35. data/lib/jat/plugins/simple_api/plugins/simple_api_activerecord/simple_api_activerecord.rb +23 -0
  36. data/lib/jat/plugins/simple_api/plugins/simple_api_lower_camel_case/simple_api_lower_camel_case.rb +34 -0
  37. data/lib/jat/plugins/simple_api/plugins/simple_api_maps_cache/simple_api_maps_cache.rb +50 -0
  38. data/lib/jat/plugins/simple_api/plugins/simple_api_preloads/lib/preloads.rb +55 -0
  39. data/lib/jat/plugins/simple_api/plugins/simple_api_preloads/simple_api_preloads.rb +38 -0
  40. data/lib/jat/plugins/simple_api/plugins/simple_api_validate_params/lib/fields_error.rb +6 -0
  41. data/lib/jat/plugins/simple_api/plugins/simple_api_validate_params/lib/validate_fields_param.rb +45 -0
  42. data/lib/jat/plugins/simple_api/plugins/simple_api_validate_params/simple_api_validate_params.rb +49 -0
  43. data/lib/jat/plugins/simple_api/simple_api.rb +87 -27
  44. data/lib/jat/plugins/to_str/to_str.rb +15 -5
  45. data/lib/jat/plugins/types/types.rb +54 -0
  46. data/lib/jat/plugins/validate_params/validate_params.rb +23 -0
  47. data/lib/jat/plugins.rb +42 -30
  48. data/lib/jat.rb +40 -35
  49. data/test/lib/jat/attribute_test.rb +22 -4
  50. data/test/lib/jat/plugins/activerecord/activerecord_test.rb +27 -0
  51. data/test/lib/jat/plugins/base_activerecord_preloads/base_activerecord_preloads_test.rb +59 -0
  52. data/test/lib/jat/plugins/{_activerecord_preloads → base_activerecord_preloads}/lib/preloader_test.rb +11 -25
  53. data/test/lib/jat/plugins/base_lower_camel_case/base_lower_camel_case_test.rb +26 -0
  54. data/test/lib/jat/plugins/{_preloads/_preloads_test.rb → base_preloads/base_preloads_test.rb} +2 -2
  55. data/test/lib/jat/plugins/{_preloads → base_preloads}/lib/format_user_preloads_test.rb +3 -3
  56. data/test/lib/jat/plugins/{_preloads → base_preloads}/lib/preloads_with_path_test.rb +2 -2
  57. data/test/lib/jat/plugins/cache/cache_test.rb +11 -11
  58. data/test/lib/jat/plugins/json_api/json_api_test.rb +57 -49
  59. data/test/lib/jat/plugins/json_api/lib/fields_param_parser_test.rb +46 -0
  60. data/test/lib/jat/plugins/json_api/lib/include_param_parser_test.rb +57 -0
  61. data/test/lib/jat/plugins/json_api/lib/map_test.rb +171 -92
  62. data/test/lib/jat/plugins/json_api/lib/response_piece_test.rb +13 -0
  63. data/test/lib/jat/plugins/json_api/lib/response_test.rb +38 -32
  64. data/test/lib/jat/plugins/json_api_activerecord/json_api_activerecord_test.rb +24 -0
  65. data/test/lib/jat/plugins/json_api_lower_camel_case/json_api_lower_camel_case_test.rb +85 -0
  66. data/test/lib/jat/plugins/json_api_maps_cache/json_api_maps_cache_test.rb +107 -0
  67. data/test/lib/jat/plugins/json_api_preloads/json_api_preloads_test.rb +51 -0
  68. data/test/lib/jat/plugins/{_json_api_activerecord → json_api_preloads}/lib/preloads_test.rb +19 -13
  69. data/test/lib/jat/plugins/json_api_validate_params/json_api_validate_params_test.rb +108 -0
  70. data/test/lib/jat/plugins/lower_camel_case/lower_camel_case_test.rb +27 -0
  71. data/test/lib/jat/plugins/maps_cache/maps_cache_test.rb +27 -0
  72. data/test/lib/jat/plugins/preloads/preloads_test.rb +27 -0
  73. data/test/lib/jat/plugins/simple_api/lib/fields_param_parser_test.rb +87 -0
  74. data/test/lib/jat/plugins/simple_api/lib/map_test.rb +118 -33
  75. data/test/lib/jat/plugins/simple_api/lib/response_piece_test.rb +13 -0
  76. data/test/lib/jat/plugins/simple_api/lib/response_test.rb +90 -76
  77. data/test/lib/jat/plugins/simple_api/simple_api_test.rb +118 -25
  78. data/test/lib/jat/plugins/simple_api_activerecord/simple_api_activerecord_test.rb +24 -0
  79. data/test/lib/jat/plugins/simple_api_lower_camel_case/simple_api_lower_camel_case_test.rb +54 -0
  80. data/test/lib/jat/plugins/simple_api_maps_cache/simple_api_maps_cache_test.rb +95 -0
  81. data/test/lib/jat/plugins/simple_api_preloads/lib/preloads_test.rb +140 -0
  82. data/test/lib/jat/plugins/simple_api_preloads/simple_api_preloads_test.rb +51 -0
  83. data/test/lib/jat/plugins/simple_api_validate_params/simple_api_validate_params_test.rb +101 -0
  84. data/test/lib/jat/plugins/to_str/to_str_test.rb +3 -3
  85. data/test/lib/jat/plugins/types/types_test.rb +84 -0
  86. data/test/lib/jat/plugins/validate_params/validate_params_test.rb +27 -0
  87. data/test/lib/jat/plugins_test.rb +88 -0
  88. data/test/lib/jat_test.rb +47 -24
  89. data/test/lib/plugin_test.rb +3 -3
  90. data/test/test_helper.rb +7 -6
  91. data/test/test_plugin.rb +9 -12
  92. metadata +107 -88
  93. data/CHANGELOG.md +0 -7
  94. data/README.md +0 -21
  95. data/jat.gemspec +0 -37
  96. data/lib/jat/plugins/_activerecord_preloads/_activerecord_preloads.rb +0 -29
  97. data/lib/jat/plugins/_json_api_activerecord/_json_api_activerecord.rb +0 -22
  98. data/lib/jat/plugins/camel_lower/camel_lower.rb +0 -18
  99. data/lib/jat/plugins/json_api/lib/construct_traversal_map.rb +0 -91
  100. data/lib/jat/plugins/json_api/lib/presenters/document_links_presenter.rb +0 -48
  101. data/lib/jat/plugins/json_api/lib/presenters/document_meta_presenter.rb +0 -48
  102. data/lib/jat/plugins/json_api/lib/presenters/jsonapi_presenter.rb +0 -48
  103. data/lib/jat/plugins/json_api/lib/presenters/links_presenter.rb +0 -48
  104. data/lib/jat/plugins/json_api/lib/presenters/meta_presenter.rb +0 -48
  105. data/lib/jat/plugins/json_api/lib/presenters/relationship_links_presenter.rb +0 -53
  106. data/lib/jat/plugins/json_api/lib/presenters/relationship_meta_presenter.rb +0 -53
  107. data/lib/jat/plugins/json_api/lib/traversal_map.rb +0 -34
  108. data/lib/jat/plugins/simple_api/lib/construct_traversal_map.rb +0 -45
  109. data/lib/jat/plugins/simple_api/lib/params/parse.rb +0 -68
  110. data/lib/jat/presenter.rb +0 -51
  111. data/test/lib/jat/plugins/_activerecord_preloads/_activerecord_preloads_test.rb +0 -40
  112. data/test/lib/jat/plugins/_json_api_activerecord/_json_api_activerecord_test.rb +0 -29
  113. data/test/lib/jat/plugins/camel_lower/camel_lower_test.rb +0 -78
  114. data/test/lib/jat/plugins/json_api/lib/construct_traversal_map_test.rb +0 -119
  115. data/test/lib/jat/plugins/json_api/lib/params/fields/parse_test.rb +0 -24
  116. data/test/lib/jat/plugins/json_api/lib/params/fields/validate_test.rb +0 -47
  117. data/test/lib/jat/plugins/json_api/lib/params/fields_test.rb +0 -37
  118. data/test/lib/jat/plugins/json_api/lib/params/include/parse_test.rb +0 -46
  119. data/test/lib/jat/plugins/json_api/lib/params/include/validate_test.rb +0 -51
  120. data/test/lib/jat/plugins/json_api/lib/params/include_test.rb +0 -41
  121. data/test/lib/jat/plugins/json_api/lib/presenters/document_links_presenter_test.rb +0 -69
  122. data/test/lib/jat/plugins/json_api/lib/presenters/document_meta_presenter_test.rb +0 -69
  123. data/test/lib/jat/plugins/json_api/lib/presenters/jsonapi_presenter_test.rb +0 -69
  124. data/test/lib/jat/plugins/json_api/lib/presenters/links_presenter_test.rb +0 -69
  125. data/test/lib/jat/plugins/json_api/lib/presenters/meta_presenter_test.rb +0 -69
  126. data/test/lib/jat/plugins/json_api/lib/presenters/relationship_links_presenter_test.rb +0 -75
  127. data/test/lib/jat/plugins/json_api/lib/presenters/relationship_meta_presenter_test.rb +0 -75
  128. data/test/lib/jat/plugins/json_api/lib/traversal_map_test.rb +0 -58
  129. data/test/lib/jat/plugins/simple_api/lib/construct_traversal_map_test.rb +0 -100
  130. data/test/lib/jat/plugins/simple_api/lib/params/parse_test.rb +0 -71
  131. data/test/lib/jat/presenter_test.rb +0 -61
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+
5
+ describe "Jat::Plugins::BaseLowerCamelCase" do
6
+ let(:jat_class) do
7
+ new_class = Class.new(Jat)
8
+ new_class.plugin(:base_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
@@ -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.load_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.load_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: {}}
@@ -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", context).to_h
32
- result2 = jat_class.new("OBJECT", context).to_h
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", context).to_h
41
- result2 = jat_class.new("OBJECT", context.merge(foo: :bazz)).to_h
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("OBJECT", {}).to_str
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", context).to_str
58
- result2 = jat_class.new("OBJECT", context).to_str
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", context).to_str
67
- result2 = jat_class.new("OBJECT", context.merge(foo: :bazz)).to_str
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", context).to_str
76
+ jat_class.new(context).to_str("OBJECT")
77
77
 
78
78
  assert_equal ["OBJECT.bar.to_str"], hash_storage.keys
79
79
  end
@@ -7,77 +7,85 @@ describe "Jat::Plugins::JsonApi" do
7
7
  new_class = Class.new(Jat)
8
8
  new_class.plugin(plugin)
9
9
  new_class.type :jat
10
- new_class.attribute :id, key: :itself
10
+ new_class.id key: :itself
11
11
  new_class
12
12
  end
13
13
 
14
- let(:plugin) { Jat::Plugins.load_plugin(:json_api) }
14
+ let(:plugin) { Jat::Plugins.find_plugin(:json_api) }
15
15
 
16
- describe ".after_load" do
17
- it "loads _json_api_activerecord plugin if activerecord option provided" do
16
+ describe ".before_load" do
17
+ it "checks some response plugin was already loaded" do
18
18
  jat_class = Class.new(Jat)
19
- jat_class.expects(:plugin).with(:_json_api_activerecord, activerecord: true)
20
-
21
- Jat::Plugins.after_load(plugin, jat_class, activerecord: true)
19
+ jat_class.config[:response_plugin_loaded] = :simple_api
20
+ error = assert_raises(Jat::Error) { jat_class.plugin(:json_api) }
21
+ assert_equal "Response plugin `simple_api` was already loaded before", error.message
22
22
  end
23
+ end
23
24
 
24
- it "registers Presenters constants" do
25
+ describe ".after_load" do
26
+ it "adds config variable with name of response plugin that was loaded" do
25
27
  jat_class = Class.new(Jat)
26
- Jat::Plugins.after_load(plugin, jat_class, activerecord: true)
27
-
28
- assert_equal jat_class::JsonapiPresenter.jat_class, jat_class
29
- assert_equal jat_class::LinksPresenter.jat_class, jat_class
30
- assert_equal jat_class::DocumentLinksPresenter.jat_class, jat_class
31
- assert_equal jat_class::RelationshipLinksPresenter.jat_class, jat_class
32
- assert_equal jat_class::MetaPresenter.jat_class, jat_class
33
- assert_equal jat_class::DocumentMetaPresenter.jat_class, jat_class
34
- assert_equal jat_class::RelationshipMetaPresenter.jat_class, jat_class
28
+ jat_class.plugin(:json_api)
29
+
30
+ assert_equal(:json_api, jat_class.config[:response_plugin_loaded])
35
31
  end
36
32
  end
37
33
 
38
34
  describe "InstanceMethods" do
39
35
  let(:jat) do
40
- jat_class.new("JAT", {})
36
+ jat_class.new
41
37
  end
42
38
 
43
39
  describe "#to_h" do
44
40
  it "returns response in json-api format" do
45
41
  expected_result = {data: {type: :jat, id: "JAT"}}
46
- assert_equal expected_result, jat.to_h
42
+ assert_equal expected_result, jat.to_h("JAT")
43
+ end
44
+ end
45
+
46
+ describe "#map" do
47
+ it "returns map for provided context" do
48
+ jat_class::Map.expects(:call).with("CONTEXT").returns "MAP"
49
+ assert_equal "MAP", jat_class.new("CONTEXT").map
50
+ end
51
+ end
52
+
53
+ describe "#map_full" do
54
+ it "returns memorized map with all fields exposed" do
55
+ jat_class::Map.expects(:call).with(exposed: :all).returns "MAP"
56
+ assert_equal "MAP", jat_class.map_full
57
+ assert_same jat_class.map_full, jat_class.map_full
47
58
  end
48
59
  end
49
60
 
50
- describe "#traversal_map" do
51
- it "returns memorized traversal_map object" do
52
- assert_equal jat.traversal_map.class, Jat::Plugins::JsonApi::TraversalMap
53
- assert_same jat.traversal_map, jat.traversal_map
61
+ describe "#map_exposed" do
62
+ it "returns memorized map with exposed by default fields" do
63
+ jat_class::Map.expects(:call).with(exposed: :default).returns "MAP"
64
+ assert_equal "MAP", jat_class.map_exposed
65
+ assert_same jat_class.map_exposed, jat_class.map_exposed
54
66
  end
55
67
  end
56
68
  end
57
69
 
58
70
  describe "ClassMethods" do
59
- describe ".relationship" do
60
- it "adds new attribute with required serializer" do
61
- jat_class.relationship(:foo, serializer: jat_class, exposed: true) { "block" }
62
-
63
- atribute = jat_class.attributes[:foo]
64
- assert_equal jat_class, atribute.serializer
65
- assert_equal true, atribute.exposed?
66
- assert_equal "block", jat_class::Presenter.new(nil, nil).foo
71
+ describe ".id" do
72
+ it "adds new `:id`" do
73
+ jat_class.id(key: :uuid)
74
+ assert_equal jat_class.get_id.params, {name: :id, opts: {key: :uuid}, block: nil}
67
75
  end
68
76
  end
69
77
 
70
78
  describe ".inherited" do
71
79
  it "inherits type" do
72
80
  child = Class.new(jat_class)
73
- assert_equal :jat, child.type
81
+ assert_equal :jat, child.get_type
74
82
  end
75
83
 
76
84
  it "inherits `jsonapi` data" do
77
85
  jat_class.jsonapi(:version) { "1.0" }
78
86
 
79
87
  child = Class.new(jat_class)
80
- assert_equal("1.0", child.jsonapi_data[:version].call)
88
+ assert_equal("1.0", child.jsonapi_data[:version].value(nil, nil))
81
89
  end
82
90
 
83
91
  it "inherits `object_links`, `relationship_links`, `document_links`" do
@@ -86,9 +94,9 @@ describe "Jat::Plugins::JsonApi" do
86
94
  jat_class.document_link(:last) { "/articles/3" }
87
95
  child = Class.new(jat_class)
88
96
 
89
- assert_equal("/articles/1", child.object_links[:self].call)
90
- assert_equal("/articles/2", child.relationship_links[:related].call)
91
- assert_equal("/articles/3", child.document_links[:last].call)
97
+ assert_equal("/articles/1", child.object_links[:self].value(nil, nil))
98
+ assert_equal("/articles/2", child.relationship_links[:related].value(nil, nil))
99
+ assert_equal("/articles/3", child.document_links[:last].value(nil, nil))
92
100
  end
93
101
 
94
102
  it "does not change parents links when children links are changed" do
@@ -101,9 +109,9 @@ describe "Jat::Plugins::JsonApi" do
101
109
  child.relationship_links.delete(:related)
102
110
  child.document_links.delete(:last)
103
111
 
104
- assert_equal("/articles/1", jat_class.object_links[:self].call)
105
- assert_equal("/articles/2", jat_class.relationship_links[:related].call)
106
- assert_equal("/articles/3", jat_class.document_links[:last].call)
112
+ assert_equal("/articles/1", jat_class.object_links[:self].value(nil, nil))
113
+ assert_equal("/articles/2", jat_class.relationship_links[:related].value(nil, nil))
114
+ assert_equal("/articles/3", jat_class.document_links[:last].value(nil, nil))
107
115
  end
108
116
 
109
117
  it "inherits `object_meta`, `relationship_meta`, `document_meta`" do
@@ -112,9 +120,9 @@ describe "Jat::Plugins::JsonApi" do
112
120
  jat_class.document_meta(:last) { "foo/3" }
113
121
  child = Class.new(jat_class)
114
122
 
115
- assert_equal("foo/1", child.added_object_meta[:self].call)
116
- assert_equal("foo/2", child.added_relationship_meta[:related].call)
117
- assert_equal("foo/3", child.added_document_meta[:last].call)
123
+ assert_equal("foo/1", child.added_object_meta[:self].value(nil, nil))
124
+ assert_equal("foo/2", child.added_relationship_meta[:related].value(nil, nil))
125
+ assert_equal("foo/3", child.added_document_meta[:last].value(nil, nil))
118
126
  end
119
127
 
120
128
  it "does not change parents meta when children meta changed" do
@@ -127,27 +135,27 @@ describe "Jat::Plugins::JsonApi" do
127
135
  child.added_relationship_meta.delete(:related)
128
136
  child.added_document_meta.delete(:last)
129
137
 
130
- assert_equal("foo/1", jat_class.added_object_meta[:self].call)
131
- assert_equal("foo/2", jat_class.added_relationship_meta[:related].call)
132
- assert_equal("foo/3", jat_class.added_document_meta[:last].call)
138
+ assert_equal("foo/1", jat_class.added_object_meta[:self].value(nil, nil))
139
+ assert_equal("foo/2", jat_class.added_relationship_meta[:related].value(nil, nil))
140
+ assert_equal("foo/3", jat_class.added_document_meta[:last].value(nil, nil))
133
141
  end
134
142
  end
135
143
 
136
- describe ".type" do
144
+ describe ".type, .get_type" do
137
145
  it "does not allows to ask for type before type is defined" do
138
146
  new_class = Class.new(Jat) { plugin(:json_api) }
139
147
 
140
- error = assert_raises(Jat::Error) { new_class.type }
148
+ error = assert_raises(Jat::Error) { new_class.get_type }
141
149
  assert_equal "#{new_class} has no defined type", error.message
142
150
  end
143
151
 
144
152
  it "saves and returns current type" do
145
- assert_equal :jat, jat_class.type
153
+ assert_equal :jat, jat_class.get_type
146
154
  end
147
155
 
148
156
  it "symbolizes type" do
149
157
  jat_class.type "users"
150
- assert_equal :users, jat_class.type
158
+ assert_equal :users, jat_class.get_type
151
159
  end
152
160
  end
153
161
  end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+
5
+ describe "Jat::Plugins::JsonApi::FieldsParamParser" do
6
+ before { Jat::Plugins.find_plugin(:json_api) }
7
+
8
+ let(:jat_class) do
9
+ Class.new(Jat) do
10
+ plugin :json_api
11
+ type :a
12
+
13
+ attribute :a1
14
+ attribute :a2
15
+ attribute :a3
16
+ end
17
+ end
18
+
19
+ let(:described_class) { jat_class::FieldsParamParser }
20
+
21
+ describe ".inspect" do
22
+ it "returns self name" do
23
+ assert_equal "#{jat_class}::FieldsParamParser", described_class.inspect
24
+ end
25
+ end
26
+
27
+ describe ".parse" do
28
+ it "returns empty hash when parameters not provided" do
29
+ result = described_class.parse(nil)
30
+
31
+ assert_equal({}, result)
32
+ end
33
+
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
45
+ end
46
+ end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+
5
+ describe "Jat::Plugins::JsonApi::Params::Include" do
6
+ before { Jat::Plugins.find_plugin(:json_api) }
7
+
8
+ let(:base_class) { Class.new(Jat) { plugin :json_api } }
9
+ let(:a_serializer) { Class.new(base_class) }
10
+ let(:b_serializer) { Class.new(base_class) }
11
+ let(:c_serializer) { Class.new(base_class) }
12
+
13
+ before do
14
+ ser = a_serializer
15
+ ser.type :a
16
+ ser.attribute :a1
17
+ ser.relationship :a2, serializer: b_serializer
18
+ ser.relationship :a3, serializer: b_serializer
19
+ ser.relationship :a4, serializer: b_serializer
20
+ ser.relationship :a5, serializer: b_serializer
21
+ ser.relationship :a6, serializer: b_serializer
22
+
23
+ ser = b_serializer
24
+ ser.type :b
25
+ ser.attribute :b1
26
+ ser.relationship :b2, serializer: c_serializer
27
+ ser.relationship :b3, serializer: c_serializer
28
+ ser.relationship :b4, serializer: c_serializer
29
+
30
+ ser = c_serializer
31
+ ser.type :c
32
+ ser.attribute :c1
33
+ ser.relationship :c2, serializer: a_serializer
34
+ ser.relationship :c3, serializer: a_serializer
35
+ ser.relationship :c4, serializer: a_serializer
36
+ end
37
+
38
+ let(:described_class) { a_serializer::IncludeParamParser }
39
+
40
+ describe ".inspect" do
41
+ it "returns self name" do
42
+ assert_equal "#{a_serializer}::IncludeParamParser", described_class.inspect
43
+ end
44
+ end
45
+
46
+ describe ".parse" do
47
+ it "returns empty hash when param not provided" do
48
+ result = described_class.parse(nil)
49
+ assert_equal({}, result)
50
+ end
51
+
52
+ it "returns typed keys" do
53
+ result = described_class.parse("a2.b2.c2,a2.b2.c3,a3.b2.c4.a5,a4")
54
+ assert_equal({a: %i[a2 a3 a5 a4], b: %i[b2], c: %i[c2 c3 c4]}, result)
55
+ end
56
+ end
57
+ end