Class: Dry::Types::Definition
Instance Attribute Summary collapse
Attributes included from Options
#options
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, #pristine, #with
Constructor Details
#initialize(primitive, options = {}) ⇒ Definition
Returns a new instance of Definition.
30
31
32
33
34
|
# File 'lib/dry/types/definition.rb', line 30
def initialize(primitive, options = {})
super
@primitive = primitive
freeze
end
|
Instance Attribute Details
#primitive ⇒ Class
14
15
16
|
# File 'lib/dry/types/definition.rb', line 14
def primitive
@primitive
end
|
Class Method Details
.[](primitive) ⇒ Type
18
19
20
21
22
23
24
25
26
|
# File 'lib/dry/types/definition.rb', line 18
def self.[](primitive)
if primitive == ::Array
Types::Array
elsif primitive == ::Hash
Types::Hash
else
self
end
end
|
Instance Method Details
#call(input) ⇒ BasicObject
Also known as:
[]
58
59
60
|
# File 'lib/dry/types/definition.rb', line 58
def call(input)
input
end
|
#constrained? ⇒ false
47
48
49
|
# File 'lib/dry/types/definition.rb', line 47
def constrained?
false
end
|
#default? ⇒ false
42
43
44
|
# File 'lib/dry/types/definition.rb', line 42
def default?
false
end
|
86
87
88
|
# File 'lib/dry/types/definition.rb', line 86
def failure(input, error)
Result::Failure.new(input, error)
end
|
#name ⇒ String
37
38
39
|
# File 'lib/dry/types/definition.rb', line 37
def name
primitive.name
end
|
#optional? ⇒ false
52
53
54
|
# File 'lib/dry/types/definition.rb', line 52
def optional?
false
end
|
#primitive?(value) ⇒ Boolean
Also known as:
valid?, ===
Checks whether value is of a #primitive class
93
94
95
|
# File 'lib/dry/types/definition.rb', line 93
def primitive?(value)
value.is_a?(primitive)
end
|
80
81
82
|
# File 'lib/dry/types/definition.rb', line 80
def success(input)
Result::Success.new(input)
end
|
#to_ast(meta: true) ⇒ Array
Return AST representation of a type definition
104
105
106
|
# File 'lib/dry/types/definition.rb', line 104
def to_ast(meta: true)
[:definition, [primitive, meta ? self.meta : EMPTY_HASH]]
end
|
#try(input, &block) {|failure| ... } ⇒ Result, ...
69
70
71
72
73
74
75
76
|
# File 'lib/dry/types/definition.rb', line 69
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
|