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

#empty_str?(value) ⇒ Boolean (private)

Checks whether String is empty

Parameters:

  • value (String, Object)

Returns:

  • (Boolean)


47
48
49
# File 'lib/dry/types/coercions.rb', line 47

def empty_str?(value)
  EMPTY_STRING.eql?(value)
end

#to_date(input) ⇒ Date, Object

Parameters:

  • input (#to_str, Object)

Returns:

  • (Date, Object)

See Also:

  • Date.parse


15
16
17
18
19
20
# File 'lib/dry/types/coercions.rb', line 15

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

#to_date_time(input) ⇒ DateTime, Object

Parameters:

  • input (#to_str, Object)

Returns:

  • (DateTime, Object)

See Also:

  • DateTime.parse


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

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

#to_nil(input) ⇒ String?

Parameters:

  • input (String)

Returns:

  • (String?)


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

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

#to_time(input) ⇒ Time, Object

Parameters:

  • input (#to_str, Object)

Returns:

  • (Time, Object)

See Also:

  • Time.parse


35
36
37
38
39
40
# File 'lib/dry/types/coercions.rb', line 35

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