cuprum-rails 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (113) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +145 -0
  3. data/DEVELOPMENT.md +20 -0
  4. data/README.md +356 -63
  5. data/lib/cuprum/rails/action.rb +32 -16
  6. data/lib/cuprum/rails/actions/create.rb +62 -15
  7. data/lib/cuprum/rails/actions/destroy.rb +23 -7
  8. data/lib/cuprum/rails/actions/edit.rb +23 -7
  9. data/lib/cuprum/rails/actions/index.rb +30 -10
  10. data/lib/cuprum/rails/actions/middleware/associations/cache.rb +112 -0
  11. data/lib/cuprum/rails/actions/middleware/associations/find.rb +23 -0
  12. data/lib/cuprum/rails/actions/middleware/associations/parent.rb +70 -0
  13. data/lib/cuprum/rails/actions/middleware/associations/query.rb +140 -0
  14. data/lib/cuprum/rails/actions/middleware/associations.rb +12 -0
  15. data/lib/cuprum/rails/actions/middleware/log_request.rb +126 -0
  16. data/lib/cuprum/rails/actions/middleware/log_result.rb +51 -0
  17. data/lib/cuprum/rails/actions/middleware/resources/find.rb +44 -0
  18. data/lib/cuprum/rails/actions/middleware/resources/query.rb +91 -0
  19. data/lib/cuprum/rails/actions/middleware/resources.rb +11 -0
  20. data/lib/cuprum/rails/actions/middleware.rb +13 -0
  21. data/lib/cuprum/rails/actions/new.rb +16 -4
  22. data/lib/cuprum/rails/actions/parameter_validation.rb +60 -0
  23. data/lib/cuprum/rails/actions/resource_action.rb +119 -42
  24. data/lib/cuprum/rails/actions/show.rb +23 -7
  25. data/lib/cuprum/rails/actions/update.rb +70 -22
  26. data/lib/cuprum/rails/actions.rb +11 -7
  27. data/lib/cuprum/rails/collection.rb +27 -47
  28. data/lib/cuprum/rails/command.rb +3 -1
  29. data/lib/cuprum/rails/commands/destroy_one.rb +10 -6
  30. data/lib/cuprum/rails/commands/find_many.rb +8 -1
  31. data/lib/cuprum/rails/commands/find_matching.rb +1 -1
  32. data/lib/cuprum/rails/commands/find_one.rb +8 -0
  33. data/lib/cuprum/rails/commands/insert_one.rb +17 -6
  34. data/lib/cuprum/rails/commands/update_one.rb +16 -5
  35. data/lib/cuprum/rails/constraints/parameters_contract.rb +14 -0
  36. data/lib/cuprum/rails/constraints.rb +10 -0
  37. data/lib/cuprum/rails/controller.rb +12 -2
  38. data/lib/cuprum/rails/controllers/action.rb +100 -0
  39. data/lib/cuprum/rails/controllers/class_methods/actions.rb +33 -7
  40. data/lib/cuprum/rails/controllers/class_methods/configuration.rb +36 -0
  41. data/lib/cuprum/rails/controllers/class_methods/middleware.rb +88 -0
  42. data/lib/cuprum/rails/controllers/class_methods/validations.rb +2 -2
  43. data/lib/cuprum/rails/controllers/configuration.rb +41 -1
  44. data/lib/cuprum/rails/controllers/middleware.rb +59 -0
  45. data/lib/cuprum/rails/controllers.rb +2 -0
  46. data/lib/cuprum/rails/errors/invalid_parameters.rb +55 -0
  47. data/lib/cuprum/rails/errors/invalid_statement.rb +11 -0
  48. data/lib/cuprum/rails/errors/missing_parameter.rb +42 -0
  49. data/lib/cuprum/rails/errors/resource_error.rb +46 -0
  50. data/lib/cuprum/rails/errors.rb +6 -1
  51. data/lib/cuprum/rails/map_errors.rb +29 -1
  52. data/lib/cuprum/rails/query.rb +1 -1
  53. data/lib/cuprum/rails/repository.rb +12 -25
  54. data/lib/cuprum/rails/request.rb +149 -60
  55. data/lib/cuprum/rails/resource.rb +119 -85
  56. data/lib/cuprum/rails/responders/base_responder.rb +78 -0
  57. data/lib/cuprum/rails/responders/html/plural_resource.rb +9 -39
  58. data/lib/cuprum/rails/responders/html/rendering.rb +81 -0
  59. data/lib/cuprum/rails/responders/html/resource.rb +107 -0
  60. data/lib/cuprum/rails/responders/html/singular_resource.rb +9 -38
  61. data/lib/cuprum/rails/responders/html.rb +2 -0
  62. data/lib/cuprum/rails/responders/html_responder.rb +8 -52
  63. data/lib/cuprum/rails/responders/json/resource.rb +3 -3
  64. data/lib/cuprum/rails/responders/json_responder.rb +31 -16
  65. data/lib/cuprum/rails/responders/matching.rb +29 -27
  66. data/lib/cuprum/rails/responders/serialization.rb +11 -9
  67. data/lib/cuprum/rails/responders.rb +1 -0
  68. data/lib/cuprum/rails/responses/head_response.rb +24 -0
  69. data/lib/cuprum/rails/responses/html/redirect_back_response.rb +55 -0
  70. data/lib/cuprum/rails/responses/html/redirect_response.rb +19 -4
  71. data/lib/cuprum/rails/responses/html/render_response.rb +17 -5
  72. data/lib/cuprum/rails/responses/html.rb +6 -2
  73. data/lib/cuprum/rails/responses.rb +1 -0
  74. data/lib/cuprum/rails/result.rb +36 -0
  75. data/lib/cuprum/rails/routes.rb +36 -23
  76. data/lib/cuprum/rails/rspec/contract_helpers.rb +57 -0
  77. data/lib/cuprum/rails/rspec/contracts/action_contracts.rb +754 -0
  78. data/lib/cuprum/rails/rspec/contracts/actions/create_contracts.rb +289 -0
  79. data/lib/cuprum/rails/rspec/contracts/actions/destroy_contracts.rb +164 -0
  80. data/lib/cuprum/rails/rspec/contracts/actions/edit_contracts.rb +73 -0
  81. data/lib/cuprum/rails/rspec/contracts/actions/index_contracts.rb +108 -0
  82. data/lib/cuprum/rails/rspec/contracts/actions/new_contracts.rb +111 -0
  83. data/lib/cuprum/rails/rspec/contracts/actions/show_contracts.rb +72 -0
  84. data/lib/cuprum/rails/rspec/contracts/actions/update_contracts.rb +263 -0
  85. data/lib/cuprum/rails/rspec/contracts/actions.rb +8 -0
  86. data/lib/cuprum/rails/rspec/contracts/command_contracts.rb +479 -0
  87. data/lib/cuprum/rails/rspec/contracts/responder_contracts.rb +232 -0
  88. data/lib/cuprum/rails/rspec/contracts/routes_contracts.rb +363 -0
  89. data/lib/cuprum/rails/rspec/contracts/serializers_contracts.rb +70 -0
  90. data/lib/cuprum/rails/rspec/contracts.rb +8 -0
  91. data/lib/cuprum/rails/rspec/matchers/be_a_result_matcher.rb +64 -0
  92. data/lib/cuprum/rails/rspec/matchers.rb +41 -0
  93. data/lib/cuprum/rails/serializers/base_serializer.rb +60 -0
  94. data/lib/cuprum/rails/serializers/context.rb +84 -0
  95. data/lib/cuprum/rails/serializers/json/active_record_serializer.rb +2 -2
  96. data/lib/cuprum/rails/serializers/json/array_serializer.rb +9 -8
  97. data/lib/cuprum/rails/serializers/json/attributes_serializer.rb +95 -172
  98. data/lib/cuprum/rails/serializers/json/error_serializer.rb +2 -2
  99. data/lib/cuprum/rails/serializers/json/hash_serializer.rb +9 -8
  100. data/lib/cuprum/rails/serializers/json/identity_serializer.rb +3 -3
  101. data/lib/cuprum/rails/serializers/json/properties_serializer.rb +252 -0
  102. data/lib/cuprum/rails/serializers/json.rb +2 -1
  103. data/lib/cuprum/rails/serializers.rb +3 -1
  104. data/lib/cuprum/rails/version.rb +1 -1
  105. data/lib/cuprum/rails.rb +19 -16
  106. metadata +73 -131
  107. data/lib/cuprum/rails/controller_action.rb +0 -121
  108. data/lib/cuprum/rails/errors/missing_parameters.rb +0 -33
  109. data/lib/cuprum/rails/errors/missing_primary_key.rb +0 -46
  110. data/lib/cuprum/rails/errors/undefined_permitted_attributes.rb +0 -34
  111. data/lib/cuprum/rails/rspec/command_contract.rb +0 -460
  112. data/lib/cuprum/rails/rspec/define_route_contract.rb +0 -84
  113. data/lib/cuprum/rails/serializers/json/serializer.rb +0 -66
