4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
# File 'lib/yukata/coercions/string_definitions.rb', line 4
def self.bind_to(coercer)
type_parser = -> (obj, target) do
begin
target.parse(obj)
rescue ArgumentError
nil
end
end
coercer.register(String, Time, &type_parser)
coercer.register(String, Date, &type_parser)
coercer.register(String, DateTime, &type_parser)
coercer.register(String, Integer) { |obj, _| obj.to_i }
coercer.register(String, Float) { |obj, _| obj.to_f }
coercer.register(String, Boolean) do |string, _|
%w(1 on t true y yes).include?(string.downcase)
end
end
|