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
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3fdf9f1733a9d7ddcdeced1bcf6b6cbb1f53b9ffa11b8cb1592874dcc55c3bf2
4
- data.tar.gz: d8982abf27a244a845ec0147a51e23946a8f6cac66c36d350aef6567253326f8
3
+ metadata.gz: c266d5784440b62adbb20bb09902f0134ad5e6a06245974f9b15a0557733528d
4
+ data.tar.gz: 989344e5918b193a2db72a863337c7209aa34ee9e1b28b7bf68f8bd9a9749b9d
5
5
  SHA512:
6
- metadata.gz: f8a9be4d5e0d59922b37763de59a8833b5d3c71ac634f2c3555b6b9ab36e8a4149be04f8b33a68406616cfaeda85f49ed349fe8cc4d47bc44e0a873f0128829a
7
- data.tar.gz: 9dcf9433b3d5f40e538db79e082095b54977f8ba8fd885ada3442441c98f374635956d0cd5201c654282954dd24d59e589a181338d2774855b97bd9677fa40d4
6
+ metadata.gz: eba6f92cd51c734ef0583447b2300971fd4707e0e79cece169858d6551e75fba688ce6155dae9317184d423b519e1398d8f583b643174cf4d4450700d73bf3c9
7
+ data.tar.gz: bf14496368b37ec614f0f79b76c217df8c60a8401a54d48e30925fad6c7501fdba7533758b9b5bde24035b645aae2c204b9fc05740282a4ff23ce5e80968e529
data/lib/jat/attribute.rb CHANGED
@@ -9,6 +9,8 @@ class Jat
9
9
  attr_reader :params, :opts
10
10
 
11
11
  def initialize(name:, opts: {}, block: nil)
12
+ check_block_valid(block)
13
+
12
14
  @opts = EnumDeepDup.call(opts)
13
15
  @params = EnumDeepFreeze.call(name: name, opts: @opts, block: block)
14
16
  end
@@ -59,11 +61,30 @@ class Jat
59
61
  end
60
62
 
61
63
  def block
62
- @block ||=
63
- params.fetch(:block) || begin
64
- key_method_name = key
65
- -> { object.public_send(key_method_name) }
66
- end
64
+ return @block if instance_variable_defined?(:@block)
65
+
66
+ current_block = params.fetch(:block)
67
+ current_block ||= keyword_block
68
+
69
+ @block = current_block
70
+ end
71
+
72
+ def value(object, context)
73
+ block.call(object, context)
74
+ end
75
+
76
+ private
77
+
78
+ def keyword_block
79
+ key_method_name = key
80
+ proc { |object| object.public_send(key_method_name) }
81
+ end
82
+
83
+ def check_block_valid(block)
84
+ return unless block
85
+
86
+ params = block.parameters
87
+ raise Error, "Block can have 0-2 parameters" if params.count > 2
67
88
  end
68
89
  end
69
90
 
data/lib/jat/config.rb CHANGED
@@ -1,23 +1,20 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "forwardable"
3
4
  require_relative "utils/enum_deep_dup"
4
5
 
5
6
  class Jat
6
7
  class Config
7
8
  module InstanceMethods
9
+ extend Forwardable
10
+
8
11
  attr_reader :opts
9
12
 
10
13
  def initialize(opts = {})
11
14
  @opts = EnumDeepDup.call(opts)
12
15
  end
13
16
 
14
- def []=(key, value)
15
- opts[key] = value
16
- end
17
-
18
- def [](key)
19
- opts[key]
20
- end
17
+ def_delegators :@opts, :[], :[]=, :fetch
21
18
  end
22
19
 