@@ -1,11 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'cuprum/rails/serializers/base_serializer'
3
4
  require 'cuprum/rails/serializers/json'
4
- require 'cuprum/rails/serializers/json/serializer'
5
5
 
6
6
  module Cuprum::Rails::Serializers::Json
7
7
  # Converts ActiveRecord record to JSON using the #as_json method.
8
- class ActiveRecordSerializer < Cuprum::Rails::Serializers::Json::Serializer
8
+ class ActiveRecordSerializer < Cuprum::Rails::Serializers::BaseSerializer
9
9
  # Converts the ActiveRecord record to JSON.
10
10
  #
11
11
  # Calls and returns the #as_json method of the record.
@@ -1,11 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'cuprum/rails/serializers/base_serializer'
3
4
  require 'cuprum/rails/serializers/json'
4
- require 'cuprum/rails/serializers/json/serializer'
5
5
 
6
6
  module Cuprum::Rails::Serializers::Json
7
7
  # Converts Array data structures to JSON based on configured serializers.
8
- class ArraySerializer < Cuprum::Rails::Serializers::Json::Serializer
8
+ class ArraySerializer < Cuprum::Rails::Serializers::BaseSerializer
9
9
  # Converts the array to JSON using the given serializers.
10
10
  #
11
11
  # First, #call finds the best serializer from the :serializers Hash for each
