Method: Croesus::Coercions::StringDefinitions.bind_to

Defined in:
lib/croesus/coercions/string_definitions.rb

.bind_to(coercer) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/croesus/coercions/string_definitions.rb', line 22

def self.bind_to(coercer)
  # Attempt to parse the date. If it can't be parsed just return nil.
  # Silently failing is about the only thing I can think of.
  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