domainic-attributer 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.
- checksums.yaml +4 -4
- data/.yardopts +11 -0
- data/CHANGELOG.md +32 -1
- data/README.md +42 -355
- data/docs/USAGE.md +723 -0
- data/lib/domainic/attributer/attribute/callback.rb +21 -9
- data/lib/domainic/attributer/attribute/coercer.rb +28 -13
- data/lib/domainic/attributer/attribute/mixin/belongs_to_attribute.rb +16 -13
- data/lib/domainic/attributer/attribute/signature.rb +43 -32
- data/lib/domainic/attributer/attribute/validator.rb +46 -16
- data/lib/domainic/attributer/attribute.rb +28 -18
- data/lib/domainic/attributer/attribute_set.rb +21 -19
- data/lib/domainic/attributer/class_methods.rb +136 -83
- data/lib/domainic/attributer/dsl/attribute_builder/option_parser.rb +64 -22
- data/lib/domainic/attributer/dsl/attribute_builder.rb +515 -26
- data/lib/domainic/attributer/dsl/initializer.rb +23 -18
- data/lib/domainic/attributer/dsl/method_injector.rb +16 -14
- data/lib/domainic/attributer/errors/aggregate_error.rb +36 -0
- data/lib/domainic/attributer/errors/callback_execution_error.rb +30 -0
- data/lib/domainic/attributer/errors/coercion_execution_error.rb +37 -0
- data/lib/domainic/attributer/errors/error.rb +19 -0
- data/lib/domainic/attributer/errors/validation_execution_error.rb +30 -0
- data/lib/domainic/attributer/instance_methods.rb +11 -8
- data/lib/domainic/attributer/undefined.rb +9 -7
- data/lib/domainic/attributer.rb +88 -27
- data/sig/domainic/attributer/attribute/callback.rbs +10 -7
- data/sig/domainic/attributer/attribute/coercer.rbs +14 -11
- data/sig/domainic/attributer/attribute/mixin/belongs_to_attribute.rbs +14 -12
- data/sig/domainic/attributer/attribute/signature.rbs +43 -32
- data/sig/domainic/attributer/attribute/validator.rbs +28 -13
- data/sig/domainic/attributer/attribute.rbs +27 -17
- data/sig/domainic/attributer/attribute_set.rbs +21 -19
- data/sig/domainic/attributer/class_methods.rbs +133 -80
- data/sig/domainic/attributer/dsl/attribute_builder/option_parser.rbs +62 -22
- data/sig/domainic/attributer/dsl/attribute_builder.rbs +515 -26
- data/sig/domainic/attributer/dsl/initializer.rbs +21 -19
- data/sig/domainic/attributer/dsl/method_injector.rbs +16 -14
- data/sig/domainic/attributer/errors/aggregate_error.rbs +28 -0
- data/sig/domainic/attributer/errors/callback_execution_error.rbs +23 -0
- data/sig/domainic/attributer/errors/coercion_execution_error.rbs +29 -0
- data/sig/domainic/attributer/errors/error.rbs +17 -0
- data/sig/domainic/attributer/errors/validation_execution_error.rbs +23 -0
- data/sig/domainic/attributer/instance_methods.rbs +11 -8
- data/sig/domainic/attributer/undefined.rbs +5 -3
- data/sig/domainic/attributer.rbs +88 -27
- metadata +19 -6
@@ -1,15 +1,20 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'domainic/attributer/undefined'
|
4
|
+
|
3
5
|
module Domainic
|
4
6
|
module Attributer
|
5
7
|
module DSL
|
6
8
|
class AttributeBuilder
|
7
|
-
# A class responsible for parsing and normalizing attribute options
|
9
|
+
# A class responsible for parsing and normalizing attribute options
|
8
10
|
#
|
9
11
|
# This class handles the conversion of flexible DSL options into a normalized
|
10
12
|
# format for attribute creation. It supports multiple ways of specifying common
|
11
13
|
# options (like visibility, nullability, validation) and consolidates them
|
12
|
-
# into a consistent internal representation
|
14
|
+
# into a consistent internal representation
|
15
|
+
#
|
16
|
+
# @!visibility private
|
17
|
+
# @api private
|
13
18
|
#
|
14
19
|
# @author {https://aaronmallen.me Aaron Allen}
|
15
20
|
# @since 0.1.0
|
@@ -105,25 +110,62 @@ module Domainic
|
|
105
110
|
# @rbs @options: options
|
106
111
|
# @rbs @result: result
|
107
112
|
|
108
|
-
# Parse attribute options into a normalized format
|
113
|
+
# Parse attribute options into a normalized format
|
109
114
|
#
|
110
115
|
# @param attribute_name [String, Symbol] the name of the attribute
|
111
116
|
# @param attribute_type [String, Symbol] the type of attribute
|
112
|
-
# @param options [Hash] the options to parse
|
117
|
+
# @param options [Hash{String, Symbol => Object}] the options to parse. See {#initialize} for details.
|
113
118
|
#
|
114
|
-
# @return [Hash] normalized options suitable for attribute creation
|
119
|
+
# @return [Hash{Symbol => Object}] normalized options suitable for attribute creation
|
115
120
|
# @rbs (String | Symbol attribute_name, String | Symbol attribute_type, options options) -> void
|
116
121
|
def self.parse!(attribute_name, attribute_type, options)
|
117
122
|
new(attribute_name, attribute_type, options).parse!
|
118
123
|
end
|
119
124
|
|
120
|
-
# Initialize a new OptionParser
|
125
|
+
# Initialize a new OptionParser instance
|
121
126
|
#
|
122
127
|
# @param attribute_name [String, Symbol] the name of the attribute
|
123
128
|
# @param attribute_type [String, Symbol] the type of attribute
|
124
|
-
# @param options [Hash] the options to parse
|
129
|
+
# @param options [Hash{String, Symbol => Object}] the options to parse
|
125
130
|
#
|
126
|
-
# @
|
131
|
+
# @option options [Array<Proc>, Proc] :callbacks handlers for attribute change events (priority over
|
132
|
+
# :callback, :on_change)
|
133
|
+
# @option options [Array<Proc>, Proc] :callback alias for :callbacks
|
134
|
+
# @option options [Array<Proc, Symbol>, Proc, Symbol] :coerce handlers for value coercion (priority over
|
135
|
+
# :coercers, :coerce_with)
|
136
|
+
# @option options [Array<Proc, Symbol>, Proc, Symbol] :coercers alias for :coerce
|
137
|
+
# @option options [Array<Proc, Symbol>, Proc, Symbol] :coerce_with alias for :coerce
|
138
|
+
# @option options [Object] :default the default value (priority over :default_generator, :default_value)
|
139
|
+
# @option options [Object] :default_generator alias for :default
|
140
|
+
# @option options [Object] :default_value alias for :default
|
141
|
+
# @option options [String] :desc short description (overridden by :description)
|
142
|
+
# @option options [String] :description description text
|
143
|
+
# @option options [Boolean] :non_nil require non-nil values (priority over :non_null, :non_nullable, :not_nil,
|
144
|
+
# :not_nilable, :not_null, :not_nullable)
|
145
|
+
# @option options [Boolean] :non_null alias for :non_nil
|
146
|
+
# @option options [Boolean] :non_nullable alias for :non_nil
|
147
|
+
# @option options [Boolean] :not_nil alias for :non_nil
|
148
|
+
# @option options [Boolean] :not_nilable alias for :non_nil
|
149
|
+
# @option options [Boolean] :not_null alias for :non_nil
|
150
|
+
# @option options [Boolean] :not_nullable alias for :non_nil
|
151
|
+
# @option options [Boolean] :null inverse of :non_nil
|
152
|
+
# @option options [Array<Proc>, Proc] :on_change alias for :callbacks
|
153
|
+
# @option options [Boolean] :optional whether attribute is optional (overridden by :required)
|
154
|
+
# @option options [Integer] :position specify order position
|
155
|
+
# @option options [Symbol] :read read visibility (:public, :protected, :private) (priority over :read_access,
|
156
|
+
# :reader)
|
157
|
+
# @option options [Symbol] :read_access alias for :read
|
158
|
+
# @option options [Symbol] :reader alias for :read
|
159
|
+
# @option options [Boolean] :required whether attribute is required
|
160
|
+
# @option options [Array<Object>, Object] :validate validators for the attribute (priority over
|
161
|
+
# :validate_with, :validators)
|
162
|
+
# @option options [Array<Object>, Object] :validate_with alias for :validate
|
163
|
+
# @option options [Array<Object>, Object] :validators alias for :validate
|
164
|
+
# @option options [Symbol] :write_access write visibility (:public, :protected, :private) (priority over
|
165
|
+
# :writer)
|
166
|
+
# @option options [Symbol] :writer alias for :write_access
|
167
|
+
#
|
168
|
+
# @return [OptionParser] the new OptionParser instance
|
127
169
|
# @rbs (String | Symbol attribute_name, String | Symbol attribute_type, options options) -> void
|
128
170
|
def initialize(attribute_name, attribute_type, options)
|
129
171
|
@options = options.transform_keys(&:to_sym)
|
@@ -133,10 +175,10 @@ module Domainic
|
|
133
175
|
@result[:position] = @options[:position] if @options.key?(:position)
|
134
176
|
end
|
135
177
|
|
136
|
-
# Parse the options into a normalized format
|
178
|
+
# Parse the options into a normalized format
|
137
179
|
#
|
138
|
-
# @return [Hash] normalized options suitable for attribute creation
|
139
|
-
# @rbs () ->
|
180
|
+
# @return [Hash{Symbol => Object}] normalized options suitable for attribute creation
|
181
|
+
# @rbs () -> result
|
140
182
|
def parse!
|
141
183
|
parse_options!
|
142
184
|
@result
|
@@ -144,11 +186,11 @@ module Domainic
|
|
144
186
|
|
145
187
|
private
|
146
188
|
|
147
|
-
# Find the last set value among multiple option keys
|
189
|
+
# Find the last set value among multiple option keys
|
148
190
|
#
|
149
191
|
# @param keys [Array<Symbol>] the keys to check
|
150
192
|
#
|
151
|
-
# @return [Object] the last set value or Undefined
|
193
|
+
# @return [Object] the last set value or {Undefined}
|
152
194
|
# @rbs (Array[Symbol]) -> untyped
|
153
195
|
def find_last_option(keys)
|
154
196
|
keys.reverse_each do |key|
|
@@ -158,7 +200,7 @@ module Domainic
|
|
158
200
|
Undefined
|
159
201
|
end
|
160
202
|
|
161
|
-
# Parse accessor (reader/writer) visibility options
|
203
|
+
# Parse accessor (reader/writer) visibility options
|
162
204
|
#
|
163
205
|
# @return [void]
|
164
206
|
# @rbs () -> void
|
@@ -167,7 +209,7 @@ module Domainic
|
|
167
209
|
@result[:write] = find_last_option(ACCESSOR_WRITER_KEYS)
|
168
210
|
end
|
169
211
|
|
170
|
-
# Parse callback handler options
|
212
|
+
# Parse callback handler options
|
171
213
|
#
|
172
214
|
# @return [void]
|
173
215
|
# @rbs () -> void
|
@@ -177,7 +219,7 @@ module Domainic
|
|
177
219
|
end
|
178
220
|
end
|
179
221
|
|
180
|
-
# Parse coercion handler options
|
222
|
+
# Parse coercion handler options
|
181
223
|
#
|
182
224
|
# @return [void]
|
183
225
|
# @rbs () -> void
|
@@ -187,7 +229,7 @@ module Domainic
|
|
187
229
|
end
|
188
230
|
end
|
189
231
|
|
190
|
-
# Parse default value options
|
232
|
+
# Parse default value options
|
191
233
|
#
|
192
234
|
# @return [void]
|
193
235
|
# @rbs () -> void
|
@@ -195,7 +237,7 @@ module Domainic
|
|
195
237
|
@result[:default] = find_last_option(DEFAULT_KEYS)
|
196
238
|
end
|
197
239
|
|
198
|
-
# Parse description options
|
240
|
+
# Parse description options
|
199
241
|
#
|
200
242
|
# @return [void]
|
201
243
|
# @rbs () -> void
|
@@ -203,7 +245,7 @@ module Domainic
|
|
203
245
|
@result[:description] = find_last_option(DESCRIPTION_KEYS)
|
204
246
|
end
|
205
247
|
|
206
|
-
# Parse nilability options
|
248
|
+
# Parse nilability options
|
207
249
|
#
|
208
250
|
# @return [void]
|
209
251
|
# @rbs () -> void
|
@@ -213,7 +255,7 @@ module Domainic
|
|
213
255
|
@result[:nilable] = !(NON_NILABLE_KEYS.any? { |k| @options[k] == true } || @options[:null] == false)
|
214
256
|
end
|
215
257
|
|
216
|
-
# Parse all option types
|
258
|
+
# Parse all option types
|
217
259
|
#
|
218
260
|
# @return [void]
|
219
261
|
# @rbs () -> void
|
@@ -221,7 +263,7 @@ module Domainic
|
|
221
263
|
private_methods.grep(/\Aparse_.*_options!\z/).each { |method| send(method) }
|
222
264
|
end
|
223
265
|
|
224
|
-
# Parse required/optional options
|
266
|
+
# Parse required/optional options
|
225
267
|
#
|
226
268
|
# @return [void]
|
227
269
|
# @rbs () -> void
|
@@ -231,7 +273,7 @@ module Domainic
|
|
231
273
|
@result[:required] = @options[:optional] == false || @options[:required] == true
|
232
274
|
end
|
233
275
|
|
234
|
-
# Parse validator options
|
276
|
+
# Parse validator options
|
235
277
|
#
|
236
278
|
# @return [void]
|
237
279
|
# @rbs () -> void
|