Class: Yarrow::Schema::Types::TypeClass
- Inherits:
-
Object
- Object
- Yarrow::Schema::Types::TypeClass
show all
- Defined in:
- lib/yarrow/schema/types.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(unit_type = nil) ⇒ TypeClass
33
34
35
36
|
# File 'lib/yarrow/schema/types.rb', line 33
def initialize(unit_type=nil)
@unit = unit_type
@accepts = {}
end
|
Instance Attribute Details
#accepts ⇒ Object
Returns the value of attribute accepts.
31
32
33
|
# File 'lib/yarrow/schema/types.rb', line 31
def accepts
@accepts
end
|
#unit ⇒ Object
Returns the value of attribute unit.
31
32
33
|
# File 'lib/yarrow/schema/types.rb', line 31
def unit
@unit
end
|
Class Method Details
.of(unit_type) ⇒ Object
27
28
29
|
# File 'lib/yarrow/schema/types.rb', line 27
def self.of(unit_type)
new(unit_type)
end
|
Instance Method Details
#accept(type, constructor = :new, options = nil) ⇒ Object
42
43
44
45
46
47
48
49
50
|
# File 'lib/yarrow/schema/types.rb', line 42
def accept(type, constructor=:new, options=nil)
accepts[type] = if options.nil?
[constructor]
else
[constructor, options]
end
self
end
|
#cast(input) ⇒ Object
91
92
93
94
|
# File 'lib/yarrow/schema/types.rb', line 91
def cast(input)
return coerce(input) if should_coerce?(input)
check(input)
end
|
#check_instance_of!(input) ⇒ Object
67
68
69
70
71
|
# File 'lib/yarrow/schema/types.rb', line 67
def check_instance_of!(input)
unless input.instance_of?(unit)
raise CastError.instance_of(input.class, unit)
end
end
|
#check_kind_of!(input) ⇒ Object
73
74
75
76
77
|
# File 'lib/yarrow/schema/types.rb', line 73
def check_kind_of!(input)
unless input.kind_of?(unit)
raise CastError.kind_of(input.class, unit)
end
end
|
#check_respond_to_all!(input, methods) ⇒ Object
85
86
87
88
89
|
# File 'lib/yarrow/schema/types.rb', line 85
def check_respond_to_all!(input, methods)
unless methods.all? { |m| input.respond_to?(m) }
raise CastError.respond_to_all(input.class, methods)
end
end
|
#check_respond_to_any!(input, methods) ⇒ Object
79
80
81
82
83
|
# File 'lib/yarrow/schema/types.rb', line 79
def check_respond_to_any!(input, methods)
unless methods.any? { |m| input.respond_to?(m) }
raise CastError.respond_to_any(input.class, methods)
end
end
|
#coerce(input) ⇒ Object
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/yarrow/schema/types.rb', line 56
def coerce(input)
constructor, options = accepts[input.class]
if options.nil?
unit.send(constructor, input)
else
unit.send(constructor, input, options.clone)
end
end
|
#should_coerce?(input) ⇒ Boolean
52
53
54
|
# File 'lib/yarrow/schema/types.rb', line 52
def should_coerce?(input)
accepts.key?(input.class)
end
|
#|(rhs_opt) ⇒ Object
38
39
40
|
# File 'lib/yarrow/schema/types.rb', line 38
def |(rhs_opt)
Union.new(self, rhs_opt)
end
|