Module: Dry::Types::Coercions
- Includes:
- Core::Constants
- Defined in:
- lib/dry/types/coercions.rb,
lib/dry/types/coercions/json.rb,
lib/dry/types/coercions/params.rb
Defined Under Namespace
Instance Method Summary collapse
-
#empty_str?(value) ⇒ Boolean
private
Checks whether String is empty.
- #to_date(input) ⇒ Date, Object
- #to_date_time(input) ⇒ DateTime, Object
- #to_nil(input) ⇒ nil, Object
- #to_time(input) ⇒ Time, Object
Instance Method Details
#empty_str?(value) ⇒ Boolean (private)
Checks whether String is empty
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
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
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
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
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 |