@@ -15,26 +15,27 @@ module Cuprum::Rails::Serializers::Json
15
15
  # combined into a new Array and returned.
16
16
  #
17
17
  # @param array [Array] The array to convert to JSON.
18
- # @param serializers [Hash<Class, #call>] The serializers for different
19
- # object types.
18
+ # @param context [Cuprum::Rails::Serializers::Context] The serialization
19
+ # context, which includes the configured serializers for attributes or
20
+ # collection items.
20
21
  #
21
22
  # @return [Array] a JSON-compatible representation of the array.
22
23
  #
23
24
  # @raise UndefinedSerializerError if there is no matching serializer for
24
25
  # any of the items in the array.
25
- def call(array, serializers:)
26
+ def call(array, context:)
26
27
  raise ArgumentError, 'object must be an Array' unless array.is_a?(Array)
27
28
 
28
- array.map { |item| super(item, serializers: serializers) }
29
+ array.map { |item| super(item, context: context) }
29
30
  end
30
31
 
31
32
  private
32
33
 
33
- def handle_recursion!(_object)
34
+ def allow_recursion?
34
35
  # Call serializes the items, not the array. Because the context changes,
35
36
  # we don't need to check for recursion (unless the Array contains itself,
36
37
  # in which case here there be dragons).
37
- yield
38
+ true
38
39
  end
39
40
  end
40
41
  end
@@ -1,217 +1,140 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'cuprum/rails/serializers/base_serializer'
3
4
  require 'cuprum/rails/serializers/json'
4
- require 'cuprum/rails/serializers/json/serializer'
5
5
 
6
6
  module Cuprum::Rails::Serializers::Json
7
- # Converts the object to JSON by serializing the specified attributes.
7
+ # Generates a JSON representation of the object's attributes.
8
8
  #
