cuprum-rails 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +98 -0
- data/CODE_OF_CONDUCT.md +132 -0
- data/DEVELOPMENT.md +28 -0
- data/LICENSE +22 -0
- data/README.md +1045 -0
- data/lib/cuprum/rails/action.rb +45 -0
- data/lib/cuprum/rails/actions/create.rb +49 -0
- data/lib/cuprum/rails/actions/destroy.rb +22 -0
- data/lib/cuprum/rails/actions/edit.rb +22 -0
- data/lib/cuprum/rails/actions/index.rb +55 -0
- data/lib/cuprum/rails/actions/new.rb +19 -0
- data/lib/cuprum/rails/actions/resource_action.rb +75 -0
- data/lib/cuprum/rails/actions/show.rb +22 -0
- data/lib/cuprum/rails/actions/update.rb +59 -0
- data/lib/cuprum/rails/actions.rb +16 -0
- data/lib/cuprum/rails/collection.rb +115 -0
- data/lib/cuprum/rails/command.rb +137 -0
- data/lib/cuprum/rails/commands/assign_one.rb +66 -0
- data/lib/cuprum/rails/commands/build_one.rb +55 -0
- data/lib/cuprum/rails/commands/destroy_one.rb +43 -0
- data/lib/cuprum/rails/commands/find_many.rb +60 -0
- data/lib/cuprum/rails/commands/find_matching.rb +121 -0
- data/lib/cuprum/rails/commands/find_one.rb +50 -0
- data/lib/cuprum/rails/commands/insert_one.rb +41 -0
- data/lib/cuprum/rails/commands/update_one.rb +49 -0
- data/lib/cuprum/rails/commands/validate_one.rb +68 -0
- data/lib/cuprum/rails/commands.rb +18 -0
- data/lib/cuprum/rails/controller.rb +50 -0
- data/lib/cuprum/rails/controller_action.rb +121 -0
- data/lib/cuprum/rails/controllers/class_methods/actions.rb +57 -0
- data/lib/cuprum/rails/controllers/class_methods/configuration.rb +64 -0
- data/lib/cuprum/rails/controllers/class_methods/validations.rb +30 -0
- data/lib/cuprum/rails/controllers/class_methods.rb +15 -0
- data/lib/cuprum/rails/controllers/configuration.rb +53 -0
- data/lib/cuprum/rails/controllers.rb +10 -0
- data/lib/cuprum/rails/errors/missing_parameters.rb +33 -0
- data/lib/cuprum/rails/errors/missing_primary_key.rb +46 -0
- data/lib/cuprum/rails/errors/undefined_permitted_attributes.rb +34 -0
- data/lib/cuprum/rails/errors.rb +8 -0
- data/lib/cuprum/rails/map_errors.rb +44 -0
- data/lib/cuprum/rails/query.rb +77 -0
- data/lib/cuprum/rails/query_builder.rb +78 -0
- data/lib/cuprum/rails/repository.rb +44 -0
- data/lib/cuprum/rails/request.rb +105 -0
- data/lib/cuprum/rails/resource.rb +145 -0
- data/lib/cuprum/rails/responders/actions.rb +73 -0
- data/lib/cuprum/rails/responders/html/plural_resource.rb +62 -0
- data/lib/cuprum/rails/responders/html/singular_resource.rb +59 -0
- data/lib/cuprum/rails/responders/html.rb +11 -0
- data/lib/cuprum/rails/responders/html_responder.rb +129 -0
- data/lib/cuprum/rails/responders/json/resource.rb +60 -0
- data/lib/cuprum/rails/responders/json.rb +10 -0
- data/lib/cuprum/rails/responders/json_responder.rb +122 -0
- data/lib/cuprum/rails/responders/matching.rb +145 -0
- data/lib/cuprum/rails/responders/serialization.rb +36 -0
- data/lib/cuprum/rails/responders.rb +15 -0
- data/lib/cuprum/rails/responses/html/redirect_response.rb +29 -0
- data/lib/cuprum/rails/responses/html/render_response.rb +52 -0
- data/lib/cuprum/rails/responses/html.rb +11 -0
- data/lib/cuprum/rails/responses/json_response.rb +29 -0
- data/lib/cuprum/rails/responses.rb +11 -0
- data/lib/cuprum/rails/routes.rb +166 -0
- data/lib/cuprum/rails/routing/plural_routes.rb +26 -0
- data/lib/cuprum/rails/routing/singular_routes.rb +24 -0
- data/lib/cuprum/rails/routing.rb +11 -0
- data/lib/cuprum/rails/rspec/command_contract.rb +460 -0
- data/lib/cuprum/rails/rspec/define_route_contract.rb +84 -0
- data/lib/cuprum/rails/rspec.rb +8 -0
- data/lib/cuprum/rails/serializers/json/active_record_serializer.rb +24 -0
- data/lib/cuprum/rails/serializers/json/array_serializer.rb +40 -0
- data/lib/cuprum/rails/serializers/json/attributes_serializer.rb +217 -0
- data/lib/cuprum/rails/serializers/json/error_serializer.rb +24 -0
- data/lib/cuprum/rails/serializers/json/hash_serializer.rb +44 -0
- data/lib/cuprum/rails/serializers/json/identity_serializer.rb +21 -0
- data/lib/cuprum/rails/serializers/json/serializer.rb +66 -0
- data/lib/cuprum/rails/serializers/json.rb +40 -0
- data/lib/cuprum/rails/serializers.rb +10 -0
- data/lib/cuprum/rails/version.rb +59 -0
- data/lib/cuprum/rails.rb +31 -0
- metadata +286 -0
@@ -0,0 +1,217 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'cuprum/rails/serializers/json'
|
4
|
+
require 'cuprum/rails/serializers/json/serializer'
|
5
|
+
|
6
|
+
module Cuprum::Rails::Serializers::Json
|
7
|
+
# Converts the object to JSON by serializing the specified attributes.
|
8
|
+
#
|
9
|
+
# When called, the serializer will convert each of the given attributes to
|
10
|
+
# JSON using the given serializers.
|
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
|
18
|
+
# end
|
19
|
+
#
|
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
|
27
|
+
#
|
28
|
+
# RecordSerializer.new.call(book, serializers: serializers)
|
29
|
+
# #=> { id: 10 }
|
30
|
+
#
|
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
|
37
|
+
# end
|
38
|
+
#
|
39
|
+
# BookSerializer.new.call(book, serializers: serializers)
|
40
|
+
# #=> {
|
41
|
+
# id: 10,
|
42
|
+
# title: 'Gideon the Ninth',
|
43
|
+
# author: 'by: Tamsyn Muir'
|
44
|
+
# }
|
45
|
+
#
|
46
|
+
# @example Defining a Serializer Subclass
|
47
|
+
# class PublishedBookSerializer < BookSerializer
|
48
|
+
# attribute :published_at
|
49
|
+
# end
|
50
|
+
#
|
51
|
+
# PublishedBookSerializer.new.call(book, serializers: serializers)
|
52
|
+
# #=> {
|
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
|
61
|
+
|
62
|
+
class << self
|
63
|
+
# Registers the attribute to be serialized.
|
64
|
+
#
|
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.
|
79
|
+
#
|
80
|
+
# @param attr_name [String, Symbol] The name of the attribute to
|
81
|
+
# serialize.
|
82
|
+
#
|
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.
|
100
|
+
#
|
101
|
+
# @yield The value of the attribute and the configured serializers.
|
102
|
+
#
|
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)
|
108
|
+
|
109
|
+
own_attributes[attr_name.to_s] = serializer || block
|
110
|
+
|
111
|
+
self
|
112
|
+
end
|
113
|
+
|
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 ||= {}
|
124
|
+
end
|
125
|
+
|
126
|
+
private
|
127
|
+
|
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)
|
151
|
+
|
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
|
158
|
+
end
|
159
|
+
|
160
|
+
raise AbstractSerializerError,
|
161
|
+
'AttributesSerializer is an abstract class - create a subclass to' \
|
162
|
+
' define attributes'
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
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) }
|
184
|
+
end
|
185
|
+
end
|
186
|
+
|
187
|
+
private
|
188
|
+
|
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
|
215
|
+
end
|
216
|
+
end
|
217
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'cuprum/rails/serializers/json'
|
4
|
+
require 'cuprum/rails/serializers/json/serializer'
|
5
|
+
|
6
|
+
module Cuprum::Rails::Serializers::Json
|
7
|
+
# Converts a Cuprum::Error to JSON using the #as_json method.
|
8
|
+
class ErrorSerializer < Cuprum::Rails::Serializers::Json::Serializer
|
9
|
+
# Converts the Cuprum error to JSON.
|
10
|
+
#
|
11
|
+
# Calls and returns the #as_json method of the error.
|
12
|
+
#
|
13
|
+
# @param error [Cuprum::Error] The error to convert to JSON.
|
14
|
+
#
|
15
|
+
# @return [Hash] a JSON-compatible representation of the error.
|
16
|
+
def call(error, **_)
|
17
|
+
unless error.is_a?(Cuprum::Error)
|
18
|
+
raise ArgumentError, 'object must be a Cuprum::Error'
|
19
|
+
end
|
20
|
+
|
21
|
+
error.as_json
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'cuprum/rails/serializers/json'
|
4
|
+
require 'cuprum/rails/serializers/json/serializer'
|
5
|
+
|
6
|
+
module Cuprum::Rails::Serializers::Json
|
7
|
+
# Converts Hash data structures to JSON based on configured serializers.
|
8
|
+
class HashSerializer < Cuprum::Rails::Serializers::Json::Serializer
|
9
|
+
# Converts the hash to JSON using the given serializers.
|
10
|
+
#
|
11
|
+
# First, #call finds the best serializer from the :serializers Hash for each
|
12
|
+
# value in the Hash. This is done by walking up the object class's ancestors
|
13
|
+
# to find the closest ancestor which is a key in the :serializers Hash.
|
14
|
+
# The corresponding value is then called with the object, and the results
|
15
|
+
# are combined into a new Hash and returned.
|
16
|
+
#
|
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.
|
20
|
+
#
|
21
|
+
# @return [Hash] a JSON-compatible representation of the hash.
|
22
|
+
#
|
23
|
+
# @raise UndefinedSerializerError if there is no matching serializer for
|
24
|
+
# any of the values in the hash.
|
25
|
+
def call(hash, serializers:)
|
26
|
+
unless hash.is_a?(Hash) && hash.keys.all? { |key| key.is_a?(String) }
|
27
|
+
raise ArgumentError, 'object must be a Hash with String keys'
|
28
|
+
end
|
29
|
+
|
30
|
+
hash.each.with_object({}) do |(key, value), mapped|
|
31
|
+
mapped[key] = super(value, serializers: serializers)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def handle_recursion!(_object)
|
38
|
+
# Call serializes the values, not the hash. Because the context changes,
|
39
|
+
# we don't need to check for recursion (unless the Hash contains itself,
|
40
|
+
# in which case here there be dragons).
|
41
|
+
yield
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'cuprum/rails/serializers/json'
|
4
|
+
require 'cuprum/rails/serializers/json/serializer'
|
5
|
+
|
6
|
+
module Cuprum::Rails::Serializers::Json
|
7
|
+
# Serializer that returns a value object as itself.
|
8
|
+
class IdentitySerializer < Cuprum::Rails::Serializers::Json::Serializer
|
9
|
+
# Returns the object.
|
10
|
+
#
|
11
|
+
# This serializer should only be used with value objects: nil, true, false,
|
12
|
+
# Integers, Floats, and Strings.
|
13
|
+
#
|
14
|
+
# @param object [Object] The object to convert to JSON.
|
15
|
+
#
|
16
|
+
# @return [Object] a JSON-compatible Hash representation of the object.
|
17
|
+
def call(object, **_)
|
18
|
+
object
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'cuprum/rails/serializers/json'
|
4
|
+
|
5
|
+
module Cuprum::Rails::Serializers::Json
|
6
|
+
# Converts objects or data structures to JSON based on configured serializers.
|
7
|
+
class Serializer
|
8
|
+
# Error class used when a serializer calls itself.
|
9
|
+
class RecursiveSerializerError < StandardError; end
|
10
|
+
|
11
|
+
# Error class used when there is no matching serializer for the object.
|
12
|
+
class UndefinedSerializerError < StandardError; end
|
13
|
+
|
14
|
+
# @return [Cuprum::Rails::Serializers::Json::Serializer] a cached instance
|
15
|
+
# of the serializer.
|
16
|
+
def self.instance
|
17
|
+
@instance ||= new
|
18
|
+
end
|
19
|
+
|
20
|
+
# Converts the object to JSON using the given serializers.
|
21
|
+
#
|
22
|
+
# First, #call finds the best serializer from the :serializers Hash. This is
|
23
|
+
# done by walking up the object class's ancestors to find the closest
|
24
|
+
# ancestor which is a key in the :serializers Hash. The corresponding value
|
25
|
+
# is then called with the object.
|
26
|
+
#
|
27
|
+
# @param object [Object] The object to convert to JSON.
|
28
|
+
# @param serializers [Hash<Class, #call>] The serializers for different
|
29
|
+
# object types.
|
30
|
+
#
|
31
|
+
# @return [Object] a JSON-compatible representation of the object.
|
32
|
+
#
|
33
|
+
# @raise RecursiveSerializerError if the serializer would create an infinite
|
34
|
+
# loop, e.g. by calling itself.
|
35
|
+
# @raise UndefinedSerializerError if there is no matching serializer for
|
36
|
+
# the object.
|
37
|
+
def call(object, serializers:)
|
38
|
+
serializer = handle_recursion!(object) do
|
39
|
+
serializer_for(object: object, serializers: serializers)
|
40
|
+
end
|
41
|
+
|
42
|
+
serializer.call(object, serializers: serializers)
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
def handle_recursion!(object)
|
48
|
+
serializer = yield
|
49
|
+
|
50
|
+
return serializer unless instance_of?(serializer.class)
|
51
|
+
|
52
|
+
raise RecursiveSerializerError,
|
53
|
+
"invalid serializer for #{object.class.name} - recursive calls to" \
|
54
|
+
" #{self.class.name}#call"
|
55
|
+
end
|
56
|
+
|
57
|
+
def serializer_for(object:, serializers:)
|
58
|
+
object.class.ancestors.each do |ancestor|
|
59
|
+
return serializers[ancestor] if serializers.key?(ancestor)
|
60
|
+
end
|
61
|
+
|
62
|
+
raise UndefinedSerializerError,
|
63
|
+
"no serializer defined for #{object.class.name}"
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'cuprum/rails/serializers'
|
4
|
+
|
5
|
+
module Cuprum::Rails::Serializers
|
6
|
+
# Namespace for JSON serializers, which convert objects to a JSON format.
|
7
|
+
module Json
|
8
|
+
autoload :ActiveRecordSerializer,
|
9
|
+
'cuprum/rails/serializers/json/active_record_serializer'
|
10
|
+
autoload :ArraySerializer,
|
11
|
+
'cuprum/rails/serializers/json/array_serializer'
|
12
|
+
autoload :AttributesSerializer,
|
13
|
+
'cuprum/rails/serializers/json/attributes_serializer'
|
14
|
+
autoload :ErrorSerializer,
|
15
|
+
'cuprum/rails/serializers/json/error_serializer'
|
16
|
+
autoload :HashSerializer,
|
17
|
+
'cuprum/rails/serializers/json/hash_serializer'
|
18
|
+
autoload :IdentitySerializer,
|
19
|
+
'cuprum/rails/serializers/json/identity_serializer'
|
20
|
+
autoload :Serializer, 'cuprum/rails/serializers/json/serializer'
|
21
|
+
|
22
|
+
# Default serializers for handling value objects and data structures.
|
23
|
+
#
|
24
|
+
# @return [Hash<Class, Cuprum::Rails::Serializers::Json::Serializer>] the
|
25
|
+
# default serializers.
|
26
|
+
def self.default_serializers # rubocop:disable Metrics/MethodLength
|
27
|
+
@default_serializers ||= {
|
28
|
+
Array => self::ArraySerializer.instance,
|
29
|
+
Cuprum::Error => self::ErrorSerializer.instance,
|
30
|
+
Hash => self::HashSerializer.instance,
|
31
|
+
FalseClass => self::IdentitySerializer.instance,
|
32
|
+
Float => self::IdentitySerializer.instance,
|
33
|
+
Integer => self::IdentitySerializer.instance,
|
34
|
+
NilClass => self::IdentitySerializer.instance,
|
35
|
+
String => self::IdentitySerializer.instance,
|
36
|
+
TrueClass => self::IdentitySerializer.instance
|
37
|
+
}
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Cuprum
|
4
|
+
module Rails
|
5
|
+
# @api private
|
6
|
+
#
|
7
|
+
# The current version of the gem.
|
8
|
+
#
|
9
|
+
# @see http://semver.org/
|
10
|
+
module Version
|
11
|
+
# Major version.
|
12
|
+
MAJOR = 0
|
13
|
+
# Minor version.
|
14
|
+
MINOR = 1
|
15
|
+
# Patch version.
|
16
|
+
PATCH = 0
|
17
|
+
# Prerelease version.
|
18
|
+
PRERELEASE = nil
|
19
|
+
# Build metadata.
|
20
|
+
BUILD = nil
|
21
|
+
|
22
|
+
class << self
|
23
|
+
# Generates the gem version string from the Version constants.
|
24
|
+
#
|
25
|
+
# Inlined here because dependencies may not be loaded when processing a
|
26
|
+
# gemspec, which results in the user being unable to install the gem for
|
27
|
+
# the first time.
|
28
|
+
#
|
29
|
+
# @see SleepingKingStudios::Tools::SemanticVersion#to_gem_version
|
30
|
+
def to_gem_version
|
31
|
+
str = +"#{MAJOR}.#{MINOR}.#{PATCH}"
|
32
|
+
|
33
|
+
prerelease = value_of(:PRERELEASE)
|
34
|
+
str << ".#{prerelease}" if prerelease
|
35
|
+
|
36
|
+
build = value_of(:BUILD)
|
37
|
+
str << ".#{build}" if build
|
38
|
+
|
39
|
+
str
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
def value_of(constant)
|
45
|
+
return nil unless const_defined?(constant)
|
46
|
+
|
47
|
+
value = const_get(constant)
|
48
|
+
|
49
|
+
return nil if value.respond_to?(:empty?) && value.empty?
|
50
|
+
|
51
|
+
value
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
# @return [String] the current version of the gem.
|
57
|
+
VERSION = Version.to_gem_version
|
58
|
+
end
|
59
|
+
end
|
data/lib/cuprum/rails.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'cuprum'
|
4
|
+
|
5
|
+
# A Ruby implementation of the command pattern.
|
6
|
+
module Cuprum
|
7
|
+
# An integration between Rails and the Cuprum library.
|
8
|
+
module Rails
|
9
|
+
# @return [String] The current version of the gem.
|
10
|
+
def self.version
|
11
|
+
VERSION
|
12
|
+
end
|
13
|
+
|
14
|
+
autoload :Action, 'cuprum/rails/action'
|
15
|
+
autoload :Actions, 'cuprum/rails/actions'
|
16
|
+
autoload :Collection, 'cuprum/rails/collection'
|
17
|
+
autoload :Command, 'cuprum/rails/command'
|
18
|
+
autoload :Commands, 'cuprum/rails/commands'
|
19
|
+
autoload :Controller, 'cuprum/rails/controller'
|
20
|
+
autoload :ControllerAction, 'cuprum/rails/controller_action'
|
21
|
+
autoload :Controllers, 'cuprum/rails/controllers'
|
22
|
+
autoload :Query, 'cuprum/rails/query'
|
23
|
+
autoload :Request, 'cuprum/rails/request'
|
24
|
+
autoload :Responders, 'cuprum/rails/responders'
|
25
|
+
autoload :Responses, 'cuprum/rails/responses'
|
26
|
+
autoload :Resource, 'cuprum/rails/resource'
|
27
|
+
autoload :Routes, 'cuprum/rails/routes'
|
28
|
+
autoload :Routing, 'cuprum/rails/routing'
|
29
|
+
autoload :Serializers, 'cuprum/rails/serializers'
|
30
|
+
end
|
31
|
+
end
|