jat 0.0.1 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (88) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +7 -0
  3. data/README.md +21 -0
  4. data/jat.gemspec +37 -0
  5. data/lib/jat/attribute.rb +85 -0
  6. data/lib/jat/config.rb +38 -0
  7. data/lib/jat/plugins/_activerecord_preloads/_activerecord_preloads.rb +29 -0
  8. data/lib/jat/plugins/_activerecord_preloads/lib/preloader.rb +89 -0
  9. data/lib/jat/plugins/_json_api_activerecord/_json_api_activerecord.rb +22 -0
  10. data/lib/jat/plugins/_json_api_activerecord/lib/preloads.rb +84 -0
  11. data/lib/jat/plugins/_preloads/_preloads.rb +53 -0
  12. data/lib/jat/plugins/_preloads/lib/format_user_preloads.rb +52 -0
  13. data/lib/jat/plugins/_preloads/lib/preloads_with_path.rb +78 -0
  14. data/lib/jat/plugins/cache/cache.rb +39 -0
  15. data/lib/jat/plugins/camel_lower/camel_lower.rb +18 -0
  16. data/lib/jat/plugins/json_api/json_api.rb +207 -0
  17. data/lib/jat/plugins/json_api/lib/construct_traversal_map.rb +91 -0
  18. data/lib/jat/plugins/json_api/lib/map.rb +54 -0
  19. data/lib/jat/plugins/json_api/lib/params/fields/parse.rb +27 -0
  20. data/lib/jat/plugins/json_api/lib/params/fields/validate.rb +55 -0
  21. data/lib/jat/plugins/json_api/lib/params/fields.rb +23 -0
  22. data/lib/jat/plugins/json_api/lib/params/include/parse.rb +55 -0
  23. data/lib/jat/plugins/json_api/lib/params/include/validate.rb +29 -0
  24. data/lib/jat/plugins/json_api/lib/params/include.rb +49 -0
  25. data/lib/jat/plugins/json_api/lib/presenters/document_links_presenter.rb +48 -0
  26. data/lib/jat/plugins/json_api/lib/presenters/document_meta_presenter.rb +48 -0
  27. data/lib/jat/plugins/json_api/lib/presenters/jsonapi_presenter.rb +48 -0
  28. data/lib/jat/plugins/json_api/lib/presenters/links_presenter.rb +48 -0
  29. data/lib/jat/plugins/json_api/lib/presenters/meta_presenter.rb +48 -0
  30. data/lib/jat/plugins/json_api/lib/presenters/relationship_links_presenter.rb +53 -0
  31. data/lib/jat/plugins/json_api/lib/presenters/relationship_meta_presenter.rb +53 -0
  32. data/lib/jat/plugins/json_api/lib/response.rb +239 -0
  33. data/lib/jat/plugins/json_api/lib/traversal_map.rb +34 -0
  34. data/lib/jat/plugins/simple_api/lib/construct_traversal_map.rb +45 -0
  35. data/lib/jat/plugins/simple_api/lib/map.rb +29 -0
  36. data/lib/jat/plugins/simple_api/lib/params/parse.rb +68 -0
  37. data/lib/jat/plugins/simple_api/lib/response.rb +134 -0
  38. data/lib/jat/plugins/simple_api/simple_api.rb +65 -0
  39. data/lib/jat/plugins/to_str/to_str.rb +44 -0
  40. data/lib/jat/plugins.rb +39 -0
  41. data/lib/jat/presenter.rb +51 -0
  42. data/lib/jat/utils/enum_deep_dup.rb +29 -0
  43. data/lib/jat/utils/enum_deep_freeze.rb +19 -0
  44. data/lib/jat.rb +66 -144
  45. data/test/lib/jat/attribute_test.rb +142 -0
  46. data/test/lib/jat/config_test.rb +57 -0
  47. data/test/lib/jat/plugins/_activerecord_preloads/_activerecord_preloads_test.rb +40 -0
  48. data/test/lib/jat/plugins/_activerecord_preloads/lib/preloader_test.rb +98 -0
  49. data/test/lib/jat/plugins/_json_api_activerecord/_json_api_activerecord_test.rb +29 -0
  50. data/test/lib/jat/plugins/_json_api_activerecord/lib/preloads_test.rb +191 -0
  51. data/test/lib/jat/plugins/_preloads/_preloads_test.rb +68 -0
  52. data/test/lib/jat/plugins/_preloads/lib/format_user_preloads_test.rb +47 -0
  53. data/test/lib/jat/plugins/_preloads/lib/preloads_with_path_test.rb +33 -0
  54. data/test/lib/jat/plugins/cache/cache_test.rb +82 -0
  55. data/test/lib/jat/plugins/camel_lower/camel_lower_test.rb +78 -0
  56. data/test/lib/jat/plugins/json_api/json_api_test.rb +154 -0
  57. data/test/lib/jat/plugins/json_api/lib/construct_traversal_map_test.rb +119 -0
  58. data/test/lib/jat/plugins/json_api/lib/map_test.rb +117 -0
  59. data/test/lib/jat/plugins/json_api/lib/params/fields/parse_test.rb +24 -0
  60. data/test/lib/jat/plugins/json_api/lib/params/fields/validate_test.rb +47 -0
  61. data/test/lib/jat/plugins/json_api/lib/params/fields_test.rb +37 -0
  62. data/test/lib/jat/plugins/json_api/lib/params/include/parse_test.rb +46 -0
  63. data/test/lib/jat/plugins/json_api/lib/params/include/validate_test.rb +51 -0
  64. data/test/lib/jat/plugins/json_api/lib/params/include_test.rb +41 -0
  65. data/test/lib/jat/plugins/json_api/lib/presenters/document_links_presenter_test.rb +69 -0
  66. data/test/lib/jat/plugins/json_api/lib/presenters/document_meta_presenter_test.rb +69 -0
  67. data/test/lib/jat/plugins/json_api/lib/presenters/jsonapi_presenter_test.rb +69 -0
  68. data/test/lib/jat/plugins/json_api/lib/presenters/links_presenter_test.rb +69 -0
  69. data/test/lib/jat/plugins/json_api/lib/presenters/meta_presenter_test.rb +69 -0
  70. data/test/lib/jat/plugins/json_api/lib/presenters/relationship_links_presenter_test.rb +75 -0
  71. data/test/lib/jat/plugins/json_api/lib/presenters/relationship_meta_presenter_test.rb +75 -0
  72. data/test/lib/jat/plugins/json_api/lib/response_test.rb +489 -0
  73. data/test/lib/jat/plugins/json_api/lib/traversal_map_test.rb +58 -0
  74. data/test/lib/jat/plugins/simple_api/lib/construct_traversal_map_test.rb +100 -0
  75. data/test/lib/jat/plugins/simple_api/lib/map_test.rb +56 -0
  76. data/test/lib/jat/plugins/simple_api/lib/params/parse_test.rb +71 -0
  77. data/test/lib/jat/plugins/simple_api/lib/response_test.rb +342 -0
  78. data/test/lib/jat/plugins/simple_api/simple_api_test.rb +81 -0
  79. data/test/lib/jat/plugins/to_str/to_str_test.rb +52 -0
  80. data/test/lib/jat/presenter_test.rb +61 -0
  81. data/test/lib/jat/utils/enum_deep_dup_test.rb +31 -0
  82. data/test/lib/jat/utils/enum_deep_freeze_test.rb +28 -0
  83. data/test/lib/jat_test.rb +120 -0
  84. data/test/lib/plugin_test.rb +49 -0
  85. data/test/support/activerecord.rb +24 -0
  86. data/test/test_helper.rb +16 -0
  87. data/test/test_plugin.rb +59 -0
  88. metadata +240 -11
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+
5
+ describe "Jat::Plugins::JsonApi::Params::Include::Parse" do
6
+ before { Jat::Plugins.load_plugin(:json_api) }
7
+
8
+ let(:described_class) { Jat::Plugins::JsonApi::Params::Include::Parse }
9
+
10
+ it "returns empty hash when param not provided" do
11
+ result = described_class.call(nil)
12
+
13
+ assert_equal({}, result)
14
+ end
15
+
16
+ it "returns hash when single element" do
17
+ result = described_class.call("foo")
18
+
19
+ assert_equal({foo: {}}, result)
20
+ end
21
+
22
+ it "returns hash when multiple elements" do
23
+ result = described_class.call("foo,bar,bazz")
24
+
25
+ assert_equal({foo: {}, bar: {}, bazz: {}}, result)
26
+ end
27
+
28
+ it "returns hash when nested elements" do
29
+ result = described_class.call("foo.bar.bazz")
30
+
31
+ assert_equal({foo: {bar: {bazz: {}}}}, result)
32
+ end
33
+
34
+ it "returns hash when multiple nested elements" do
35
+ result = described_class.call("foo,bar.bazz,bar.bazzz,test.test1.test2,test.test1.test3")
36
+
37
+ assert_equal(
38
+ {
39
+ foo: {},
40
+ bar: {bazz: {}, bazzz: {}},
41
+ test: {test1: {test2: {}, test3: {}}}
42
+ },
43
+ result
44
+ )
45
+ end
46
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+
5
+ describe "Jat::Plugins::JsonApi::Params::Include::Validate" do
6
+ before { Jat::Plugins.load_plugin(:json_api) }
7
+
8
+ let(:described_class) { Jat::Plugins::JsonApi::Params::Include::Validate }
9
+
10
+ let(:base_class) { Class.new(Jat) { plugin :json_api } }
11
+ let(:a_serializer) { Class.new(base_class) }
12
+ let(:b_serializer) { Class.new(base_class) }
13
+
14
+ before do
15
+ ser = a_serializer
16
+ ser.type :a
17
+ ser.attribute :a1
18
+ ser.relationship :a2, serializer: b_serializer
19
+ ser.relationship :a3, serializer: b_serializer
20
+
21
+ ser = b_serializer
22
+ ser.type :b
23
+ ser.attribute :b1
24
+ ser.relationship :b2, serializer: a_serializer
25
+ end
26
+
27
+ it "does not raises error when all `include` values are valid" do
28
+ include_param = {a2: {b2: {a2: {b2: {a2: {}}}}}, a3: {b2: {a2: {b2: {a2: {}}}}}}
29
+ assert described_class.call(a_serializer, include_param)
30
+ end
31
+
32
+ it "raises error when some key is invalid" do
33
+ error = assert_raises(Jat::Error) { described_class.call(a_serializer, foo: {}) }
34
+ assert_equal "#{a_serializer} has no `foo` relationship", error.message
35
+ end
36
+
37
+ it "raises error when some children key is invalid" do
38
+ error = assert_raises(Jat::Error) { described_class.call(a_serializer, a2: {foo: {}}) }
39
+ assert_equal "#{b_serializer} has no `foo` relationship", error.message
40
+ end
41
+
42
+ it "raises error when trying to include attribute, not relationship" do
43
+ error = assert_raises(Jat::Error) { described_class.call(a_serializer, a1: {}) }
44
+ assert_equal "#{a_serializer} has no `a1` relationship", error.message
45
+ end
46
+
47
+ it "raises error when trying to include not existing nested attribute" do
48
+ error = assert_raises(Jat::Error) { described_class.call(a_serializer, a2: {b1: {}}) }
49
+ assert_equal "#{b_serializer} has no `b1` relationship", error.message
50
+ end
51
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+
5
+ describe "Jat::Plugins::JsonApi::Params::Include" do
6
+ before { Jat::Plugins.load_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
+
12
+ before do
13
+ ser = a_serializer
14
+ ser.type :a
15
+ ser.attribute :a1
16
+ ser.relationship :a2, serializer: b_serializer
17
+ ser.relationship :a3, serializer: b_serializer
18
+ ser.relationship :a4, serializer: b_serializer
19
+ ser.relationship :a5, serializer: b_serializer
20
+ ser.relationship :a6, serializer: b_serializer
21
+
22
+ ser = b_serializer
23
+ ser.type :b
24
+ ser.attribute :b1
25
+ ser.relationship :b2, serializer: a_serializer
26
+ ser.relationship :b3, serializer: a_serializer
27
+ ser.relationship :b4, serializer: a_serializer
28
+ end
29
+
30
+ let(:described_class) { Jat::Plugins::JsonApi::Params::Include }
31
+
32
+ it "returns empty hash when param not provided" do
33
+ result = described_class.call(a_serializer.allocate, nil)
34
+ assert_equal({}, result)
35
+ end
36
+
37
+ it "returns typed keys" do
38
+ result = described_class.call(a_serializer.allocate, "a2.b2,a2.b3,a3.b2.a5,a4")
39
+ assert_equal({a: %i[a2 a3 a5 a4], b: %i[b2 b3]}, result)
40
+ end
41
+ end
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+
5
+ describe "Jat::Presenters::DocumentLinksPresenter" do
6
+ before { Jat::Plugins.load_plugin(:json_api) }
7
+
8
+ let(:jat_class) { Class.new(Jat) { plugin :json_api } }
9
+ let(:presenter_class) { jat_class::DocumentLinksPresenter }
10
+
11
+ describe ".jat_class=" do
12
+ it "assigns @jat_class" do
13
+ presenter_class.jat_class = :foo
14
+ assert_equal :foo, presenter_class.instance_variable_get(:@jat_class)
15
+ end
16
+ end
17
+
18
+ describe ".jat_class" do
19
+ it "returns self @jat_class" do
20
+ assert_same jat_class, presenter_class.instance_variable_get(:@jat_class)
21
+ assert_same jat_class, presenter_class.jat_class
22
+ end
23
+ end
24
+
25
+ describe ".inspect" do
26
+ it "returns self name" do
27
+ assert_equal "#{jat_class}::Presenters::DocumentLinksPresenter", presenter_class.inspect
28
+ end
29
+ end
30
+
31
+ describe ".add_method" do
32
+ let(:presenter) { presenter_class.new("OBJECT", "CONTEXT") }
33
+
34
+ it "adds method by providing block without variables" do
35
+ presenter_class.add_method(:foo, proc { [object, context] })
36
+ assert_equal %w[OBJECT CONTEXT], presenter.foo
37
+ end
38
+
39
+ it "adds method by providing block with one variables" do
40
+ presenter_class.add_method(:foo, proc { |obj| [obj, object, context] })
41
+ assert_equal %w[OBJECT OBJECT CONTEXT], presenter.foo
42
+ end
43
+
44
+ it "adds method by providing block with two variables" do
45
+ presenter_class.add_method(:foo, proc { |obj, ctx| [obj, ctx, object, context] })
46
+ assert_equal %w[OBJECT CONTEXT OBJECT CONTEXT], presenter.foo
47
+ end
48
+
49
+ it "raises error when block has more than two variables" do
50
+ error = assert_raises(Jat::Error) { presenter_class.add_method(:foo, proc { |_a, _b, _c| }) }
51
+ assert_equal "Invalid block arguments count", error.message
52
+ end
53
+
54
+ it "redefines_method" do
55
+ presenter_class.add_method(:foo, proc { "OLD" })
56
+ presenter_class.add_method(:foo, proc { "NEW" })
57
+ assert_equal "NEW", presenter.foo
58
+ end
59
+ end
60
+
61
+ describe "#initialize" do
62
+ it "initializes object presenter with provided object and context" do
63
+ presenter = presenter_class.new("OBJ", "CTX")
64
+
65
+ assert_equal "OBJ", presenter.object
66
+ assert_equal "CTX", presenter.context
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+
5
+ describe "Jat::Presenters::DocumentMetaPresenter" do
6
+ before { Jat::Plugins.load_plugin(:json_api) }
7
+
8
+ let(:jat_class) { Class.new(Jat) { plugin :json_api } }
9
+ let(:presenter_class) { jat_class::DocumentMetaPresenter }
10
+
11
+ describe ".jat_class=" do
12
+ it "assigns @jat_class" do
13
+ presenter_class.jat_class = :foo
14
+ assert_equal :foo, presenter_class.instance_variable_get(:@jat_class)
15
+ end
16
+ end
17
+
18
+ describe ".jat_class" do
19
+ it "returns self @jat_class" do
20
+ assert_same jat_class, presenter_class.instance_variable_get(:@jat_class)
21
+ assert_same jat_class, presenter_class.jat_class
22
+ end
23
+ end
24
+
25
+ describe ".inspect" do
26
+ it "returns self name" do
27
+ assert_equal "#{jat_class}::Presenters::DocumentMetaPresenter", presenter_class.inspect
28
+ end
29
+ end
30
+
31
+ describe ".add_method" do
32
+ let(:presenter) { presenter_class.new("OBJECT", "CONTEXT") }
33
+
34
+ it "adds method by providing block without variables" do
35
+ presenter_class.add_method(:foo, proc { [object, context] })
36
+ assert_equal %w[OBJECT CONTEXT], presenter.foo
37
+ end
38
+
39
+ it "adds method by providing block with one variables" do
40
+ presenter_class.add_method(:foo, proc { |obj| [obj, object, context] })
41
+ assert_equal %w[OBJECT OBJECT CONTEXT], presenter.foo
42
+ end
43
+
44
+ it "adds method by providing block with two variables" do
45
+ presenter_class.add_method(:foo, proc { |obj, ctx| [obj, ctx, object, context] })
46
+ assert_equal %w[OBJECT CONTEXT OBJECT CONTEXT], presenter.foo
47
+ end
48
+
49
+ it "raises error when block has more than two variables" do
50
+ error = assert_raises(Jat::Error) { presenter_class.add_method(:foo, proc { |_a, _b, _c| }) }
51
+ assert_equal "Invalid block arguments count", error.message
52
+ end
53
+
54
+ it "redefines_method" do
55
+ presenter_class.add_method(:foo, proc { "OLD" })
56
+ presenter_class.add_method(:foo, proc { "NEW" })
57
+ assert_equal "NEW", presenter.foo
58
+ end
59
+ end
60
+
61
+ describe "#initialize" do
62
+ it "initializes object presenter with provided object and context" do
63
+ presenter = presenter_class.new("OBJ", "CTX")
64
+
65
+ assert_equal "OBJ", presenter.object
66
+ assert_equal "CTX", presenter.context
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+
5
+ describe "Jat::Presenters::JsonapiPresenter" do
6
+ before { Jat::Plugins.load_plugin(:json_api) }
7
+
8
+ let(:jat_class) { Class.new(Jat) { plugin :json_api } }
9
+ let(:presenter_class) { jat_class::JsonapiPresenter }
10
+
11
+ describe ".jat_class=" do
12
+ it "assigns @jat_class" do
13
+ presenter_class.jat_class = :foo
14
+ assert_equal :foo, presenter_class.instance_variable_get(:@jat_class)
15
+ end
16
+ end
17
+
18
+ describe ".jat_class" do
19
+ it "returns self @jat_class" do
20
+ assert_same jat_class, presenter_class.instance_variable_get(:@jat_class)
21
+ assert_same jat_class, presenter_class.jat_class
22
+ end
23
+ end
24
+
25
+ describe ".inspect" do
26
+ it "returns self name" do
27
+ assert_equal "#{jat_class}::Presenters::JsonapiPresenter", presenter_class.inspect
28
+ end
29
+ end
30
+
31
+ describe ".add_method" do
32
+ let(:presenter) { presenter_class.new("OBJECT", "CONTEXT") }
33
+
34
+ it "adds method by providing block without variables" do
35
+ presenter_class.add_method(:foo, proc { [object, context] })
36
+ assert_equal %w[OBJECT CONTEXT], presenter.foo
37
+ end
38
+
39
+ it "adds method by providing block with one variables" do
40
+ presenter_class.add_method(:foo, proc { |obj| [obj, object, context] })
41
+ assert_equal %w[OBJECT OBJECT CONTEXT], presenter.foo
42
+ end
43
+
44
+ it "adds method by providing block with two variables" do
45
+ presenter_class.add_method(:foo, proc { |obj, ctx| [obj, ctx, object, context] })
46
+ assert_equal %w[OBJECT CONTEXT OBJECT CONTEXT], presenter.foo
47
+ end
48
+
49
+ it "raises error when block has more than two variables" do
50
+ error = assert_raises(Jat::Error) { presenter_class.add_method(:foo, proc { |_a, _b, _c| }) }
51
+ assert_equal "Invalid block arguments count", error.message
52
+ end
53
+
54
+ it "redefines_method" do
55
+ presenter_class.add_method(:foo, proc { "OLD" })
56
+ presenter_class.add_method(:foo, proc { "NEW" })
57
+ assert_equal "NEW", presenter.foo
58
+ end
59
+ end
60
+
61
+ describe "#initialize" do
62
+ it "initializes object presenter with provided object and context" do
63
+ presenter = presenter_class.new("OBJ", "CTX")
64
+
65
+ assert_equal "OBJ", presenter.object
66
+ assert_equal "CTX", presenter.context
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+
5
+ describe "Jat::Presenters::LinksPresenter" do
6
+ before { Jat::Plugins.load_plugin(:json_api) }
7
+
8
+ let(:jat_class) { Class.new(Jat) { plugin :json_api } }
9
+ let(:presenter_class) { jat_class::LinksPresenter }
10
+
11
+ describe ".jat_class=" do
12
+ it "assigns @jat_class" do
13
+ presenter_class.jat_class = :foo
14
+ assert_equal :foo, presenter_class.instance_variable_get(:@jat_class)
15
+ end
16
+ end
17
+
18
+ describe ".jat_class" do
19
+ it "returns self @jat_class" do
20
+ assert_same jat_class, presenter_class.instance_variable_get(:@jat_class)
21
+ assert_same jat_class, presenter_class.jat_class
22
+ end
23
+ end
24
+
25
+ describe ".inspect" do
26
+ it "returns self name" do
27
+ assert_equal "#{jat_class}::Presenters::LinksPresenter", presenter_class.inspect
28
+ end
29
+ end
30
+
31
+ describe ".add_method" do
32
+ let(:presenter) { presenter_class.new("OBJECT", "CONTEXT") }
33
+
34
+ it "adds method by providing block without variables" do
35
+ presenter_class.add_method(:foo, proc { [object, context] })
36
+ assert_equal %w[OBJECT CONTEXT], presenter.foo
37
+ end
38
+
39
+ it "adds method by providing block with one variables" do
40
+ presenter_class.add_method(:foo, proc { |obj| [obj, object, context] })
41
+ assert_equal %w[OBJECT OBJECT CONTEXT], presenter.foo
42
+ end
43
+
44
+ it "adds method by providing block with two variables" do
45
+ presenter_class.add_method(:foo, proc { |obj, ctx| [obj, ctx, object, context] })
46
+ assert_equal %w[OBJECT CONTEXT OBJECT CONTEXT], presenter.foo
47
+ end
48
+
49
+ it "raises error when block has more than two variables" do
50
+ error = assert_raises(Jat::Error) { presenter_class.add_method(:foo, proc { |_a, _b, _c| }) }
51
+ assert_equal "Invalid block arguments count", error.message
52
+ end
53
+
54
+ it "redefines_method" do
55
+ presenter_class.add_method(:foo, proc { "OLD" })
56
+ presenter_class.add_method(:foo, proc { "NEW" })
57
+ assert_equal "NEW", presenter.foo
58
+ end
59
+ end
60
+
61
+ describe "#initialize" do
62
+ it "initializes object presenter with provided object and context" do
63
+ presenter = presenter_class.new("OBJ", "CTX")
64
+
65
+ assert_equal "OBJ", presenter.object
66
+ assert_equal "CTX", presenter.context
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+
5
+ describe "Jat::Presenters::MetaPresenter" do
6
+ before { Jat::Plugins.load_plugin(:json_api) }
7
+
8
+ let(:jat_class) { Class.new(Jat) { plugin :json_api } }
9
+ let(:presenter_class) { jat_class::MetaPresenter }
10
+
11
+ describe ".jat_class=" do
12
+ it "assigns @jat_class" do
13
+ presenter_class.jat_class = :foo
14
+ assert_equal :foo, presenter_class.instance_variable_get(:@jat_class)
15
+ end
16
+ end
17
+
18
+ describe ".jat_class" do
19
+ it "returns self @jat_class" do
20
+ assert_same jat_class, presenter_class.instance_variable_get(:@jat_class)
21
+ assert_same jat_class, presenter_class.jat_class
22
+ end
23
+ end
24
+
25
+ describe ".inspect" do
26
+ it "returns self name" do
27
+ assert_equal "#{jat_class}::Presenters::MetaPresenter", presenter_class.inspect
28
+ end
29
+ end
30
+
31
+ describe ".add_method" do
32
+ let(:presenter) { presenter_class.new("OBJECT", "CONTEXT") }
33
+
34
+ it "adds method by providing block without variables" do
35
+ presenter_class.add_method(:foo, proc { [object, context] })
36
+ assert_equal %w[OBJECT CONTEXT], presenter.foo
37
+ end
38
+
39
+ it "adds method by providing block with one variables" do
40
+ presenter_class.add_method(:foo, proc { |obj| [obj, object, context] })
41
+ assert_equal %w[OBJECT OBJECT CONTEXT], presenter.foo
42
+ end
43
+
44
+ it "adds method by providing block with two variables" do
45
+ presenter_class.add_method(:foo, proc { |obj, ctx| [obj, ctx, object, context] })
46
+ assert_equal %w[OBJECT CONTEXT OBJECT CONTEXT], presenter.foo
47
+ end
48
+
49
+ it "raises error when block has more than two variables" do
50
+ error = assert_raises(Jat::Error) { presenter_class.add_method(:foo, proc { |_a, _b, _c| }) }
51
+ assert_equal "Invalid block arguments count", error.message
52
+ end
53
+
54
+ it "redefines_method" do
55
+ presenter_class.add_method(:foo, proc { "OLD" })
56
+ presenter_class.add_method(:foo, proc { "NEW" })
57
+ assert_equal "NEW", presenter.foo
58
+ end
59
+ end
60
+
61
+ describe "#initialize" do
62
+ it "initializes object presenter with provided object and context" do
63
+ presenter = presenter_class.new("OBJ", "CTX")
64
+
65
+ assert_equal "OBJ", presenter.object
66
+ assert_equal "CTX", presenter.context
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+
5
+ describe "Jat::Presenters::RelationshipLinksPresenter" do
6
+ before { Jat::Plugins.load_plugin(:json_api) }
7
+
8
+ let(:jat_class) { Class.new(Jat) { plugin :json_api } }
9
+ let(:presenter_class) { jat_class::RelationshipLinksPresenter }
10
+
11
+ describe ".jat_class=" do
12
+ it "assigns @jat_class" do
13
+ presenter_class.jat_class = :foo
14
+ assert_equal :foo, presenter_class.instance_variable_get(:@jat_class)
15
+ end
16
+ end
17
+
18
+ describe ".jat_class" do
19
+ it "returns self @jat_class" do
20
+ assert_same jat_class, presenter_class.instance_variable_get(:@jat_class)
21
+ assert_same jat_class, presenter_class.jat_class
22
+ end
23
+ end
24
+
25
+ describe ".inspect" do
26
+ it "returns self name" do
27
+ assert_equal "#{jat_class}::Presenters::RelationshipLinksPresenter", presenter_class.inspect
28
+ end
29
+ end
30
+
31
+ describe ".add_method" do
32
+ let(:presenter) { presenter_class.new("PARENT_OBJECT", "OBJECT", "CONTEXT") }
33
+
34
+ it "adds method by providing block without variables" do
35
+ presenter_class.add_method(:foo, proc { [parent_object, object, context] })
36
+ assert_equal %w[PARENT_OBJECT OBJECT CONTEXT], presenter.foo
37
+ end
38
+
39
+ it "adds method by providing block with one variables" do
40
+ presenter_class.add_method(:foo, proc { |parent_obj| [parent_obj, parent_object, object, context] })
41
+ assert_equal %w[PARENT_OBJECT PARENT_OBJECT OBJECT CONTEXT], presenter.foo
42
+ end
43
+
44
+ it "adds method by providing block with two variables" do
45
+ presenter_class.add_method(:foo, proc { |parent_obj, obj| [parent_obj, parent_object, obj, object, context] })
46
+ assert_equal %w[PARENT_OBJECT PARENT_OBJECT OBJECT OBJECT CONTEXT], presenter.foo
47
+ end
48
+
49
+ it "adds method by providing block with three variables" do
50
+ presenter_class.add_method(:foo, proc { |parent_obj, obj, ctx| [parent_obj, parent_object, obj, object, ctx, context] })
51
+ assert_equal %w[PARENT_OBJECT PARENT_OBJECT OBJECT OBJECT CONTEXT CONTEXT], presenter.foo
52
+ end
53
+
54
+ it "raises error when block has more than three variables" do
55
+ error = assert_raises(Jat::Error) { presenter_class.add_method(:foo, proc { |_a, _b, _c, _d| }) }
56
+ assert_equal "Invalid block arguments count", error.message
57
+ end
58
+
59
+ it "redefines_method" do
60
+ presenter_class.add_method(:foo, proc { "OLD" })
61
+ presenter_class.add_method(:foo, proc { "NEW" })
62
+ assert_equal "NEW", presenter.foo
63
+ end
64
+ end
65
+
66
+ describe "#initialize" do
67
+ it "initializes object presenter with provided object and context" do
68
+ presenter = presenter_class.new("PARENT", "OBJ", "CTX")
69
+
70
+ assert_equal "PARENT", presenter.parent_object
71
+ assert_equal "OBJ", presenter.object
72
+ assert_equal "CTX", presenter.context
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+
5
+ describe "Jat::Presenters::RelationshipMetaPresenter" do
6
+ before { Jat::Plugins.load_plugin(:json_api) }
7
+
8
+ let(:jat_class) { Class.new(Jat) { plugin :json_api } }
9
+ let(:presenter_class) { jat_class::RelationshipMetaPresenter }
10
+
11
+ describe ".jat_class=" do
12
+ it "assigns @jat_class" do
13
+ presenter_class.jat_class = :foo
14
+ assert_equal :foo, presenter_class.instance_variable_get(:@jat_class)
15
+ end
16
+ end
17
+
18
+ describe ".jat_class" do
19
+ it "returns self @jat_class" do
20
+ assert_same jat_class, presenter_class.instance_variable_get(:@jat_class)
21
+ assert_same jat_class, presenter_class.jat_class
22
+ end
23
+ end
24
+
25
+ describe ".inspect" do
26
+ it "returns self name" do
27
+ assert_equal "#{jat_class}::Presenters::RelationshipMetaPresenter", presenter_class.inspect
28
+ end
29
+ end
30
+
31
+ describe ".add_method" do
32
+ let(:presenter) { presenter_class.new("PARENT_OBJECT", "OBJECT", "CONTEXT") }
33
+
34
+ it "adds method by providing block without variables" do
35
+ presenter_class.add_method(:foo, proc { [parent_object, object, context] })
36
+ assert_equal %w[PARENT_OBJECT OBJECT CONTEXT], presenter.foo
37
+ end
38
+
39
+ it "adds method by providing block with one variables" do
40
+ presenter_class.add_method(:foo, proc { |parent_obj| [parent_obj, parent_object, object, context] })
41
+ assert_equal %w[PARENT_OBJECT PARENT_OBJECT OBJECT CONTEXT], presenter.foo
42
+ end
43
+
44
+ it "adds method by providing block with two variables" do
45
+ presenter_class.add_method(:foo, proc { |parent_obj, obj| [parent_obj, parent_object, obj, object, context] })
46
+ assert_equal %w[PARENT_OBJECT PARENT_OBJECT OBJECT OBJECT CONTEXT], presenter.foo
47
+ end
48
+
49
+ it "adds method by providing block with three variables" do
50
+ presenter_class.add_method(:foo, proc { |parent_obj, obj, ctx| [parent_obj, parent_object, obj, object, ctx, context] })
51
+ assert_equal %w[PARENT_OBJECT PARENT_OBJECT OBJECT OBJECT CONTEXT CONTEXT], presenter.foo
52
+ end
53
+
54
+ it "raises error when block has more than three variables" do
55
+ error = assert_raises(Jat::Error) { presenter_class.add_method(:foo, proc { |_a, _b, _c, _d| }) }
56
+ assert_equal "Invalid block arguments count", error.message
57
+ end
58
+
59
+ it "redefines_method" do
60
+ presenter_class.add_method(:foo, proc { "OLD" })
61
+ presenter_class.add_method(:foo, proc { "NEW" })
62
+ assert_equal "NEW", presenter.foo
63
+ end
64
+ end
65
+
66
+ describe "#initialize" do
67
+ it "initializes object presenter with provided object and context" do
68
+ presenter = presenter_class.new("PARENT", "OBJ", "CTX")
69
+
70
+ assert_equal "PARENT", presenter.parent_object
71
+ assert_equal "OBJ", presenter.object
72
+ assert_equal "CTX", presenter.context
73
+ end
74
+ end
75
+ end