9
- # When called, the serializer will convert each of the given attributes to
10
- # JSON using the given serializers.
9
+ # Defined properties are inherited from the parent serializer. This allows
10
+ # you to extend existing serializers with additional functionality.
11
11
  #
12
- # Defined attributes are inherited from the parent serializer. This allows you
13
- # to extend existing serializers with additional functionality.
14
- #
15
- # @example Defining A Base Serializer
16
- # class RecordSerializer < Cuprum::Rails::Serializers::Json::AttributesSerializer # rubocop:disable Layout/LineLength
17
- # attribute :id
12
+ # @example Serializing an attribute.
13
+ # User = Struct.new(:first_name, :last_name, :salary, :department)
14
+ # class FirstNameSerializer < Cuprum::Rails::Serializers::Json::AttributesSerializer
15
+ # attribute :first_name
18
16
  # end
19
17
  #
20
- # book = Book.new(
21
- # id: 10,
22
- # title: 'Gideon the Ninth',
23
- # author: 'Tamsyn Muir',
24
- # published_at: '2019-09-10'
25
- # )
26
- # serializers = Cuprum::Rails::Serializers::Json.default_serializers
18
+ # user = User.new('Alan', 'Bradley')
19
+ # serializer = FirstNameSerializer.new
20
+ # serializer.call(user, context: context)
21
+ # #=> {
22
+ # 'first_name' => 'Alan'
23
+ # }
27
24
  #
28
- # RecordSerializer.new.call(book, serializers: serializers)
29
- # #=> { id: 10 }
25
+ # @example Using a custom serializer.
26
+ # User = Struct.new(:first_name, :last_name, :salary, :department)
27
+ # class SalarySerializer < Cuprum::Rails::Serializers::Json::AttributesSerializer
28
+ # attribute :salary, serializer: BigDecimalSerializer
29
+ # end
30
+ #
31
+ # user = User.new('Alan', 'Bradley', BigDecimal('100000'))
32
+ # serializer = SalarySerializer.new
33
+ # serializer.call(user, context: context)
34
+ # #=> {
35
+ # 'salary' => '0.1e6'
36
+ # }
30
37
  #
31
- # @example Defining A Record Serializer
32
- # class BookSerializer < RecordSerializer
33
- # attribute :title, Cuprum::Rails::Serializers::Json::IdentitySerializer.instance # rubocop:disable Layout/LineLength
34
- # attribute :author do |author|
35
- # "by: #{author}"
36
- # end
38
+ # @example Using a custom mapping.
39
+ # User = Struct.new(:first_name, :last_name, :hire_date)
40
+ # class HireDateSerializer < Cuprum::Rails::Serializers::Json::AttributesSerializer
41
+ # attribute(:hire_date) { |value| value&.iso8601 }
37
42
  # end
38
43
  #
39
- # BookSerializer.new.call(book, serializers: serializers)
44
+ # user = User.new('Alan', 'Bradley', Date.new(1977, 5, 25))
45
+ # serializer = HireDateSerializer.new
46
+ # serializer.call(user, context: context)
40
47
  # #=> {
41
- # id: 10,
42
- # title: 'Gideon the Ninth',
43
- # author: 'by: Tamsyn Muir'
44
- # }
48
+ # 'hire_date' => '1977-05-25'
49
+ # }
45
50
  #
46
- # @example Defining a Serializer Subclass
47
- # class PublishedBookSerializer < BookSerializer
48
- # attribute :published_at
51
+ # @example Serializing multiple attributes.
52
+ # User = Struct.new(:first_name, :last_name, :hire_date)
53
+ # class UserSerializer < Cuprum::Rails::Serializers::Json::AttributesSerializer
54
+ # attributes \
55
+ # :first_name,
56
+ # :last_name,
57
+ # hire_date: :iso8601
49
58
  # end
50
59
  #
51
- # PublishedBookSerializer.new.call(book, serializers: serializers)
60
+ # user = User.new('Alan', 'Bradley', Date.new(1977, 5, 25))
61
+ # serializer = HireDateSerializer.new
62
+ # serializer.call(user, context: context)
52
63
  # #=> {