23
20
  module ClassMethods
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Jat
4
+ module Plugins
5
+ module Activerecord
6
+ def self.plugin_name
7
+ :activerecord
8
+ end
9
+
10
+ def self.load(jat_class, **opts)
11
+ if jat_class.plugin_used?(:json_api)
12
+ jat_class.plugin :json_api_activerecord, **opts
13
+ elsif jat_class.plugin_used?(:simple_api)
14
+ jat_class.plugin :simple_api_activerecord, **opts
15
+ else
16
+ raise Error, "Please load :json_api or :simple_api plugin first"
17
+ end
18
+ end
19
+ end
20
+
21
+ register_plugin(Activerecord.plugin_name, Activerecord)
22
+ end
23
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "./lib/preloader"
4
+
5
+ class Jat
6
+ module Plugins
7
+ module BaseActiverecordPreloads
8
+ def self.plugin_name
9
+ :base_activerecord_preloads
10
+ end
11
+
12
+ def self.load(jat_class, **_opts)
13
+ jat_class.include(InstanceMethods)
14
+ end
15
+
16
+ module InstanceMethods
17
+ def to_h(object)
18
+ object = add_preloads(object)
19
+ super
20
+ end
21
+
22
+ private
23
+
24
+ def add_preloads(obj)
25
+ return obj if obj.nil? || (obj.is_a?(Array) && obj.empty?)
26
+
27
+ # preloads() method comes from simple_api_activerecord or json_api_activerecord plugin
28
+ preloads = preloads()
29
+ return obj if preloads.empty?
30
+
31
+ Preloader.preload(obj, preloads)
32
+ end
33
+ end
34
+ end
35
+
36
+ register_plugin(BaseActiverecordPreloads.plugin_name, BaseActiverecordPreloads)
37
+ end
38
+ end
@@ -2,7 +2,7 @@
2
2
 
3
3
  class Jat
4
4
  module Plugins
5
- module ActiverecordPreloads
5
+ module BaseActiverecordPreloads
6
6
  class Preloader
7
7
  module ClassMethods
8
8
  def preload(object, preloads)
@@ -20,6 +20,18 @@ class Jat
20
20
  extend ClassMethods
21
21
  end
22
22
 
23
+ class Loader
24
+ # :nocov: We can check only one version of activerecord
25
+ def self.call(records, associations)
26
+ if ActiveRecord::VERSION::MAJOR >= 7
27
+ ActiveRecord::Associations::Preloader.new(records: records, associations: associations).call
28
+ else
29
+ ActiveRecord::Associations::Preloader.new.preload(records, associations)
30
+ end
31
+ end
32
+ # :nocov:
33
+ end
34
+
23
35
  class ActiverecordObject
24
36
  module ClassMethods
25
37
  def fit?(object)
@@ -27,11 +39,7 @@ class Jat
27
39
  end
28
40
 
29
41
  def preload(object, preloads)
30
- # Reset associations that will be preloaded to fix possible bugs with
31
- # ActiveRecord::Associations::Preloader
32
- preloads.each_key { |key| object.association(key).reset }
33
- ActiveRecord::Associations::Preloader.new.preload(object, preloads)
34
-
42
+ Loader.call([object], preloads)
35
43
  object
36
44
  end
37
45
  end
@@ -46,7 +54,13 @@ class Jat
46
54
  end
47
55
 
48
56
  def preload(objects, preloads)
49
- objects.preload(preloads)
57
+ if objects.loaded?
58
+ array_objects = objects.to_a
59
+ Loader.call(array_objects, preloads)
60
+ objects
61
+ else
62
+ objects.preload(preloads).load
63
+ end
50
64
  end
51
65
  end
52
66
 
@@ -62,20 +76,12 @@ class Jat
62
76
  end
63
77
 
64
78
  def preload(objects, preloads)
65
- # Reset associations that will be preloaded to fix possible bugs with
66
- # ActiveRecord::Associations::Preloader
67
- preloads.each_key { |key| reset_association(objects, key) }
68
- ActiveRecord::Associations::Preloader.new.preload(objects, preloads)
69
-
79
+ Loader.call(objects, preloads)
70
80
  objects
71
81
  end
72
82
 
73
83
  private
74
84
 
75
- def reset_association(objects, key)
76
- objects.each { |object| object.association(key).reset }
77
- end
78
-
79
85
  def same_kind?(objects)
80
86
  first_object_class = objects.first.class
