domainic-type 0.1.0.alpha.2.1.0 → 0.1.0.alpha.3.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +14 -0
- data/LICENSE +1 -1
- data/README.md +28 -4
- data/lib/domainic/type/accessors.rb +41 -0
- data/lib/domainic/type/behavior/enumerable_behavior.rb +262 -0
- data/lib/domainic/type/behavior/numeric_behavior.rb +340 -0
- data/lib/domainic/type/behavior/sizable_behavior.rb +246 -0
- data/lib/domainic/type/behavior/string_behavior.rb +379 -0
- data/lib/domainic/type/behavior.rb +239 -0
- data/lib/domainic/type/config/registry.yml +101 -0
- data/lib/domainic/type/constraint/behavior.rb +342 -0
- data/lib/domainic/type/constraint/constraints/all_constraint.rb +81 -0
- data/lib/domainic/type/constraint/constraints/and_constraint.rb +105 -0
- data/lib/domainic/type/constraint/constraints/any_constraint.rb +83 -0
- data/lib/domainic/type/constraint/constraints/case_constraint.rb +104 -0
- data/lib/domainic/type/constraint/constraints/character_set_constraint.rb +111 -0
- data/lib/domainic/type/constraint/constraints/divisibility_constraint.rb +126 -0
- data/lib/domainic/type/constraint/constraints/emptiness_constraint.rb +69 -0
- data/lib/domainic/type/constraint/constraints/equality_constraint.rb +75 -0
- data/lib/domainic/type/constraint/constraints/finiteness_constraint.rb +123 -0
- data/lib/domainic/type/constraint/constraints/inclusion_constraint.rb +74 -0
- data/lib/domainic/type/constraint/constraints/match_pattern_constraint.rb +87 -0
- data/lib/domainic/type/constraint/constraints/method_presence_constraint.rb +72 -0
- data/lib/domainic/type/constraint/constraints/none_constraint.rb +83 -0
- data/lib/domainic/type/constraint/constraints/nor_constraint.rb +105 -0
- data/lib/domainic/type/constraint/constraints/not_constraint.rb +76 -0
- data/lib/domainic/type/constraint/constraints/or_constraint.rb +106 -0
- data/lib/domainic/type/constraint/constraints/ordering_constraint.rb +75 -0
- data/lib/domainic/type/constraint/constraints/parity_constraint.rb +102 -0
- data/lib/domainic/type/constraint/constraints/polarity_constraint.rb +147 -0
- data/lib/domainic/type/constraint/constraints/range_constraint.rb +135 -0
- data/lib/domainic/type/constraint/constraints/type_constraint.rb +110 -0
- data/lib/domainic/type/constraint/constraints/uniqueness_constraint.rb +69 -0
- data/lib/domainic/type/constraint/resolver.rb +172 -0
- data/lib/domainic/type/constraint/set.rb +266 -0
- data/lib/domainic/type/definitions.rb +364 -0
- data/lib/domainic/type/types/core/array_type.rb +48 -0
- data/lib/domainic/type/types/core/float_type.rb +39 -0
- data/lib/domainic/type/types/core/hash_type.rb +143 -0
- data/lib/domainic/type/types/core/integer_type.rb +38 -0
- data/lib/domainic/type/types/core/string_type.rb +51 -0
- data/lib/domainic/type/types/core/symbol_type.rb +51 -0
- data/lib/domainic/type/types/specification/anything_type.rb +22 -0
- data/lib/domainic/type/types/specification/duck_type.rb +55 -0
- data/lib/domainic/type/types/specification/enum_type.rb +26 -0
- data/lib/domainic/type/types/specification/union_type.rb +26 -0
- data/lib/domainic/type/types/specification/void_type.rb +12 -0
- data/lib/domainic/type.rb +7 -0
- data/lib/domainic-type.rb +3 -0
- data/sig/domainic/type/accessors.rbs +22 -0
- data/sig/domainic/type/behavior/enumerable_behavior.rbs +238 -0
- data/sig/domainic/type/behavior/numeric_behavior.rbs +299 -0
- data/sig/domainic/type/behavior/sizable_behavior.rbs +218 -0
- data/sig/domainic/type/behavior/string_behavior.rbs +315 -0
- data/sig/domainic/type/behavior.rbs +153 -0
- data/sig/domainic/type/constraint/behavior.rbs +258 -0
- data/sig/domainic/type/constraint/constraints/all_constraint.rbs +55 -0
- data/sig/domainic/type/constraint/constraints/and_constraint.rbs +72 -0
- data/sig/domainic/type/constraint/constraints/any_constraint.rbs +57 -0
- data/sig/domainic/type/constraint/constraints/case_constraint.rbs +73 -0
- data/sig/domainic/type/constraint/constraints/character_set_constraint.rbs +82 -0
- data/sig/domainic/type/constraint/constraints/divisibility_constraint.rbs +91 -0
- data/sig/domainic/type/constraint/constraints/emptiness_constraint.rbs +54 -0
- data/sig/domainic/type/constraint/constraints/equality_constraint.rbs +60 -0
- data/sig/domainic/type/constraint/constraints/finiteness_constraint.rbs +82 -0
- data/sig/domainic/type/constraint/constraints/inclusion_constraint.rbs +59 -0
- data/sig/domainic/type/constraint/constraints/match_pattern_constraint.rbs +66 -0
- data/sig/domainic/type/constraint/constraints/method_presence_constraint.rbs +51 -0
- data/sig/domainic/type/constraint/constraints/none_constraint.rbs +57 -0
- data/sig/domainic/type/constraint/constraints/nor_constraint.rbs +72 -0
- data/sig/domainic/type/constraint/constraints/not_constraint.rbs +56 -0
- data/sig/domainic/type/constraint/constraints/or_constraint.rbs +74 -0
- data/sig/domainic/type/constraint/constraints/ordering_constraint.rbs +60 -0
- data/sig/domainic/type/constraint/constraints/parity_constraint.rbs +71 -0
- data/sig/domainic/type/constraint/constraints/polarity_constraint.rbs +101 -0
- data/sig/domainic/type/constraint/constraints/range_constraint.rbs +88 -0
- data/sig/domainic/type/constraint/constraints/type_constraint.rbs +86 -0
- data/sig/domainic/type/constraint/constraints/uniqueness_constraint.rbs +54 -0
- data/sig/domainic/type/constraint/resolver.rbs +117 -0
- data/sig/domainic/type/constraint/set.rbs +159 -0
- data/sig/domainic/type/definitions.rbs +304 -0
- data/sig/domainic/type/types/core/array_type.rbs +42 -0
- data/sig/domainic/type/types/core/float_type.rbs +33 -0
- data/sig/domainic/type/types/core/hash_type.rbs +107 -0
- data/sig/domainic/type/types/core/integer_type.rbs +32 -0
- data/sig/domainic/type/types/core/string_type.rbs +45 -0
- data/sig/domainic/type/types/core/symbol_type.rbs +45 -0
- data/sig/domainic/type/types/specification/anything_type.rbs +14 -0
- data/sig/domainic/type/types/specification/duck_type.rbs +41 -0
- data/sig/domainic/type/types/specification/enum_type.rbs +14 -0
- data/sig/domainic/type/types/specification/union_type.rbs +14 -0
- data/sig/domainic/type/types/specification/void_type.rbs +8 -0
- data/sig/domainic/type.rbs +5 -0
- data/sig/domainic-type.rbs +1 -0
- data/sig/manifest.yaml +2 -0
- metadata +108 -71
@@ -0,0 +1,159 @@
|
|
1
|
+
module Domainic
|
2
|
+
module Type
|
3
|
+
module Constraint
|
4
|
+
# A class managing collections of type constraints.
|
5
|
+
#
|
6
|
+
# The Set class provides a structured way to manage multiple constraints,
|
7
|
+
# organizing them by their accessor method and constraint name. It handles
|
8
|
+
# the creation, storage, and retrieval of constraints while maintaining
|
9
|
+
# their relationships and configuration.
|
10
|
+
#
|
11
|
+
# Key features:
|
12
|
+
# - Organized storage by accessor and constraint name
|
13
|
+
# - Dynamic constraint resolution and creation
|
14
|
+
# - Flexible constraint lookup and enumeration
|
15
|
+
# - Support for both symbol and string identifiers
|
16
|
+
#
|
17
|
+
# @example Creating and managing constraints
|
18
|
+
# set = Set.new
|
19
|
+
# set.add(:self, :string, 'being a')
|
20
|
+
# set.add(:length, :range, 'having length', minimum: 5)
|
21
|
+
#
|
22
|
+
# set.constraint?(:self, 'being a') # => true
|
23
|
+
# set.constraints # => [StringConstraint, RangeConstraint]
|
24
|
+
# set.count # => 2
|
25
|
+
#
|
26
|
+
# @author {https://aaronmallen.me Aaron Allen}
|
27
|
+
# @since 0.1.0
|
28
|
+
class Set
|
29
|
+
extend Forwardable
|
30
|
+
|
31
|
+
@lookup: Hash[Type::accessor, Hash[Symbol, Behavior]]
|
32
|
+
|
33
|
+
# Initialize a new empty constraint set.
|
34
|
+
#
|
35
|
+
# @return [void]
|
36
|
+
def initialize: () -> void
|
37
|
+
|
38
|
+
# Add a new constraint to the set.
|
39
|
+
#
|
40
|
+
# Creates and configures a new constraint instance based on the provided type
|
41
|
+
# and configuration. If a constraint with the same accessor and name already
|
42
|
+
# exists, it will be replaced.
|
43
|
+
#
|
44
|
+
# @param accessor [String, Symbol] The accessor method for the constraint
|
45
|
+
# @param constraint_type [String, Symbol] The type of constraint to create
|
46
|
+
# @param expectation [Object, nil] The expected value for the constraint
|
47
|
+
# @param options [Hash{Symbol, String => Object}] Additional options for the constraint
|
48
|
+
#
|
49
|
+
# @option options [String, Symbol] concerning The subject of the constraint. This is used to namespace
|
50
|
+
# constraints that may have compatibility issues with other constraints (i,e, min/max size constraints).
|
51
|
+
# @option options [String, Symbol, nil] description The quantifier description of the constraint when given
|
52
|
+
# a string that ends with "not_described" it will not be included in the constraint set description.
|
53
|
+
# @option options [Boolean] :abort_on_failure Whether to stop on failure
|
54
|
+
# @option options [Array<Proc>, Proc] :coerce_with Coercers to run on the value before validating the
|
55
|
+
# constraint.
|
56
|
+
#
|
57
|
+
# @return [void]
|
58
|
+
def add: (Type::accessor accessor, String | Symbol constraint_type, ?untyped expectation, **untyped options) -> void
|
59
|
+
|
60
|
+
# Get all constraints in the set.
|
61
|
+
#
|
62
|
+
# @return [Array<Behavior>] Array of all constraints
|
63
|
+
def all: () -> Array[Behavior]
|
64
|
+
|
65
|
+
# @yield [Behavior] The constraint to check
|
66
|
+
# @return [Boolean] true if all constraints match.
|
67
|
+
def all?: () -> bool
|
68
|
+
| (Class | Module) -> bool
|
69
|
+
| () { (Behavior) -> boolish } -> bool
|
70
|
+
|
71
|
+
# Get the total number of constraints.
|
72
|
+
#
|
73
|
+
# @return [Integer] The number of constraints
|
74
|
+
def count: () -> Integer
|
75
|
+
|
76
|
+
alias length count
|
77
|
+
|
78
|
+
alias size count
|
79
|
+
|
80
|
+
# The aggregate description of all constraints in the set.
|
81
|
+
#
|
82
|
+
# @return [String] The description of all constraints
|
83
|
+
def description: () -> String
|
84
|
+
|
85
|
+
# @yield [Behavior] The constraint to process
|
86
|
+
# @return [void]
|
87
|
+
def each: () { (Behavior) -> void } -> void
|
88
|
+
|
89
|
+
# Check if a specific constraint exists.
|
90
|
+
#
|
91
|
+
# @param accessor [Symbol] The accessor method for the constraint
|
92
|
+
# @param concerning [String, Symbol] The subject of the constraint.
|
93
|
+
#
|
94
|
+
# @return [Boolean] true if the constraint exists
|
95
|
+
def exist?: (Type::accessor accessor, Symbol | String concerning) -> bool
|
96
|
+
|
97
|
+
alias has_constraint? exist?
|
98
|
+
|
99
|
+
# Whether any constraints in the set have failed satisfaction.
|
100
|
+
#
|
101
|
+
# @return [Boolean] true if any constraints have failed
|
102
|
+
def failures?: () -> bool
|
103
|
+
|
104
|
+
# @yield [Behavior] The constraint to process
|
105
|
+
# @return [Array] The results of the block.
|
106
|
+
def filter_map: () { (Behavior) -> untyped } -> Array[Behavior]
|
107
|
+
|
108
|
+
# Get a specific constraint by its accessor and name.
|
109
|
+
#
|
110
|
+
# @param accessor [Symbol] The accessor method for the constraint
|
111
|
+
# @param concerning [String, Symbol] The subject of the constraint.
|
112
|
+
#
|
113
|
+
# @return [Behavior, nil] The constraint if found, nil otherwise
|
114
|
+
def find: (Type::accessor accessor, String | Symbol concerning) -> Behavior?
|
115
|
+
|
116
|
+
# Prepare a new constraint.
|
117
|
+
#
|
118
|
+
# This is useful for preparing a constraint to use as a sub-constraint for a more complex constraint.
|
119
|
+
#
|
120
|
+
# @param accessor [String, Symbol] The accessor method for the constraint
|
121
|
+
# @param constraint_type [String, Symbol] The type of constraint to create
|
122
|
+
# @param expectation [Object, nil] The expected value for the constraint
|
123
|
+
# @param options [Hash{Symbol, String => Object}] Additional options for the constraint
|
124
|
+
#
|
125
|
+
# @option options [String, Symbol, nil] description The quantifier description of the constraint when given
|
126
|
+
# a string that ends with "not_described" it will not be included in the constraint set description.
|
127
|
+
# @option options [Boolean] :abort_on_failure Whether to stop on failure
|
128
|
+
# @option options [Array<Proc>, Proc] :coerce_with Coercers to run on the value before validating the
|
129
|
+
# constraint.
|
130
|
+
#
|
131
|
+
# @return [Behavior] The new constraint instance
|
132
|
+
def prepare: (Type::accessor accessor, String | Symbol constraint_type, ?untyped expectation, **untyped options) -> Behavior
|
133
|
+
|
134
|
+
# The aggregate violation description of all constraints in the set.
|
135
|
+
#
|
136
|
+
# @return [String] The description of all constraints
|
137
|
+
def violation_description: () -> String
|
138
|
+
|
139
|
+
private
|
140
|
+
|
141
|
+
# Build a new constraint instance for the given type.
|
142
|
+
#
|
143
|
+
# @param constraint_type [String, Symbol] The type of constraint to create
|
144
|
+
# @param accessor [String, Symbol] The accessor method for the constraint
|
145
|
+
# @param quantifier_description [String, Symbol] The quantifier description of the constraint
|
146
|
+
#
|
147
|
+
# @return [Behavior] The new constraint instance
|
148
|
+
def build_constraint: (String | Symbol constraint_type, Type::accessor accessor, (String | Symbol)?) -> Behavior
|
149
|
+
|
150
|
+
# Ensure that the lookup hash is deep copied when duplicating.
|
151
|
+
#
|
152
|
+
# @param source [Set] The source object to copy
|
153
|
+
#
|
154
|
+
# @return [void]
|
155
|
+
def initialize_copy: ...
|
156
|
+
end
|
157
|
+
end
|
158
|
+
end
|
159
|
+
end
|
@@ -0,0 +1,304 @@
|
|
1
|
+
module Domainic
|
2
|
+
module Type
|
3
|
+
# A module providing convenient factory methods for creating type instances.
|
4
|
+
#
|
5
|
+
# This module serves as a temporary access point for type creation in the Domainic::Type
|
6
|
+
# system, offering a collection of factory methods with consistent naming patterns.
|
7
|
+
# Each method creates and configures a specific type instance, with optional nilable
|
8
|
+
# variants and aliases for common use cases.
|
9
|
+
#
|
10
|
+
# @note This module is considered temporary and may be significantly altered or removed
|
11
|
+
# before the final release. It should not be considered part of the stable API.
|
12
|
+
#
|
13
|
+
# @example Basic type creation
|
14
|
+
# include Domainic::Type::Definitions
|
15
|
+
#
|
16
|
+
# string_type = _String()
|
17
|
+
# array_type = _Array()
|
18
|
+
# hash_type = _Hash()
|
19
|
+
#
|
20
|
+
# @example Creating nilable types
|
21
|
+
# nullable_string = _String?()
|
22
|
+
# nullable_array = _Array?()
|
23
|
+
#
|
24
|
+
# @example Using union types
|
25
|
+
# string_or_symbol = _Union(String, Symbol)
|
26
|
+
# boolean = _Boolean() # Union of TrueClass and FalseClass
|
27
|
+
#
|
28
|
+
# @author {https://aaronmallen.me Aaron Allen}
|
29
|
+
# @since 0.1.0
|
30
|
+
module Definitions
|
31
|
+
# Creates an AnythingType instance.
|
32
|
+
#
|
33
|
+
# @example
|
34
|
+
# type = _Anything
|
35
|
+
#
|
36
|
+
# @param options [Hash] additional configuration options
|
37
|
+
#
|
38
|
+
# @return [Domainic::Type::AnythingType] the created type
|
39
|
+
def _Anything: (**__todo__ options) -> AnythingType
|
40
|
+
|
41
|
+
alias _Any _Anything
|
42
|
+
|
43
|
+
# Creates an ArrayType instance.
|
44
|
+
#
|
45
|
+
# @example
|
46
|
+
# type = _Array
|
47
|
+
#
|
48
|
+
# @param options [Hash] additional configuration options
|
49
|
+
#
|
50
|
+
# @return [Domainic::Type::ArrayType] the created type
|
51
|
+
def _Array: (**__todo__ options) -> ArrayType
|
52
|
+
|
53
|
+
alias _List _Array
|
54
|
+
|
55
|
+
# Creates a nilable ArrayType instance.
|
56
|
+
#
|
57
|
+
# @example
|
58
|
+
# type = _Array?
|
59
|
+
#
|
60
|
+
# @param options [Hash] additional configuration options
|
61
|
+
#
|
62
|
+
# @return [Domainic::Type::UnionType] the created type (Array or NilClass)
|
63
|
+
def _Array?: (**__todo__ options) -> UnionType
|
64
|
+
|
65
|
+
alias _List? _Array?
|
66
|
+
|
67
|
+
# Creates a Boolean type.
|
68
|
+
#
|
69
|
+
# Represents a union of TrueClass and FalseClass.
|
70
|
+
#
|
71
|
+
# @example
|
72
|
+
# type = _Boolean
|
73
|
+
#
|
74
|
+
# @return [Domainic::Type::UnionType] the created type
|
75
|
+
def _Boolean: () -> UnionType
|
76
|
+
|
77
|
+
alias _Bool _Boolean
|
78
|
+
|
79
|
+
# Creates a nilable Boolean type.
|
80
|
+
#
|
81
|
+
# @example
|
82
|
+
# type = _Boolean?
|
83
|
+
#
|
84
|
+
# @return [Domainic::Type::UnionType] the created type (TrueClass, FalseClass, or NilClass)
|
85
|
+
def _Boolean?: () -> UnionType
|
86
|
+
|
87
|
+
alias _Bool? _Boolean?
|
88
|
+
|
89
|
+
# Creates a DuckType instance.
|
90
|
+
#
|
91
|
+
# DuckType allows specifying behavior based on method availability.
|
92
|
+
#
|
93
|
+
# @example
|
94
|
+
# type = _Duck(respond_to: :to_s)
|
95
|
+
#
|
96
|
+
# @param options [Hash] additional configuration options
|
97
|
+
#
|
98
|
+
# @return [Domainic::Type::DuckType] the created type
|
99
|
+
def _Duck: (**__todo__ options) -> DuckType
|
100
|
+
|
101
|
+
alias _Interface _Duck
|
102
|
+
|
103
|
+
alias _Protocol _Duck
|
104
|
+
|
105
|
+
alias _RespondingTo _Duck
|
106
|
+
|
107
|
+
# Creates an EnumType instance.
|
108
|
+
#
|
109
|
+
# EnumType restricts values to a specific set of literals.
|
110
|
+
#
|
111
|
+
# @example
|
112
|
+
# type = _Enum(:red, :green, :blue)
|
113
|
+
#
|
114
|
+
# @param literals [Array<Object>] the allowed literals
|
115
|
+
# @param options [Hash] additional configuration options
|
116
|
+
#
|
117
|
+
# @return [Domainic::Type::EnumType] the created type
|
118
|
+
def _Enum: (*untyped literals, **__todo__ options) -> EnumType
|
119
|
+
|
120
|
+
alias _Literal _Enum
|
121
|
+
|
122
|
+
# Creates a nilable EnumType instance.
|
123
|
+
#
|
124
|
+
# @example
|
125
|
+
# type = _Enum?(:red, :green, :blue)
|
126
|
+
#
|
127
|
+
# @param literals [Array<Object>] the allowed literals
|
128
|
+
# @param options [Hash] additional configuration options
|
129
|
+
#
|
130
|
+
# @return [Domainic::Type::UnionType] the created type (Enum or NilClass)
|
131
|
+
def _Enum?: (*untyped literals, **__todo__ options) -> UnionType
|
132
|
+
|
133
|
+
alias _Literal? _Enum?
|
134
|
+
|
135
|
+
# Creates a FloatType instance.
|
136
|
+
#
|
137
|
+
# @example
|
138
|
+
# type = _Float
|
139
|
+
#
|
140
|
+
# @param options [Hash] additional configuration options
|
141
|
+
#
|
142
|
+
# @return [Domainic::Type::FloatType] the created type
|
143
|
+
def _Float: (**__todo__ options) -> FloatType
|
144
|
+
|
145
|
+
alias _Decimal _Float
|
146
|
+
|
147
|
+
alias _Real _Float
|
148
|
+
|
149
|
+
# Creates a nilable FloatType instance.
|
150
|
+
#
|
151
|
+
# @example
|
152
|
+
# type = _Float?
|
153
|
+
#
|
154
|
+
# @param options [Hash] additional configuration options
|
155
|
+
#
|
156
|
+
# @return [Domainic::Type::UnionType] the created type (Float or NilClass)
|
157
|
+
def _Float?: (**__todo__ options) -> UnionType
|
158
|
+
|
159
|
+
alias _Decimal? _Float?
|
160
|
+
|
161
|
+
alias _Real? _Float?
|
162
|
+
|
163
|
+
# Creates a HashType instance.
|
164
|
+
#
|
165
|
+
# @example
|
166
|
+
# type = _Hash
|
167
|
+
#
|
168
|
+
# @param options [Hash] additional configuration options
|
169
|
+
#
|
170
|
+
# @return [Domainic::Type::HashType] the created type
|
171
|
+
def _Hash: (**__todo__ options) -> HashType
|
172
|
+
|
173
|
+
alias _Map _Hash
|
174
|
+
|
175
|
+
# Creates a nilable HashType instance.
|
176
|
+
#
|
177
|
+
# @example
|
178
|
+
# type = _Hash?
|
179
|
+
#
|
180
|
+
# @param options [Hash] additional configuration options
|
181
|
+
#
|
182
|
+
# @return [Domainic::Type::UnionType] the created type (Hash or NilClass)
|
183
|
+
def _Hash?: (**__todo__ options) -> UnionType
|
184
|
+
|
185
|
+
alias _Map? _Hash?
|
186
|
+
|
187
|
+
# Creates an IntegerType instance.
|
188
|
+
#
|
189
|
+
# @example
|
190
|
+
# type = _Integer
|
191
|
+
#
|
192
|
+
# @param options [Hash] additional configuration options
|
193
|
+
#
|
194
|
+
# @return [Domainic::Type::IntegerType] the created type
|
195
|
+
def _Integer: (**__todo__ options) -> IntegerType
|
196
|
+
|
197
|
+
alias _Int _Integer
|
198
|
+
|
199
|
+
alias _Number _Integer
|
200
|
+
|
201
|
+
# Creates a nilable IntegerType instance.
|
202
|
+
#
|
203
|
+
# @example
|
204
|
+
# type = _Integer?
|
205
|
+
#
|
206
|
+
# @param options [Hash] additional configuration options
|
207
|
+
#
|
208
|
+
# @return [Domainic::Type::UnionType] the created type (Integer or NilClass)
|
209
|
+
def _Integer?: (**__todo__ options) -> UnionType
|
210
|
+
|
211
|
+
alias _Int? _Integer?
|
212
|
+
|
213
|
+
alias _Number? _Integer?
|
214
|
+
|
215
|
+
# Creates a Nilable (nullable) type.
|
216
|
+
#
|
217
|
+
# Combines one or more types with `NilClass` to allow nil values.
|
218
|
+
#
|
219
|
+
# @example
|
220
|
+
# type = _Nilable(String, Symbol)
|
221
|
+
#
|
222
|
+
# @param types [Array<Class, Module, Behavior>] the base types
|
223
|
+
# @param options [Hash] additional configuration options
|
224
|
+
#
|
225
|
+
# @return [Domainic::Type::UnionType] the created type (NilClass or other specified types)
|
226
|
+
def _Nilable: (*Class | Module | Behavior[untyped, untyped, untyped] types, **__todo__ options) -> UnionType
|
227
|
+
|
228
|
+
alias _Nullable _Nilable
|
229
|
+
|
230
|
+
# Creates a StringType instance.
|
231
|
+
#
|
232
|
+
# @example
|
233
|
+
# type = _String
|
234
|
+
#
|
235
|
+
# @param options [Hash] additional configuration options
|
236
|
+
#
|
237
|
+
# @return [Domainic::Type::StringType] the created type
|
238
|
+
def _String: (**__todo__ options) -> StringType
|
239
|
+
|
240
|
+
alias _Text _String
|
241
|
+
|
242
|
+
# Creates a nilable StringType instance.
|
243
|
+
#
|
244
|
+
# @example
|
245
|
+
# type = _String?
|
246
|
+
#
|
247
|
+
# @param options [Hash] additional configuration options
|
248
|
+
#
|
249
|
+
# @return [Domainic::Type::UnionType] the created type (String or NilClass)
|
250
|
+
def _String?: (**__todo__ options) -> UnionType
|
251
|
+
|
252
|
+
alias _Text? _String?
|
253
|
+
|
254
|
+
# Creates a SymbolType instance.
|
255
|
+
#
|
256
|
+
# @example
|
257
|
+
# type = _Symbol
|
258
|
+
#
|
259
|
+
# @param options [Hash] additional configuration options
|
260
|
+
#
|
261
|
+
# @return [Domainic::Type::SymbolType] the created type
|
262
|
+
def _Symbol: (**__todo__ options) -> SymbolType
|
263
|
+
|
264
|
+
alias _Interned _Symbol
|
265
|
+
|
266
|
+
# Creates a nilable SymbolType instance.
|
267
|
+
#
|
268
|
+
# @example
|
269
|
+
# type = _Symbol?
|
270
|
+
#
|
271
|
+
# @param options [Hash] additional configuration options
|
272
|
+
#
|
273
|
+
# @return [Domainic::Type::UnionType] the created type (Symbol or NilClass)
|
274
|
+
def _Symbol?: (**__todo__ options) -> UnionType
|
275
|
+
|
276
|
+
alias _Interned? _Symbol?
|
277
|
+
|
278
|
+
# Creates a UnionType instance.
|
279
|
+
#
|
280
|
+
# Allows combining multiple types into a single union type.
|
281
|
+
#
|
282
|
+
# @example
|
283
|
+
# type = _Union(String, Symbol)
|
284
|
+
#
|
285
|
+
# @param types [Array<Class, Module, Behavior>] the types included in the union
|
286
|
+
# @param options [Hash] additional configuration options
|
287
|
+
#
|
288
|
+
# @return [Domainic::Type::UnionType] the created type
|
289
|
+
def _Union: (*Class | Module | Behavior[untyped, untyped, untyped] types, **__todo__ options) -> UnionType
|
290
|
+
|
291
|
+
alias _Either _Union
|
292
|
+
|
293
|
+
# Creates a VoidType instance.
|
294
|
+
#
|
295
|
+
# Represents an operation that returns no value.
|
296
|
+
#
|
297
|
+
# @example
|
298
|
+
# type = _Void
|
299
|
+
#
|
300
|
+
# @return [Domainic::Type::VoidType] the created type
|
301
|
+
def _Void: () -> VoidType
|
302
|
+
end
|
303
|
+
end
|
304
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module Domainic
|
2
|
+
module Type
|
3
|
+
# A type for validating Array objects with flexible element and size constraints
|
4
|
+
#
|
5
|
+
# This class provides a comprehensive set of validations specifically designed for Array
|
6
|
+
# objects, including element type checking, value inclusion/exclusion, ordering, and all
|
7
|
+
# standard enumerable validations.
|
8
|
+
#
|
9
|
+
# Key features:
|
10
|
+
# - Element type validation
|
11
|
+
# - Element presence/absence validation
|
12
|
+
# - Uniqueness constraints
|
13
|
+
# - Order constraints
|
14
|
+
# - Size constraints (via EnumerableBehavior)
|
15
|
+
# - Standard collection validations (via EnumerableBehavior)
|
16
|
+
#
|
17
|
+
# @example Basic usage
|
18
|
+
# type = ArrayType.new
|
19
|
+
# type.of(String) # enforce element type
|
20
|
+
# type.containing(1, 2) # require specific elements
|
21
|
+
# type.having_size(3) # exact size constraint
|
22
|
+
# type.being_distinct # unique elements
|
23
|
+
#
|
24
|
+
# @example Complex constraints
|
25
|
+
# type = ArrayType.new
|
26
|
+
# type
|
27
|
+
# .of(Integer) # type constraints
|
28
|
+
# .being_ordered # must be sorted
|
29
|
+
# .having_size_between(2, 5) # size range
|
30
|
+
# .starting_with(1) # first element
|
31
|
+
#
|
32
|
+
# @author {https://aaronmallen.me Aaron Allen}
|
33
|
+
# @since 0.1.0
|
34
|
+
class ArrayType
|
35
|
+
extend Behavior::ClassMethods
|
36
|
+
|
37
|
+
include Behavior
|
38
|
+
|
39
|
+
include Behavior::EnumerableBehavior
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module Domainic
|
2
|
+
module Type
|
3
|
+
# A type for validating Float objects with extensive numeric validation capabilities.
|
4
|
+
#
|
5
|
+
# This class provides a comprehensive set of validations specifically designed for
|
6
|
+
# floating-point values, including basic type checking and all numeric validations
|
7
|
+
# like finiteness, polarity, and range constraints. It properly handles floating-point
|
8
|
+
# arithmetic by using appropriate tolerances in comparisons.
|
9
|
+
#
|
10
|
+
# @example Basic usage
|
11
|
+
# type = FloatType.new
|
12
|
+
# type.validate(3.14) # => true
|
13
|
+
# type.validate(42) # => false
|
14
|
+
# type.validate("3.14") # => false
|
15
|
+
#
|
16
|
+
# @example With numeric constraints
|
17
|
+
# type = FloatType.new
|
18
|
+
# type
|
19
|
+
# .being_positive
|
20
|
+
# .being_finite
|
21
|
+
# .being_less_than(10.0)
|
22
|
+
#
|
23
|
+
# @author {https://aaronmallen.me Aaron Allen}
|
24
|
+
# @since 0.1.0
|
25
|
+
class FloatType
|
26
|
+
extend Behavior::ClassMethods
|
27
|
+
|
28
|
+
include Behavior
|
29
|
+
|
30
|
+
include Behavior::NumericBehavior
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,107 @@
|
|
1
|
+
module Domainic
|
2
|
+
module Type
|
3
|
+
# A type for validating Hash objects with flexible key and value constraints
|
4
|
+
#
|
5
|
+
# This class provides a comprehensive set of validations specifically designed
|
6
|
+
# for Hash objects, including key/value type checking, inclusion/exclusion of
|
7
|
+
# specific keys or values, and all standard enumerable validations.
|
8
|
+
#
|
9
|
+
# Key features:
|
10
|
+
# - Key type validation
|
11
|
+
# - Value type validation
|
12
|
+
# - Key presence/absence checking
|
13
|
+
# - Value presence/absence checking
|
14
|
+
# - Size constraints (via EnumerableBehavior)
|
15
|
+
# - Standard collection validations (via EnumerableBehavior)
|
16
|
+
#
|
17
|
+
# @example Basic usage
|
18
|
+
# type = HashType.new
|
19
|
+
# type.of(String => Integer) # enforce key/value types
|
20
|
+
# type.containing_keys('a', 'b') # require specific keys
|
21
|
+
# type.containing_values(1, 2) # require specific values
|
22
|
+
# type.having_size(2) # exact size constraint
|
23
|
+
#
|
24
|
+
# @example Complex constraints
|
25
|
+
# type = HashType.new
|
26
|
+
# type
|
27
|
+
# .of(Symbol => String) # type constraints
|
28
|
+
# .containing_keys(:name, :email) # required keys
|
29
|
+
# .excluding_values(nil, '') # no empty values
|
30
|
+
# .having_size_between(2, 5) # size range
|
31
|
+
#
|
32
|
+
# @author {https://aaronmallen.me Aaron Allen}
|
33
|
+
# @since 0.1.0
|
34
|
+
class HashType
|
35
|
+
extend Behavior::ClassMethods
|
36
|
+
|
37
|
+
include Behavior
|
38
|
+
|
39
|
+
include Behavior::EnumerableBehavior
|
40
|
+
|
41
|
+
# Validate that the hash contains specific keys
|
42
|
+
#
|
43
|
+
# @example
|
44
|
+
# type.containing_keys(:name, :email)
|
45
|
+
# type.validate({ name: 'John', email: 'john@example.com' }) # => true
|
46
|
+
# type.validate({ name: 'John' }) # => false
|
47
|
+
#
|
48
|
+
# @param keys [Array<Object>] the keys that must be present
|
49
|
+
# @return [self] self for method chaining
|
50
|
+
def containing_keys: (*untyped keys) -> self
|
51
|
+
|
52
|
+
alias including_keys containing_keys
|
53
|
+
|
54
|
+
# Validate that the hash contains specific values
|
55
|
+
#
|
56
|
+
# @example
|
57
|
+
# type.containing_values('active', 'pending')
|
58
|
+
# type.validate({ status: 'active' }) # => true
|
59
|
+
# type.validate({ status: 'inactive' }) # => false
|
60
|
+
#
|
61
|
+
# @param values [Array<Object>] the values that must be present
|
62
|
+
# @return [self] self for method chaining
|
63
|
+
def containing_values: (*untyped values) -> self
|
64
|
+
|
65
|
+
alias including_values containing_values
|
66
|
+
|
67
|
+
# Validate that the hash does not contain specific keys
|
68
|
+
#
|
69
|
+
# @example
|
70
|
+
# type.excluding_keys(:admin, :superuser)
|
71
|
+
# type.validate({ user: 'John' }) # => true
|
72
|
+
# type.validate({ admin: true }) # => false
|
73
|
+
#
|
74
|
+
# @param keys [Array<Object>] the keys that must not be present
|
75
|
+
# @return [self] self for method chaining
|
76
|
+
def excluding_keys: (*untyped keys) -> self
|
77
|
+
|
78
|
+
alias omitting_keys excluding_keys
|
79
|
+
|
80
|
+
# Validate that the hash does not contain specific values
|
81
|
+
#
|
82
|
+
# @example
|
83
|
+
# type.excluding_values(nil, '')
|
84
|
+
# type.validate({ name: 'John' }) # => true
|
85
|
+
# type.validate({ name: nil }) # => false
|
86
|
+
#
|
87
|
+
# @param values [Array<Object>] the values that must not be present
|
88
|
+
# @return [self] self for method chaining
|
89
|
+
def excluding_values: (*untyped values) -> self
|
90
|
+
|
91
|
+
alias omitting_values excluding_values
|
92
|
+
|
93
|
+
# Validate the hash to have the given key type and value type.
|
94
|
+
#
|
95
|
+
# @example
|
96
|
+
# type.of(String => Integer)
|
97
|
+
# type.validate({ 'a' => 1 }) # => true
|
98
|
+
# type.validate({ 1 => 'a' }) # => false
|
99
|
+
#
|
100
|
+
# @param key_to_value_type [Hash{Object => Object}] the key type and value type that the hash must have
|
101
|
+
#
|
102
|
+
# @raise [ArgumentError] if the key_to_value_type pair is not a Hash or does not have exactly one entry
|
103
|
+
# @return [self] self for method chaining
|
104
|
+
def of: (Hash[untyped, untyped] key_to_value_type) -> self
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module Domainic
|
2
|
+
module Type
|
3
|
+
# A type for validating Integer objects with extensive numeric validation capabilities.
|
4
|
+
#
|
5
|
+
# This class provides a comprehensive set of validations specifically designed for
|
6
|
+
# integer values, including basic type checking and all numeric validations like
|
7
|
+
# divisibility, parity, polarity, and range constraints.
|
8
|
+
#
|
9
|
+
# @example Basic usage
|
10
|
+
# type = IntegerType.new
|
11
|
+
# type.validate(42) # => true
|
12
|
+
# type.validate(3.14) # => false
|
13
|
+
# type.validate("42") # => false
|
14
|
+
#
|
15
|
+
# @example With numeric constraints
|
16
|
+
# type = IntegerType.new
|
17
|
+
# type
|
18
|
+
# .being_positive
|
19
|
+
# .being_even
|
20
|
+
# .being_less_than(100)
|
21
|
+
#
|
22
|
+
# @author {https://aaronmallen.me Aaron Allen}
|
23
|
+
# @since 0.1.0
|
24
|
+
class IntegerType
|
25
|
+
extend Behavior::ClassMethods
|
26
|
+
|
27
|
+
include Behavior
|
28
|
+
|
29
|
+
include Behavior::NumericBehavior
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|