53
- # id: 10,
54
- # title: 'Gideon the Ninth',
55
- # author: 'by: Tamsyn Muir',
56
- # published_at: '2019-09-10'
57
- # }
58
- class AttributesSerializer < Cuprum::Rails::Serializers::Json::Serializer
59
- # Error class used when a serializer calls itself.
60
- class AbstractSerializerError < StandardError; end
64
+ # 'first_name' => 'Alan',
65
+ # 'last_name' => 'Bradley',
66
+ # 'hire_date' => '1977-05-25'
67
+ # }
68
+ #
69
+ # @see Cuprum::Rails::Serializers::Json::PropertiesSerializer
70
+ class AttributesSerializer < Cuprum::Rails::Serializers::Json::PropertiesSerializer # rubocop:disable Layout/LineLength
71
+ @abstract_class = true
61
72
 
62
73
  class << self
63
74
  # Registers the attribute to be serialized.
64
75
  #
65
- # This class method will raise an AbstractSerializerError if called on
66
- # AttributesSerializer directly. Instead, create a subclass and define
67
- # attributes on that subclass.
68
- #
69
- # @return [Class] the serializer class.
70
- #
71
- # @raise [AbstractSerializerError] if called on AttributesSerializer.
72
- #
73
- # @see AttributesSerializer#call.
74
- #
75
- # @overload attribute(attr_name)
76
- # Registers the attribute for serialization. When the serializer is
77
- # called, the value of the attribute will be converted to JSON using the
78
- # serializers passed to #call.
76
+ # @param name [String, Symbol] the name of the attribute to serialize.
77
+ # This will determine the hash key of the serialized value, as well as
78
+ # the base value to be serialized.
79
+ # @param serializer [#call] the serializer used to serialize the value. If
80
+ # no serializer is given, the default serializer (if any) will be used.
79
81
  #
80
- # @param attr_name [String, Symbol] The name of the attribute to
81
- # serialize.
82
+ # @yield If a block is given, the block is used to transform the attribute
83
+ # value prior to serialization.
84
+ # @yieldparam value [Object] the attribute value.
85
+ # @yieldreturn [Object] the transformed value.
82
86
  #
83
- # @overload attribute(attr_name, serializer)
84
- # Registers the attribute for serialization. When the serializer is
85
- # called, the given serializer will be called with the value of the
86
- # attribute and the configured serializers.
87
- #
88
- # @param attr_name [String, Symbol] The name of the attribute to
89
- # serialize.
90
- # @param serializer [Cuprum::Rails::Serializers::Json::Serializer] the
91
- # serializer to use when converting the attribute to JSON.
92
- #
93
- # @overload attribute(attr_name, &block)
94
- # Registers the attribute for serialization. When the serializer is
95
- # called, the block will be called with the value of the attribute and
96
- # the configured serializers.
97
- #
98
- # @param attr_name [String, Symbol] The name of the attribute to
99
- # serialize.
87
+ # @raise AbstractSerializerError when attempting to define a serialized
88
+ # property on an abstract class.
89
+ def attribute(name, serializer: nil, &block)
90
+ property(
91
+ name,
92
+ scope: name,
93
+ serializer: serializer,
94
+ &block
95
+ )
96
+ end
97
+
98
+ # Registers the attributes to be serialized.
100
99
  #
101
- # @yield The value of the attribute and the configured serializers.
100
+ # @param attribute_names [Array<String, Symbol>] the names of the
101
+ # attributes to serialize.
102
+ # @param attribute_mappings [Hash{String, Symbol => #to_proc}] the names
103
+ # and mappings of additional attributes to serialize.
102
104
  #
103
- # @yieldreturn the attribute value converted to JSON.
104
- def attribute(attr_name, serializer = nil, &block)
105
- validate_subclass!
106
- validate_attribute_name!(attr_name)
107
- validate_serializer!(serializer)
105
+ # @raise AbstractSerializerError when attempting to define a serialized
106
+ # property on an abstract class.
107
+ def attributes(*attribute_names, **attribute_mappings)
108
+ require_concrete_class!
108
109
 
109
- own_attributes[attr_name.to_s] = serializer || block
110
+ validate_property_names!(*attribute_names, *attribute_mappings.keys)
111
+ validate_property_mappings!(*attribute_mappings.values)
110
112
 
