Module: Dry::Types::Coercions

Includes:
Core::Constants
Included in:
Form, JSON
Defined in:
lib/dry/types/coercions.rb,
lib/dry/types/coercions/form.rb,
lib/dry/types/coercions/json.rb

Defined Under Namespace

Modules: Form, JSON

Instance Method Summary collapse

Instance Method Details

#to_date(input) ⇒ Object



12
13
14
15
16
17
# File 'lib/dry/types/coercions.rb', line 12

def to_date(input)
  return input unless input.respond_to?(:to_str)
  Date.parse(input)
rescue ArgumentError
  input
end

#to_date_time(input) ⇒ Object



19
20
21
22
23
24
# File 'lib/dry/types/coercions.rb', line 19

def to_date_time(input)
  return input unless input.respond_to?(:to_str)
  DateTime.parse(input)
rescue ArgumentError
  input
end

#to_nil(input) ⇒ Object



8
9
10
# File 'lib/dry/types/coercions.rb', line 8

def to_nil(input)
  input unless empty_str?(input)
end

#to_time(input) ⇒ Object



26
27
28
29
30
31
# File 'lib/dry/types/coercions.rb', line 26

def to_time(input)
  return input unless input.respond_to?(:to_str)
  Time.parse(input)
rescue ArgumentError
  input
end