Module: Dry::Types::Coercions

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

Defined Under Namespace

Modules: JSON, Params

Instance Method Summary collapse

Instance Method Details

#empty_str?(value) ⇒ Boolean (private)

Checks whether String is empty

Parameters:

  • value (String, Object)

Returns:

  • (Boolean)


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

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


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

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

#to_date_time(input) ⇒ DateTime, Object

Parameters:

  • input (#to_str, Object)

Returns:

  • (DateTime, Object)

See Also:

  • DateTime.parse


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

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

#to_nil(input) ⇒ nil, Object

Parameters:

  • input (String, Object)

Returns:

  • (nil)

    if the input is an empty string

  • (Object)

    otherwise the input object is returned



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

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


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

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