111
- self
112
- end
113
+ attribute_names.each { |name| attribute(name) }
113
114
 
114
- # @return [Hash<String, Object>] the defined attributes and respective
115
- # serializers.
116
- def attributes(*_, **_)
117
- all_attributes
118
- end
119
-
120
- protected
121
-
122
- def own_attributes
123
- @own_attributes ||= {}
115
+ attribute_mappings.each { |name, mapping| attribute(name, &mapping) }
124
116
  end
125
117
 
126
118
  private
127
119
 
128
- def all_attributes
129
- ancestors
130
- .select do |ancestor|
131
- ancestor < Cuprum::Rails::Serializers::Json::AttributesSerializer
132
- end # rubocop:disable Style/MultilineBlockChain
133
- .reverse_each
134
- .reduce({}) { |hsh, ancestor| hsh.merge(ancestor.own_attributes) }
135
- end
136
-
137
- def validate_attribute_name!(attr_name)
138
- raise ArgumentError, "attribute name can't be blank" if attr_name.nil?
139
-
140
- unless attr_name.is_a?(String) || attr_name.is_a?(Symbol)
141
- raise ArgumentError, 'attribute name must be a string or symbol'
142
- end
143
-
144
- return unless attr_name.empty?
145
-
146
- raise ArgumentError, "attribute name can't be blank"
147
- end
148
-
149
- def validate_serializer!(serializer)
150
- return if serializer.nil? || serializer.respond_to?(:call)
120
+ def validate_property_mappings!(*mappings)
121
+ mappings.each do |mapping|
122
+ next if mapping.respond_to?(:to_proc)
151
123
 
152
- raise ArgumentError, 'serializer must respond to #call'
153
- end
154
-
155
- def validate_subclass!
156
- unless self == Cuprum::Rails::Serializers::Json::AttributesSerializer
157
- return
124
+ raise ArgumentError, 'property mapping must respond to #to_proc'
158
125
  end
159
-
160
- raise AbstractSerializerError,
161
- 'AttributesSerializer is an abstract class - create a subclass to' \
162
- ' define attributes'
163
126
  end
164
- end
165
127
 
166
- # Converts the defined attributes to JSON.
167
- #
168
- # @param object [Object] The object to convert to JSON.
169
- # @param serializers [Hash<Class, #call>] The serializers for different
170
- # object types.
171
- #
172
- # @return [Hash<String, Object] a JSON-compatible representation of the
173
- # object's attributes.
174
- def call(object, serializers:)
175
- self.class.attributes.each.with_object({}) \
176
- do |(attr_name, serializer), hsh|
177
- attr_value = object.send(attr_name)
178
- hsh[attr_name] =
179
- serialize_attribute(
180
- attr_value: attr_value,
181
- serializer: serializer,
182
- serializers: serializers
183
- ) { super(attr_value, serializers: serializers) }
128
+ def validate_property_names!(*names)
129
+ names.each { |name| validate_property_name!(name) }
184
130
  end
185
131
  end
186
132
 
187
133
  private
188
134
 
189
- def apply_block(attr_value, block:, serializers:)
190
- args = block_argument?(block) ? [attr_value] : []
191
- kwargs = block_keyword?(block) ? { serializers: serializers } : {}
192
-
193
- kwargs.empty? ? block.call(*args) : block.call(*args, **kwargs)
194
- end
195
-
196
- def block_argument?(block)
197
- block.parameters.any? { |type, _| type == :req || type == :rest } # rubocop:disable Style/MultipleComparison
198
- end
199
-
200
- def block_keyword?(block)
201
- block.parameters.any? do |type, name|
202
- (type == :keyreq && name == :serializers) || type == :keyrest
203
- end
204
- end
205
-
206
- def serialize_attribute(attr_value:, serializer:, serializers:)
207
- case serializer
208
- when Cuprum::Rails::Serializers::Json::Serializer
209
- serializer.call(attr_value)
210
- when Proc
211
- apply_block(attr_value, block: serializer, serializers: serializers)
212
- else
213
- yield
214
- end
135
+ def allow_recursion?
136
+ # Call serializes the attributes, not the object itself.
137
+ true
215
138
  end
