domainic-attributer 0.1.0 → 0.2.2
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 +39 -1
- data/README.md +41 -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 +44 -33
- data/lib/domainic/attributer/attribute/validator.rb +47 -17
- data/lib/domainic/attributer/attribute.rb +29 -19
- data/lib/domainic/attributer/attribute_set.rb +23 -21
- 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 +51 -32
- data/sig/domainic/attributer/attribute/validator.rbs +28 -13
- data/sig/domainic/attributer/attribute.rbs +41 -17
- data/sig/domainic/attributer/attribute_set.rbs +21 -19
- data/sig/domainic/attributer/class_methods.rbs +190 -83
- data/sig/domainic/attributer/dsl/attribute_builder/option_parser.rbs +56 -22
- data/sig/domainic/attributer/dsl/attribute_builder.rbs +521 -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 +19 -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 +21 -13
- data/sig/domainic/attributer/dsl.rbs +0 -1
- data/sig/domainic-attributer.rbs +0 -1
@@ -1,12 +1,14 @@
|
|
1
1
|
module Domainic
|
2
2
|
module Attributer
|
3
3
|
class Attribute
|
4
|
-
# A class responsible for managing change callbacks for an attribute
|
4
|
+
# A class responsible for managing change callbacks for an attribute
|
5
5
|
#
|
6
6
|
# This class handles the execution of callbacks that are triggered when an
|
7
7
|
# attribute's value changes. Each callback must be a Proc that accepts two
|
8
|
-
# arguments: the old value and the new value
|
8
|
+
# arguments: the old value, and the new value
|
9
9
|
#
|
10
|
+
# @api private
|
11
|
+
# @!visibility private
|
10
12
|
# @author {https://aaronmallen.me Aaron Allen}
|
11
13
|
# @since 0.1.0
|
12
14
|
class Callback
|
@@ -16,26 +18,27 @@ module Domainic
|
|
16
18
|
|
17
19
|
include BelongsToAttribute
|
18
20
|
|
19
|
-
# Initialize a new Callback instance
|
21
|
+
# Initialize a new Callback instance
|
20
22
|
#
|
21
|
-
# @param attribute [Attribute] the
|
23
|
+
# @param attribute [Attribute] the {Attribute} this instance belongs to
|
22
24
|
# @param handlers [Array<Proc>] the handlers to use for processing
|
23
25
|
#
|
24
|
-
# @return [Callback] the new instance
|
26
|
+
# @return [Callback] the new Callback instance
|
25
27
|
def initialize: (Attribute attribute, Array[handler] | handler handlers) -> void
|
26
28
|
|
27
|
-
# Execute all callbacks for a value change
|
29
|
+
# Execute all callbacks for a value change
|
28
30
|
#
|
29
31
|
# @param instance [Object] the instance on which to execute callbacks
|
30
32
|
# @param old_value [Object] the previous value
|
31
33
|
# @param new_value [Object] the new value
|
32
34
|
#
|
35
|
+
# @raise [CallbackExecutionError] if any callback handlers raises an error
|
33
36
|
# @return [void]
|
34
37
|
def call: (untyped instance, untyped old_value, untyped new_value) -> void
|
35
38
|
|
36
39
|
private
|
37
40
|
|
38
|
-
# Validate that a callback handler is a valid Proc
|
41
|
+
# Validate that a callback handler is a valid Proc
|
39
42
|
#
|
40
43
|
# @param handler [Object] the handler to validate
|
41
44
|
#
|
@@ -1,12 +1,14 @@
|
|
1
1
|
module Domainic
|
2
2
|
module Attributer
|
3
3
|
class Attribute
|
4
|
-
# A class responsible for coercing attribute values
|
4
|
+
# A class responsible for coercing attribute values
|
5
5
|
#
|
6
6
|
# This class manages the coercion of values assigned to an attribute. Coercion can be
|
7
|
-
# handled by either a Proc that accepts a single value argument, or by referencing an
|
8
|
-
# instance method via Symbol
|
7
|
+
# handled by either a {Proc} that accepts a single value argument, or by referencing an
|
8
|
+
# instance method via {Symbol}
|
9
9
|
#
|
10
|
+
# @api private
|
11
|
+
# @!visibility private
|
10
12
|
# @author {https://aaronmallen.me Aaron Allen}
|
11
13
|
# @since 0.1.0
|
12
14
|
class Coercer
|
@@ -18,25 +20,26 @@ module Domainic
|
|
18
20
|
|
19
21
|
@handlers: Array[handler]
|
20
22
|
|
21
|
-
# Initialize a new Coercer instance
|
23
|
+
# Initialize a new {Coercer} instance
|
22
24
|
#
|
23
|
-
# @param attribute [Attribute] the
|
25
|
+
# @param attribute [Attribute] the {Attribute} this instance belongs to
|
24
26
|
# @param handlers [Array<Proc, Symbol>] the handlers to use for processing
|
25
27
|
#
|
26
|
-
# @return [Coercer] the new instance
|
28
|
+
# @return [Coercer] the new Coercer instance
|
27
29
|
def initialize: (Attribute attribute, Array[handler] | handler handlers) -> void
|
28
30
|
|
29
|
-
# Process a value through all coercion handlers
|
31
|
+
# Process a value through all coercion handlers
|
30
32
|
#
|
31
33
|
# @param instance [Object] the instance on which to perform coercion
|
32
34
|
# @param value [Object] the value to coerce
|
33
35
|
#
|
34
|
-
# @
|
35
|
-
|
36
|
+
# @raise [CoercionExecutionError] if a coercion handler raises an error
|
37
|
+
# @return [Object, nil] the coerced value
|
38
|
+
def call: (untyped instance, untyped? value) -> untyped?
|
36
39
|
|
37
40
|
private
|
38
41
|
|
39
|
-
# Process a value through a single coercion handler
|
42
|
+
# Process a value through a single coercion handler
|
40
43
|
#
|
41
44
|
# @param instance [Object] the instance on which to perform coercion
|
42
45
|
# @param handler [Proc, Symbol] the coercion handler
|
@@ -46,7 +49,7 @@ module Domainic
|
|
46
49
|
# @return [Object] the coerced value
|
47
50
|
def coerce_value: (untyped instance, handler, untyped value) -> untyped
|
48
51
|
|
49
|
-
# Validate that a coercion handler is valid
|
52
|
+
# Validate that a coercion handler is valid
|
50
53
|
#
|
51
54
|
# @param handler [Object] the handler to validate
|
52
55
|
#
|
@@ -1,43 +1,45 @@
|
|
1
1
|
module Domainic
|
2
2
|
module Attributer
|
3
3
|
class Attribute
|
4
|
-
# A mixin providing common functionality for classes that belong to an Attribute
|
4
|
+
# A mixin providing common functionality for classes that belong to an Attribute
|
5
5
|
#
|
6
6
|
# This module provides initialization and duplication behavior for classes that are owned
|
7
7
|
# by and work in conjunction with an Attribute instance. These classes typically handle
|
8
|
-
# specific aspects of attribute processing such as coercion, validation, or callbacks
|
8
|
+
# specific aspects of attribute processing such as coercion, validation, or callbacks
|
9
9
|
#
|
10
|
+
# @api private
|
11
|
+
# @!visibility private
|
10
12
|
# @author {https://aaronmallen.me Aaron Allen}
|
11
13
|
# @since 0.1.0
|
12
14
|
module BelongsToAttribute
|
13
15
|
@attribute: Attribute
|
14
16
|
|
15
|
-
# Initialize a new instance that belongs to an
|
17
|
+
# Initialize a new instance that belongs to an {Attribute}
|
16
18
|
#
|
17
|
-
# @param attribute [Attribute] the
|
19
|
+
# @param attribute [Attribute] the {Attribute} this instance belongs to
|
18
20
|
#
|
19
|
-
# @return [
|
21
|
+
# @return [BelongsToAttribute] the new BelongsToAttribute instance
|
20
22
|
def initialize: (Attribute attribute, *untyped, **untyped) -> void
|
21
23
|
|
22
|
-
# Create a duplicate instance associated with a new
|
24
|
+
# Create a duplicate instance associated with a new {Attribute}
|
23
25
|
#
|
24
26
|
# @param new_attribute [Attribute] the new attribute to associate with
|
25
27
|
#
|
26
|
-
# @return [BelongsToAttribute]
|
28
|
+
# @return [BelongsToAttribute] duplicate instance with new {Attribute}
|
27
29
|
def dup_with_attribute: (Attribute attribute) -> BelongsToAttribute
|
28
30
|
|
29
31
|
private
|
30
32
|
|
31
|
-
# Generate a method name for error messages
|
33
|
+
# Generate a method name for error messages
|
32
34
|
#
|
33
|
-
# @return [String]
|
35
|
+
# @return [String] formatted method name
|
34
36
|
def attribute_method_name: () -> String
|
35
37
|
|
36
|
-
# Ensure that an
|
38
|
+
# Ensure that an {Attribute} is a valid {Attribute} instance
|
37
39
|
#
|
38
|
-
# @param attribute [Attribute] the
|
40
|
+
# @param attribute [Attribute] the {Attribute} to validate
|
39
41
|
#
|
40
|
-
# @raise [TypeError] if the attribute is not a valid
|
42
|
+
# @raise [TypeError] if the attribute is not a valid Attribute instance
|
41
43
|
# @return [void]
|
42
44
|
def validate_attribute!: (Attribute attribute) -> void
|
43
45
|
end
|
@@ -1,12 +1,14 @@
|
|
1
1
|
module Domainic
|
2
2
|
module Attributer
|
3
3
|
class Attribute
|
4
|
-
# A class responsible for managing attribute signature information
|
4
|
+
# A class responsible for managing attribute signature information
|
5
5
|
#
|
6
|
-
# This class encapsulates the type and visibility configuration for an
|
6
|
+
# This class encapsulates the type and visibility configuration for an {Attribute}
|
7
7
|
# It validates and manages whether an attribute is an argument or option, as well
|
8
|
-
# as controlling read and write visibility (public, protected, or private)
|
8
|
+
# as controlling read and write visibility (public, protected, or private)
|
9
9
|
#
|
10
|
+
# @api private
|
11
|
+
# @!visibility private
|
10
12
|
# @author {https://aaronmallen.me Aaron Allen}
|
11
13
|
# @since 0.1.0
|
12
14
|
class Signature
|
@@ -20,10 +22,10 @@ module Domainic
|
|
20
22
|
|
21
23
|
include BelongsToAttribute
|
22
24
|
|
23
|
-
# @return [Hash{Symbol => Object}] Default options for a new Signature instance
|
25
|
+
# @return [Hash{Symbol => Object}] Default options for a new Signature instance
|
24
26
|
DEFAULT_OPTIONS: default_options
|
25
27
|
|
26
|
-
# Constants defining valid attribute types
|
28
|
+
# Constants defining valid attribute types
|
27
29
|
#
|
28
30
|
# @author {https://aaronmallen.me Aaron Allen}
|
29
31
|
# @since 0.1.0
|
@@ -38,7 +40,7 @@ module Domainic
|
|
38
40
|
ALL: Array[type_symbol]
|
39
41
|
end
|
40
42
|
|
41
|
-
# Constants defining valid visibility levels
|
43
|
+
# Constants defining valid visibility levels
|
42
44
|
#
|
43
45
|
# @author {https://aaronmallen.me Aaron Allen}
|
44
46
|
# @since 0.1.0
|
@@ -68,110 +70,127 @@ module Domainic
|
|
68
70
|
|
69
71
|
@type: type_symbol
|
70
72
|
|
73
|
+
# Get the position of the attribute
|
74
|
+
#
|
71
75
|
# @return [Integer, nil] the position of the attribute
|
72
76
|
attr_reader position: Integer?
|
73
77
|
|
78
|
+
# Get the visibility level for reading the attribute
|
79
|
+
#
|
74
80
|
# @return [Symbol] the visibility level for reading the attribute
|
75
81
|
attr_reader read_visibility: visibility_symbol
|
76
82
|
|
83
|
+
# Get the attribute type
|
84
|
+
#
|
77
85
|
# @return [Symbol] the type of the attribute
|
78
86
|
attr_reader type: type_symbol
|
79
87
|
|
88
|
+
# Get the visibility level for writing the attribute
|
89
|
+
#
|
80
90
|
# @return [Symbol] the visibility level for writing the attribute
|
81
91
|
attr_reader write_visibility: visibility_symbol
|
82
92
|
|
83
|
-
# Initialize a new Signature instance
|
93
|
+
# Initialize a new {Signature} instance
|
84
94
|
#
|
85
|
-
# @param attribute [Attribute] the
|
95
|
+
# @param attribute [Attribute] the {Attribute} this instance belongs to
|
86
96
|
# @param options [Hash{Symbol => Object}] the signature options
|
87
|
-
# @option options [Boolean] nilable (true) whether the attribute is allowed to be nil
|
97
|
+
# @option options [Boolean] nilable (true) whether the attribute is allowed to be nil
|
88
98
|
# @option options [Integer, nil] position (nil) optional position for ordered attributes
|
89
99
|
# @option options [Symbol] read (:public) the read visibility
|
90
100
|
# @option options [Boolean] required (false) whether the attribute is required
|
91
101
|
# @option options [Symbol] type the type of attribute
|
92
102
|
# @option options [Symbol] write (:public) the write visibility
|
93
103
|
#
|
94
|
-
# @
|
104
|
+
# @raise [ArgumentError] if the configuration is invalid
|
105
|
+
# @return [Signature] the new Signature instance
|
106
|
+
# Attribute attribute,
|
107
|
+
# ?nilable: bool,
|
108
|
+
# ?position: Integer?,
|
109
|
+
# ?read: visibility_symbol,
|
110
|
+
# ?required: bool,
|
111
|
+
# type: type_symbol,
|
112
|
+
# ?write: visibility_symbol
|
113
|
+
# ) -> void
|
95
114
|
def initialize: (Attribute attribute, type: type_symbol, ?nilable: bool, ?position: Integer?, ?read: visibility_symbol, ?required: bool, ?write: visibility_symbol) -> void
|
96
115
|
|
97
|
-
# Check if this signature is for an argument attribute
|
116
|
+
# Check if this signature is for an argument attribute
|
98
117
|
#
|
99
118
|
# @return [Boolean] true if this is an argument attribute
|
100
119
|
def argument?: () -> bool
|
101
120
|
|
102
|
-
# Check if the attribute
|
121
|
+
# Check if the attribute allows nil values
|
103
122
|
#
|
104
|
-
# @return [Boolean] true if the attribute
|
123
|
+
# @return [Boolean] true if the attribute allows nil values
|
105
124
|
def nilable?: () -> bool
|
106
125
|
|
107
|
-
# Check if this signature is for an option attribute
|
126
|
+
# Check if this signature is for an option attribute
|
108
127
|
#
|
109
128
|
# @return [Boolean] true if this is an option attribute
|
110
129
|
def option?: () -> bool
|
111
130
|
|
112
|
-
# Check if this signature is for an optional attribute
|
131
|
+
# Check if this signature is for an optional attribute
|
113
132
|
#
|
114
133
|
# @return [Boolean] true if this is an optional attribute
|
115
134
|
def optional?: () -> bool
|
116
135
|
|
117
|
-
# Check if both read and write operations are private
|
136
|
+
# Check if both read and write operations are private
|
118
137
|
#
|
119
138
|
# @return [Boolean] true if both read and write are private
|
120
139
|
def private?: () -> bool
|
121
140
|
|
122
|
-
# Check if read operations are private
|
141
|
+
# Check if read operations are private
|
123
142
|
#
|
124
143
|
# @return [Boolean] true if read operations are private
|
125
144
|
def private_read?: () -> bool
|
126
145
|
|
127
|
-
# Check if write operations are private
|
146
|
+
# Check if write operations are private
|
128
147
|
#
|
129
148
|
# @return [Boolean] true if write operations are private
|
130
149
|
def private_write?: () -> bool
|
131
150
|
|
132
|
-
# Check if both read and write operations are protected
|
151
|
+
# Check if both read and write operations are protected
|
133
152
|
#
|
134
153
|
# @return [Boolean] true if both read and write are protected
|
135
154
|
def protected?: () -> bool
|
136
155
|
|
137
|
-
# Check if read operations are protected
|
156
|
+
# Check if read operations are protected
|
138
157
|
#
|
139
158
|
# @return [Boolean] true if read operations are protected
|
140
159
|
def protected_read?: () -> bool
|
141
160
|
|
142
|
-
# Check if write operations are protected
|
161
|
+
# Check if write operations are protected
|
143
162
|
#
|
144
163
|
# @return [Boolean] true if write operations are protected
|
145
164
|
def protected_write?: () -> bool
|
146
165
|
|
147
|
-
# Check if both read and write operations are public
|
166
|
+
# Check if both read and write operations are public
|
148
167
|
#
|
149
168
|
# @return [Boolean] true if both read and write are public
|
150
169
|
def public?: () -> bool
|
151
170
|
|
152
|
-
# Check if read operations are public
|
171
|
+
# Check if read operations are public
|
153
172
|
#
|
154
173
|
# @return [Boolean] true if read operations are public
|
155
174
|
def public_read?: () -> bool
|
156
175
|
|
157
|
-
# Check if write operations are public
|
176
|
+
# Check if write operations are public
|
158
177
|
#
|
159
178
|
# @return [Boolean] true if write operations are public
|
160
179
|
def public_write?: () -> bool
|
161
180
|
|
162
|
-
# Check if
|
181
|
+
# Check if this signature requires an attribute value
|
163
182
|
#
|
164
|
-
# @return [Boolean] true if
|
183
|
+
# @return [Boolean] true if this signature requires an attribute value
|
165
184
|
def required?: () -> bool
|
166
185
|
|
167
186
|
private
|
168
187
|
|
169
|
-
# Get signature options as a hash
|
188
|
+
# Get signature options as a hash
|
170
189
|
#
|
171
190
|
# @return [Hash] the signature options
|
172
191
|
def to_options: () -> initialize_options
|
173
192
|
|
174
|
-
# Validate that a value is a
|
193
|
+
# Validate that a value is a boolean
|
175
194
|
#
|
176
195
|
# @param name [String, Symbol] the name of the attribute being validated
|
177
196
|
# @param value [Boolean] the value to validate
|
@@ -180,7 +199,7 @@ module Domainic
|
|
180
199
|
# @return [void]
|
181
200
|
def validate_boolean!: (String | Symbol name, bool value) -> void
|
182
201
|
|
183
|
-
# Validate all initialization options
|
202
|
+
# Validate all initialization options
|
184
203
|
#
|
185
204
|
# @param options [Hash{Symbol => Object}] the options to validate
|
186
205
|
# @option options [Boolean] nilable the nilable flag to validate
|
@@ -193,7 +212,7 @@ module Domainic
|
|
193
212
|
# @return [void]
|
194
213
|
def validate_initialize_options!: (Hash[Symbol, untyped] options) -> void
|
195
214
|
|
196
|
-
# Validate that a position value is valid
|
215
|
+
# Validate that a position value is valid
|
197
216
|
#
|
198
217
|
# @param position [Integer, nil] the position to validate
|
199
218
|
#
|
@@ -201,7 +220,7 @@ module Domainic
|
|
201
220
|
# @return [void]
|
202
221
|
def validate_position!: (Integer? position) -> void
|
203
222
|
|
204
|
-
# Validate that a type value is valid
|
223
|
+
# Validate that a type value is valid
|
205
224
|
#
|
206
225
|
# @param type [Symbol] the type to validate
|
207
226
|
#
|
@@ -209,7 +228,7 @@ module Domainic
|
|
209
228
|
# @return [void]
|
210
229
|
def validate_type!: (type_symbol type) -> void
|
211
230
|
|
212
|
-
# Validate that visibility values are valid
|
231
|
+
# Validate that visibility values are valid
|
213
232
|
#
|
214
233
|
# @param type [Symbol] which visibility setting to validate
|
215
234
|
# @param value [Symbol] the visibility value to validate
|
@@ -1,12 +1,14 @@
|
|
1
1
|
module Domainic
|
2
2
|
module Attributer
|
3
3
|
class Attribute
|
4
|
-
# A class responsible for validating attribute values
|
4
|
+
# A class responsible for validating attribute values
|
5
5
|
#
|
6
6
|
# This class manages the validation of values assigned to an attribute. Validation
|
7
|
-
# can be performed either by a Proc that accepts a single value argument and returns
|
8
|
-
# a boolean, or by any object that responds to the `===` operator
|
7
|
+
# can be performed either by a {Proc} that accepts a single value argument and returns
|
8
|
+
# a boolean, or by any object that responds to the `===` operator
|
9
9
|
#
|
10
|
+
# @api private
|
11
|
+
# @!visibility private
|
10
12
|
# @author {https://aaronmallen.me Aaron Allen}
|
11
13
|
# @since 0.1.0
|
12
14
|
class Validator
|
@@ -32,38 +34,49 @@ module Domainic
|
|
32
34
|
|
33
35
|
@handlers: Array[handler]
|
34
36
|
|
35
|
-
# Initialize a new Validator instance
|
37
|
+
# Initialize a new Validator instance
|
36
38
|
#
|
37
|
-
# @param attribute [Attribute] the
|
39
|
+
# @param attribute [Attribute] the {Attribute} this instance belongs to
|
38
40
|
# @param handlers [Array<Class, Module, Object, Proc>] the handlers to use for processing
|
39
41
|
#
|
40
|
-
# @return [Validator] the new instance
|
42
|
+
# @return [Validator] the new Validator instance
|
41
43
|
def initialize: (Attribute attribute, Array[handler] | handler handlers) -> void
|
42
44
|
|
43
|
-
# Validate a value using all configured validators
|
45
|
+
# Validate a value using all configured validators
|
44
46
|
#
|
45
47
|
# @param instance [Object] the instance on which to perform validation
|
46
48
|
# @param value [Object] the value to validate
|
47
49
|
#
|
48
50
|
# @raise [ArgumentError] if the value fails validation
|
51
|
+
# @raise [ValidationExecutionError] if errors occur during validation execution
|
49
52
|
# @return [void]
|
50
53
|
def call: (untyped instance, untyped value) -> void
|
51
54
|
|
52
55
|
private
|
53
56
|
|
54
|
-
# Handle a
|
57
|
+
# Handle a nil value
|
55
58
|
#
|
56
59
|
# @raise [ArgumentError] if the attribute is not nilable
|
57
|
-
# @return [
|
60
|
+
# @return [Boolean] true if the attribute is nilable
|
58
61
|
def handle_nil!: () -> bool
|
59
62
|
|
60
|
-
# Handle an {Undefined} value
|
63
|
+
# Handle an {Undefined} value
|
61
64
|
#
|
62
65
|
# @raise [ArgumentError] if the attribute is required
|
63
|
-
# @return [
|
66
|
+
# @return [Boolean] true if the attribute is optional
|
64
67
|
def handle_undefined!: () -> bool
|
65
68
|
|
66
|
-
#
|
69
|
+
# Run all configured validations
|
70
|
+
#
|
71
|
+
# @param instance [Object] the instance on which to perform validation
|
72
|
+
# @param value [Object] the value to validate
|
73
|
+
#
|
74
|
+
# @raise [ArgumentError] if the value fails validation
|
75
|
+
# @raise [ValidationExecutionError] if errors occur during validation execution
|
76
|
+
# @return [void]
|
77
|
+
def run_validations!: (untyped instance, untyped value) -> void
|
78
|
+
|
79
|
+
# Validate that a validation handler is valid
|
67
80
|
#
|
68
81
|
# @param handler [Object] the handler to validate
|
69
82
|
#
|
@@ -71,11 +84,13 @@ module Domainic
|
|
71
84
|
# @return [void]
|
72
85
|
def validate_handler!: (handler handler) -> void
|
73
86
|
|
74
|
-
# Validate a value using a single handler
|
87
|
+
# Validate a value using a single handler
|
75
88
|
#
|
76
89
|
# @param handler [Object] the handler to use for validation
|
77
90
|
# @param instance [Object] the instance on which to perform validation
|
78
91
|
# @param value [Object] the value to validate
|
92
|
+
#
|
93
|
+
# @return [Boolean] true if validation succeeds
|
79
94
|
def validate_value!: (handler handler, untyped instance, untyped value) -> bool
|
80
95
|
end
|
81
96
|
end
|
@@ -1,12 +1,14 @@
|
|
1
1
|
module Domainic
|
2
2
|
module Attributer
|
3
|
-
# A class representing a managed attribute in the Domainic::Attributer system
|
3
|
+
# A class representing a managed attribute in the {Domainic::Attributer} system
|
4
4
|
#
|
5
5
|
# This class serves as the core component of the attribute management system.
|
6
6
|
# It coordinates type information, visibility settings, value coercion,
|
7
7
|
# validation, and change notifications for an attribute. Each instance
|
8
|
-
# represents a single attribute definition within a class
|
8
|
+
# represents a single attribute definition within a class
|
9
9
|
#
|
10
|
+
# @api private
|
11
|
+
# @!visibility private
|
10
12
|
# @author {https://aaronmallen.me Aaron Allen}
|
11
13
|
# @since 0.1.0
|
12
14
|
class Attribute
|
@@ -28,22 +30,30 @@ module Domainic
|
|
28
30
|
|
29
31
|
@base: __todo__
|
30
32
|
|
33
|
+
# Get the class or module this attribute belongs to
|
34
|
+
#
|
31
35
|
# @return [Class, Module] the class or module this attribute belongs to
|
32
36
|
attr_reader base: __todo__
|
33
37
|
|
38
|
+
# Get the description of the attribute
|
39
|
+
#
|
34
40
|
# @return [String, nil] the description of the attribute
|
35
41
|
attr_reader description: String?
|
36
42
|
|
43
|
+
# Get the name of the attribute
|
44
|
+
#
|
37
45
|
# @return [Symbol] the name of the attribute
|
38
46
|
attr_reader name: Symbol
|
39
47
|
|
48
|
+
# Get the signature configuration for this attribute
|
49
|
+
#
|
40
50
|
# @return [Signature] the signature configuration for this attribute
|
41
51
|
attr_reader signature: Signature
|
42
52
|
|
43
|
-
# Initialize a new Attribute instance
|
53
|
+
# Initialize a new {Attribute} instance
|
44
54
|
#
|
45
55
|
# @param base [Class, Module] the class or module this attribute belongs to
|
46
|
-
# @param options [Hash] the options to create the attribute with
|
56
|
+
# @param options [Hash{Symbol => Object}] the options to create the attribute with
|
47
57
|
# @option options [Array<Proc>, Proc] :callbacks callbacks to trigger on value changes
|
48
58
|
# @option options [Array<Proc, Symbol>, Proc, Symbol] :coercers handlers for value coercion
|
49
59
|
# @option options [Object] :default the default value or generator
|
@@ -58,11 +68,24 @@ module Domainic
|
|
58
68
|
# @option options [Symbol] :write the write visibility
|
59
69
|
#
|
60
70
|
# @raise [ArgumentError] if the configuration is invalid
|
61
|
-
# @return [
|
62
|
-
#
|
71
|
+
# @return [Attribute] the new Attribute instance
|
72
|
+
# __todo__ base,
|
73
|
+
# ?callbacks: Array[Callback::handler] | Callback::handler,
|
74
|
+
# ?coercers: Array[Coercer::handler] | Coercer::handler,
|
75
|
+
# ?default: untyped,
|
76
|
+
# ?description: String?,
|
77
|
+
# name: String | Symbol,
|
78
|
+
# ?nilable: bool,
|
79
|
+
# ?position: Integer?,
|
80
|
+
# ?read: Signature::visibility_symbol,
|
81
|
+
# ?required: bool,
|
82
|
+
# type: Signature::type_symbol,
|
83
|
+
# ?validators: Array[Validator::handler] | Validator::handler,
|
84
|
+
# ?write: Signature::visibility_symbol
|
85
|
+
# ) -> void
|
63
86
|
def initialize: (__todo__ base, name: String | Symbol, type: Signature::type_symbol, ?callbacks: Array[Callback::handler] | Callback::handler, ?coercers: Array[Coercer::handler] | Coercer::handler, ?default: untyped, ?description: String?, ?nilable: bool, ?position: Integer?, ?read: Signature::visibility_symbol, ?required: bool, ?validators: Array[Validator::handler] | Validator::handler, ?write: Signature::visibility_symbol) -> void
|
64
87
|
|
65
|
-
# Apply a value to the attribute on an instance
|
88
|
+
# Apply a value to the attribute on an instance
|
66
89
|
#
|
67
90
|
# This method applies all attribute constraints (coercion, validation) to a value
|
68
91
|
# and sets it on the given instance. It manages the complete lifecycle of setting
|
@@ -80,36 +103,37 @@ module Domainic
|
|
80
103
|
# @return [void]
|
81
104
|
def apply!: (untyped instance, untyped value) -> void
|
82
105
|
|
83
|
-
# Check if this attribute has a default value
|
106
|
+
# Check if this attribute has a default value
|
84
107
|
#
|
85
108
|
# @return [Boolean] true if a default value is set
|
86
109
|
def default?: () -> bool
|
87
110
|
|
88
|
-
# Create a duplicate instance for a new base class
|
111
|
+
# Create a duplicate instance for a new base class
|
89
112
|
#
|
90
113
|
# @param new_base [Class, Module] the new base class
|
91
114
|
#
|
115
|
+
# @raise [ArgumentError] if the new base is invalid
|
92
116
|
# @return [Attribute] the duplicated instance
|
93
117
|
def dup_with_base: (__todo__ new_base) -> Attribute
|
94
118
|
|
95
|
-
# Generate the default value for this attribute
|
119
|
+
# Generate the default value for this attribute
|
96
120
|
#
|
97
121
|
# @param instance [Object] the instance to generate the default for
|
98
122
|
#
|
99
123
|
# @return [Object] the generated default value
|
100
124
|
def generate_default: (untyped instance) -> untyped
|
101
125
|
|
102
|
-
# Merge this attribute's configuration with another
|
126
|
+
# Merge this attribute's configuration with another
|
103
127
|
#
|
104
128
|
# @param other [Attribute] the attribute to merge with
|
105
129
|
#
|
106
|
-
# @raise [ArgumentError] if other is not an Attribute
|
130
|
+
# @raise [ArgumentError] if other is not an {Attribute}
|
107
131
|
# @return [Attribute] a new attribute with merged configuration
|
108
132
|
def merge: (Attribute other) -> Attribute
|
109
133
|
|
110
134
|
private
|
111
135
|
|
112
|
-
# Apply initialization options to create attribute components
|
136
|
+
# Apply initialization options to create attribute components
|
113
137
|
#
|
114
138
|
# @param base [Class, Module] the base class
|
115
139
|
# @param options [Hash] the initialization options
|
@@ -117,19 +141,19 @@ module Domainic
|
|
117
141
|
# @return [void]
|
118
142
|
def apply_initialize_options!: (__todo__ base, initialize_options options) -> void
|
119
143
|
|
120
|
-
# Initialize a copy of this attribute
|
144
|
+
# Initialize a copy of this attribute
|
121
145
|
#
|
122
146
|
# @param source [Attribute] the source attribute
|
123
147
|
#
|
124
148
|
# @return [Attribute] the initialized copy
|
125
149
|
def initialize_copy: ...
|
126
150
|
|
127
|
-
# Get this attribute's configuration as options
|
151
|
+
# Get this attribute's configuration as options
|
128
152
|
#
|
129
153
|
# @return [Hash] the configuration options
|
130
154
|
def to_options: () -> initialize_options
|
131
155
|
|
132
|
-
# Validate and apply initialization options
|
156
|
+
# Validate and apply initialization options
|
133
157
|
#
|
134
158
|
# @param base [Class, Module] the base class
|
135
159
|
# @param options [Hash] the initialization options
|
@@ -137,7 +161,7 @@ module Domainic
|
|
137
161
|
# @return [void]
|
138
162
|
def validate_and_apply_initialize_options!: (__todo__ base, initialize_options options) -> void
|
139
163
|
|
140
|
-
# Validate initialization options
|
164
|
+
# Validate initialization options
|
141
165
|
#
|
142
166
|
# @param base [Class, Module] the base class
|
143
167
|
# @param options [Hash] the initialization options
|