Module: Literal::Types

Includes:
Monads
Included in:
Literal, Attributable, Class
Defined in:
lib/literal/types.rb

Defined Under Namespace

Classes: DescendantType

Constant Summary

Constants included from Monads

Monads::Either, Monads::Left, Monads::Maybe, Monads::Nothing, Monads::Result, Monads::Right, Monads::Some

Instance Method Summary collapse

Methods included from Monads

#Either, #Left, #Maybe, #Result, #Right, #Some, #Success

Instance Method Details

#_AnyObject

Matches any value except nil. Use _Nilable(_Any) to match any value including nil.



60
61
62
# File 'lib/literal/types.rb', line 60

def _Any
	Literal::Types::AnyType
end

#_Array(type) ⇒ Object

Matches if the value is an Array and all the elements match the given type.



28
29
30
# File 'lib/literal/types.rb', line 28

def _Array(type)
	Literal::Types::ArrayType.new(type)
end

#_BooleanObject

Matches if the value is true or false.



65
66
67
# File 'lib/literal/types.rb', line 65

def _Boolean
	Literal::Types::BooleanType
end

#_Callable(type = nil) ⇒ Object

Matches if the value responds to #call.



103
104
105
# File 'lib/literal/types.rb', line 103

def _Callable(type = nil)
	Literal::Types::CallableType
end

#_Class(type) ⇒ Object

Matches if the value either the given class or a subclass of it.



70
71
72
# File 'lib/literal/types.rb', line 70

def _Class(type)
	Literal::Types::ClassType.new(type)
end

#_Constraint(*constraints, **attributes) ⇒ Object

Similar to _Intersection, but allows you to specify attribute constraints as keyword arguments.

Examples:

_Constraint(Array, size: 1..3)


162
163
164
# File 'lib/literal/types.rb', line 162

def _Constraint(*constraints, **attributes)
	Literal::Types::ConstraintType.new(*constraints, **attributes)
end

#_Descendant(type) ⇒ Object



178
179
180
# File 'lib/literal/types.rb', line 178

def _Descendant(type)
	Literal::Types::DescendantType.new(type)
end

#_Enumerable(type) ⇒ Object

 Matches if the value is an Enumerable and all the elements match the given type.



38
39
40
# File 'lib/literal/types.rb', line 38

def _Enumerable(type)
	Literal::Types::EnumerableType.new(type)
end

#_FalsyObject

Matches "falsy" values (nil and false).



140
141
142
# File 'lib/literal/types.rb', line 140

def _Falsy
	Literal::Types::FalsyType
end

#_Float(constraint) ⇒ Object

Matches if the value is a Float and matches the given constraint. You could use a Range, for example, as a constraint. If you don't need a constraint, use Float instead of _Float.



93
94
95
# File 'lib/literal/types.rb', line 93

def _Float(constraint)
	Literal::Types::FloatType.new(constraint)
end

#_Frozen(type) ⇒ Object

Matches if the value is frozen.



155
156
157
# File 'lib/literal/types.rb', line 155

def _Frozen(type)
	Literal::Types::FrozenType.new(type)
end

#_Hash(key_type, value_type) ⇒ Object

Matches if the value is a Hash and all the keys and values match the given types.



43
44
45
# File 'lib/literal/types.rb', line 43

def _Hash(key_type, value_type)
	Literal::Types::HashType.new(key_type, value_type)
end

#_Integer(constraint) ⇒ Object

Matches if the value is an Integer and matches the given constraint. You could use a Range, for example, as a constraint. If you don't need a constraint, use Integer instead of _Integer.

Examples:

attribute :age, _Integer(18..127)


86
87
88
# File 'lib/literal/types.rb', line 86

def _Integer(constraint)
	Literal::Types::IntegerType.new(constraint)
end

#_Interface(*methods) ⇒ Object

Matches if the value responds to all the given methods.



48
49
50
51
52
# File 'lib/literal/types.rb', line 48

def _Interface(*methods)
	raise Literal::ArgumentError, "_Interface type must have at least one method." if methods.size < 1

	Literal::Types::InterfaceType.new(*methods)
end

#_Intersection(*types) ⇒ Object

Matches if all given types are matched.