216
139
  end
217
140
  end
@@ -1,11 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'cuprum/rails/serializers/base_serializer'
3
4
  require 'cuprum/rails/serializers/json'
4
- require 'cuprum/rails/serializers/json/serializer'
5
5
 
6
6
  module Cuprum::Rails::Serializers::Json
7
7
  # Converts a Cuprum::Error to JSON using the #as_json method.
8
- class ErrorSerializer < Cuprum::Rails::Serializers::Json::Serializer
8
+ class ErrorSerializer < Cuprum::Rails::Serializers::BaseSerializer
9
9
  # Converts the Cuprum error to JSON.
10
10
  #
11
11
  # Calls and returns the #as_json method of the error.
@@ -1,11 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'cuprum/rails/serializers/base_serializer'
3
4
  require 'cuprum/rails/serializers/json'
4
- require 'cuprum/rails/serializers/json/serializer'
5
5
 
6
6
  module Cuprum::Rails::Serializers::Json
7
7
  # Converts Hash data structures to JSON based on configured serializers.
8
- class HashSerializer < Cuprum::Rails::Serializers::Json::Serializer
8
+ class HashSerializer < Cuprum::Rails::Serializers::BaseSerializer
9
9
  # Converts the hash to JSON using the given serializers.
10
10
  #
11
11
  # First, #call finds the best serializer from the :serializers Hash for each
@@ -15,30 +15,31 @@ module Cuprum::Rails::Serializers::Json
15
15
  # are combined into a new Hash and returned.
16
16
  #
17
17
  # @param hash [Hash<String, Object>] The hash to convert to JSON.
18
- # @param serializers [Hash<Class, #call>] The serializers for different
19
- # object types.
18
+ # @param context [Cuprum::Rails::Serializers::Context] The serialization
19
+ # context, which includes the configured serializers for attributes or
20
+ # collection items.
20
21
  #
21
22
  # @return [Hash] a JSON-compatible representation of the hash.
22
23
  #
23
24
  # @raise UndefinedSerializerError if there is no matching serializer for
24
25
  # any of the values in the hash.
25
- def call(hash, serializers:)
26
+ def call(hash, context:)
26
27
  unless hash.is_a?(Hash) && hash.keys.all? { |key| key.is_a?(String) }
27
28
  raise ArgumentError, 'object must be a Hash with String keys'
28
29
  end
29
30
 
30
31
  hash.each.with_object({}) do |(key, value), mapped|
31
- mapped[key] = super(value, serializers: serializers)
32
+ mapped[key] = super(value, context: context)
32
33
  end
33
34
  end
34
35
 
35
36
  private
36
37
 
37
- def handle_recursion!(_object)
38
+ def allow_recursion?
38
39
  # Call serializes the values, not the hash. Because the context changes,
39
40
  # we don't need to check for recursion (unless the Hash contains itself,
40
41
  # in which case here there be dragons).
41
- yield
42
+ true
42
43
  end
43
44
  end
44
45
  end
@@ -1,11 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'cuprum/rails/serializers/base_serializer'
3
4
  require 'cuprum/rails/serializers/json'
4
- require 'cuprum/rails/serializers/json/serializer'
5
5
 
6
6
  module Cuprum::Rails::Serializers::Json
7
7
  # Serializer that returns a value object as itself.
8
- class IdentitySerializer < Cuprum::Rails::Serializers::Json::Serializer
8
+ class IdentitySerializer < Cuprum::Rails::Serializers::BaseSerializer
9
9
  # Returns the object.
10
10
  #
11
11
  # This serializer should only be used with value objects: nil, true, false,
@@ -13,7 +13,7 @@ module Cuprum::Rails::Serializers::Json
13
13
  #
14
14
  # @param object [Object] The object to convert to JSON.
15
15
  #
16
- # @return [Object] a JSON-compatible Hash representation of the object.
16
+ # @return [Object] a JSON representation of the object.
17
17
  def call(object, **_)
18
18
  object
19
19
  end