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