81
87
  objects.all? { |object| object.instance_of?(first_object_class) }
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Jat
4
+ module Plugins
5
+ module BaseLowerCamelCase
6
+ def self.plugin_name
7
+ :base_lower_camel_case
8
+ end
9
+
10
+ def self.load(jat_class, **_opts)
11
+ jat_class::Attribute.include(AttributeInstanceMethods)
12
+ end
13
+
14
+ module AttributeInstanceMethods
15
+ def name
16
+ LowerCamelCaseTransformation.call(original_name)
17
+ end
18
+ end
19
+ end
20
+
21
+ register_plugin(BaseLowerCamelCase.plugin_name, BaseLowerCamelCase)
22
+ end
23
+
24
+ class LowerCamelCaseTransformation
25
+ SEPARATOR = "_"
26
+
27
+ def self.call(string)
28
+ first_word, *others = string.to_s.split(SEPARATOR)
29
+
30
+ first_word[0] = first_word[0].downcase
31
+ last_words = others.each(&:capitalize!).join
32
+
33
+ :"#{first_word}#{last_words}"
34
+ end
35
+ end
36
+ end
@@ -6,7 +6,15 @@ require_relative "./lib/preloads_with_path"
6
6
  # This plugin adds attribute methods #preloads, #preloads_path
7
7
  class Jat
8
8
  module Plugins
9
- module Preloads
9
+ module BasePreloads
10
+ def self.plugin_name
11
+ :base_preloads
12
+ end
13
+
14
+ def self.load(jat_class, **_opts)
15
+ jat_class::Attribute.include(AttributeMethods)
16
+ end
17
+
10
18
  module AttributeMethods
11
19
  NULL_PRELOADS = [nil, [].freeze].freeze
12
20
 
@@ -28,7 +36,7 @@ class Jat
28
36
  # we don't know which entity is main (:user or :profile in this example) but
29
37
  # we need to know main value to add nested preloads to it.
30
38
  # User can specify main preloaded entity by adding "!" suffix
31
- # ({ user!: [:profile] } for example), othervice the latest key will be considered main.
39
+ # ({ user!: [:profile] } for example), otherwise the latest key will be considered main.
32
40
  def get_preloads_with_path
33
41
  preloads_provided = opts.key?(:preload)
34
42
  preloads =
@@ -43,11 +51,13 @@ class Jat
43
51
  return NULL_PRELOADS if preloads_provided && !preloads
44
52
 
45
53
  preloads = FormatUserPreloads.to_hash(preloads)
46
- PreloadsWithPath.call(preloads)
54
+ preloads, path = PreloadsWithPath.call(preloads)
55
+
56
+ [EnumDeepFreeze.call(preloads), path.freeze]
47
57
  end
48
58
  end
49
59
  end
50
60
 
51
- register_plugin(:_preloads, Preloads)
61
+ register_plugin(BasePreloads.plugin_name, BasePreloads)
52
62
  end
53
63
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  class Jat
4
4
  module Plugins
5
- module Preloads
5
+ module BasePreloads
6
6
  class FormatUserPreloads
7
7
  METHODS = {
8
8
  Array => :array_to_hash,
@@ -2,7 +2,7 @@
2
2
 
3
3
  class Jat
4
4
  module Plugins
5
- module Preloads
5
+ module BasePreloads
6
6
  class PreloadsWithPath
7
7
  module ClassMethods
8
8
  BANG = "!"
@@ -3,29 +3,37 @@
3
3
  class Jat
4
4
  module Plugins
5
5
  module Cache
6
+ def self.plugin_name
7
+ :cache
8
+ end
9
+
6
10
  def self.before_load(jat_class, **opts)
7
11
  jat_class.plugin :to_str, **opts
8
12
  end
9
13
 
14
+ def self.load(jat_class, **_opts)
15
+ jat_class.include(InstanceMethods)
16
+ end
17
+
10
18
  module InstanceMethods
11
19
  FORMAT_TO_STR = :to_str
12
20
  FORMAT_TO_H = :to_h
13
21
 
14
- def to_h
22
+ def to_h(object)
15
23
  return super if context[:_format] == FORMAT_TO_STR
16
24
 
17
25
  context[:_format] = FORMAT_TO_H
18
- cached { super }
26
+ cached(object) { super }
19
27
  end
20
28
 
21
- def to_str
29
+ def to_str(object)
22
30
  context[:_format] = FORMAT_TO_STR
23
- cached { super }
31
+ cached(object) { super }
24
32
  end
25
33
 
26
34
  private
27
35
 
28
- def cached(&block)
36
+ def cached(object, &block)
29
37
  cache = context[:cache]
30
38
  return yield unless cache
31
39
 
@@ -34,6 +42,6 @@ class Jat
34
42
  end
35
43
  end
36
44
 
37
- register_plugin(:cache, Cache)
45
+ register_plugin(Cache.plugin_name, Cache)
38
46
  end
39
47
  end