14
15
16
17
18
# File 'lib/literal/types.rb', line 14

def _Intersection(*types)
	raise Literal::ArgumentError, "_Intersection type must have at least one type." if types.size < 1

	Literal::Types::IntersectionType.new(*types)
end

#_Is(*predicates) ⇒ Object

Matches if the given predicates are truthy.



21
22
23
24
25
# File 'lib/literal/types.rb', line 21

def _Is(*predicates)
	raise Literal::ArgumentError, "_Is type must have at least one predicate." if predicates.size < 1

	Literal::Types::IsType.new(*predicates)
end

#_JSONDataObject

Ensures the value is valid JSON data (i.e. it came from JSON.parse).



150
151
152
# File 'lib/literal/types.rb', line 150

def _JSONData
	Literal::Types::JSONDataType
end

#_LambdaObject

Matches if the value is a Proc and #lambda? returns truthy.



113
114
115
# File 'lib/literal/types.rb', line 113

def _Lambda
	Literal::Types::LambdaType
end

#_Map(**shape) ⇒ Object



170
171
172
# File 'lib/literal/types.rb', line 170

def _Map(**shape)
	Literal::Types::MapType
end

#_NeverObject



174
175
176
# File 'lib/literal/types.rb', line 174

def _Never
	Literal::Types::NeverType
end

#_Nilable(type) ⇒ Object

Matches if the value is either nil or the given type.



55
56
57
# File 'lib/literal/types.rb', line 55

def _Nilable(type)
	Literal::Types::NilableType.new(type)
end

#_Not(type) ⇒ Object

Matches if the given type is not matched.



130
131
132
# File 'lib/literal/types.rb', line 130

def _Not(type)
	Literal::Types::NotType.new(type)
end

#_ProcableObject

Matches if the value is a Proc or responds to #to_proc.



108
109
110
# File 'lib/literal/types.rb', line 108

def _Procable
	Literal::Types::ProcableType
end

#_Range(type) ⇒ Object

Matches if the value is a Range of the given type.



98
99
100
# File 'lib/literal/types.rb', line 98

def _Range(type)
	Literal::Types::RangeType.new(type)
end

#_Set(type) ⇒ Object

Matches if the value is a Set and all the elements match the given type.



33
34
35
# File 'lib/literal/types.rb', line 33

def _Set(type)
	Literal::Types::SetType.new(type)
end

#_Shape(*constraints, **shape) ⇒ Object

Ensures a value matches the given shape of a Hash



145
146
147
# File 'lib/literal/types.rb', line 145

def _Shape(*constraints, **shape)
	Literal::Types::ShapeType.new(*constraints, **shape)
end

#_String(constraint) ⇒ Object

Matches if the value is a String and matches the given constraint. You could use a Regexp, for example, as a constraint. If you don't need a constraint, use String instead of _String.



120
121
122
# File 'lib/literal/types.rb', line 120

def _String(constraint)
	Literal::Types::StringType.new(constraint)
end

#_Symbol(constraint) ⇒ Object

Matches if the value is a Symbol and matches the given constraint.



125
126
127
# File 'lib/literal/types.rb', line 125

def _Symbol(constraint)
	Literal::Types::SymbolType.new(constraint)
end

#_TruthyObject

Matches "truthy" values (anything except nil and false).



135
136
137
# File 'lib/literal/types.rb', line 135

def _Truthy
	Literal::Types::TruthyType
end

#_Tuple(*types) ⇒ Object

Matches if the value is an Array and each element matches the given types in order.



75
76
77
78
79
# File 'lib/literal/types.rb', line 75

def _Tuple(*types)
	raise Literal::ArgumentError, "_Tuple type must have at least one type." if types.size < 1

	Literal::Types::TupleType.new(*types)
end

#_Union(*types) ⇒ Object

Matches if any given type is matched.



7
8
9
10
11
# File 'lib/literal/types.rb', line 7

def _Union(*types)
	raise Literal::ArgumentError, "_Union type must have at least one type." if types.size < 1

	Literal::Union.new(*types)
end

#_VoidObject



166
167
168
# File 'lib/literal/types.rb', line 166

def _Void
	Literal::Types::VoidType
end