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
Returns a new instance of 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, context = nil) ⇒ Object
114
115
116
117
|
# File 'lib/yarrow/schema/types.rb', line 114
def cast(input, context=nil)
return coerce(input, context) if should_coerce?(input)
check(input)
end
|
#check_instance_of!(input) ⇒ Object
90
91
92
93
94
|
# File 'lib/yarrow/schema/types.rb', line 90
def check_instance_of!(input)
unless input.instance_of?(unit)
raise CastError.instance_of(input.class, unit)
end
end
|
#check_kind_of!(input) ⇒ Object
96
97
98
99
100
|
# File 'lib/yarrow/schema/types.rb', line 96
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
108
109
110
111
112
|
# File 'lib/yarrow/schema/types.rb', line 108
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
102
103
104
105
106
|
# File 'lib/yarrow/schema/types.rb', line 102
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, context = nil) ⇒ Object
Coerce input into the defined format based on configured accept rules.
Will use the unit type’s constructor by default unless an alternative factory method and constructor arguments are provided in the accept rule or passed in via context.
Most object coercions use the defaults, but the context and options enable specific customisations, such as passing in an assigned attribute name or the hash key that the input is a value of.
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
# File 'lib/yarrow/schema/types.rb', line 72
def coerce(input, context=nil)
constructor, options = accepts[input.class]
context_args = if context.nil?
options.clone
elsif options.nil?
context.clone
else
options.merge(context)
end
if context_args.nil?
unit.send(constructor, input)
else
unit.send(constructor, input, context_args)
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
|