Class: Dry::Types::Definition
- Inherits:
-
Object
- Object
- Dry::Types::Definition
show all
- Includes:
- Builder, Options
- Defined in:
- lib/dry/types/definition.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Builder
#constrained, #constrained_type, #constructor, #default, #enum, #maybe, #optional, #safe, #|
Methods included from Options
#meta, #with
Constructor Details
#initialize(primitive, options = {}) ⇒ Definition
Returns a new instance of Definition.
32
33
34
35
36
|
# File 'lib/dry/types/definition.rb', line 32
def initialize(primitive, options = {})
super
@primitive = primitive
freeze
end
|
Instance Attribute Details
#options ⇒ Hash
13
14
15
|
# File 'lib/dry/types/definition.rb', line 13
def options
@options
end
|
#primitive ⇒ Class
16
17
18
|
# File 'lib/dry/types/definition.rb', line 16
def primitive
@primitive
end
|
Class Method Details
20
21
22
23
24
25
26
27
28
|
# File 'lib/dry/types/definition.rb', line 20
def self.[](primitive)
if primitive == ::Array
Types::Array
elsif primitive == ::Hash
Types::Hash
else
self
end
end
|
Instance Method Details
#call(input) ⇒ Object
Also known as:
[]
55
56
57
|
# File 'lib/dry/types/definition.rb', line 55
def call(input)
input
end
|
#constrained? ⇒ false
49
50
51
|
# File 'lib/dry/types/definition.rb', line 49
def constrained?
false
end
|
#default? ⇒ false
44
45
46
|
# File 'lib/dry/types/definition.rb', line 44
def default?
false
end
|
#failure(input, error) ⇒ Failure
83
84
85
|
# File 'lib/dry/types/definition.rb', line 83
def failure(input, error)
Result::Failure.new(input, error)
end
|
#name ⇒ String
39
40
41
|
# File 'lib/dry/types/definition.rb', line 39
def name
primitive.name
end
|
#primitive?(value) ⇒ Boolean
Also known as:
valid?
Checks whether value is of a #primitive class
97
98
99
|
# File 'lib/dry/types/definition.rb', line 97
def primitive?(value)
value.is_a?(primitive)
end
|
#result(klass, *args) ⇒ Object
Returns new instance of the given +klass+.
90
91
92
|
# File 'lib/dry/types/definition.rb', line 90
def result(klass, *args)
klass.new(*args)
end
|
#success(input) ⇒ Success
76
77
78
|
# File 'lib/dry/types/definition.rb', line 76
def success(input)
Result::Success.new(input)
end
|
#try(input, &block) {|failure| ... } ⇒ Result
65
66
67
68
69
70
71
72
|
# File 'lib/dry/types/definition.rb', line 65
def try(input, &block)
if valid?(input)
success(input)
else
failure = failure(input, "#{input.inspect} must be an instance of #{primitive}")
block ? yield(failure) : failure
end
end
|