Module: Dry::Types::Coercions::Params
- Extended by:
- Dry::Types::Coercions
- Defined in:
- lib/dry/types/coercions/params.rb
Overview
Params-specific coercions
Constant Summary collapse
- TRUE_VALUES =
%w[1 on On ON t true True TRUE T y yes Yes YES Y].freeze
- FALSE_VALUES =
%w[0 off Off OFF f false False FALSE F n no No NO N].freeze
- BOOLEAN_MAP =
EMPTY_HASH.merge( [true, *TRUE_VALUES].to_h { |v| [v, true] }, [false, *FALSE_VALUES].to_h { |v| [v, false] } ).freeze
Class Method Summary collapse
- .to_ary(input, &_block) ⇒ Array, Object
- .to_date(input, &block) ⇒ Date, Object extended from Dry::Types::Coercions
- .to_date_time(input, &block) ⇒ DateTime, Object extended from Dry::Types::Coercions
- .to_decimal(input, &_block) ⇒ BigDecimal, ...
- .to_false(input, &_block) ⇒ Boolean, Object
- .to_float(input, &block) ⇒ Float, ...
- .to_hash(input, &_block) ⇒ Hash, Object
- .to_int(input, &block) ⇒ Integer, ...
-
.to_nil(input, &_block) ⇒ nil
If the input is an empty string or nil.
- .to_symbol(input, &block) ⇒ Symbol, Object extended from Dry::Types::Coercions
- .to_time(input, &block) ⇒ Time, Object extended from Dry::Types::Coercions
- .to_true(input, &_block) ⇒ Boolean, Object
Class Method Details
.to_ary(input, &_block) ⇒ Array, Object
135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/dry/types/coercions/params.rb', line 135 def self.to_ary(input, &_block) if empty_str?(input) [] elsif input.is_a?(::Array) input elsif block_given? yield else raise CoercionError, "#{input.inspect} cannot be coerced to array" end end |
.to_date(input, &block) ⇒ Date, Object Originally defined in module Dry::Types::Coercions
.to_date_time(input, &block) ⇒ DateTime, Object Originally defined in module Dry::Types::Coercions
.to_decimal(input, &_block) ⇒ BigDecimal, ...
116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/dry/types/coercions/params.rb', line 116 def self.to_decimal(input, &_block) to_float(input) do if block_given? return yield else raise CoercionError, "#{input.inspect} cannot be coerced to decimal" end end input.to_d end |
.to_false(input, &_block) ⇒ Boolean, Object
69 70 71 72 73 74 75 76 77 |
# File 'lib/dry/types/coercions/params.rb', line 69 def self.to_false(input, &_block) BOOLEAN_MAP.fetch(input.to_s) do if block_given? yield else raise CoercionError, "#{input} cannot be coerced to false" end end end |
.to_float(input, &block) ⇒ Float, ...
103 104 105 106 107 |
# File 'lib/dry/types/coercions/params.rb', line 103 def self.to_float(input, &block) Float(input) rescue ArgumentError, TypeError => e CoercionError.handle(e, &block) end |
.to_hash(input, &_block) ⇒ Hash, Object
154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/dry/types/coercions/params.rb', line 154 def self.to_hash(input, &_block) if empty_str?(input) {} elsif input.is_a?(::Hash) input elsif block_given? yield else raise CoercionError, "#{input.inspect} cannot be coerced to hash" end end |
.to_int(input, &block) ⇒ Integer, ...
86 87 88 89 90 91 92 93 94 |
# File 'lib/dry/types/coercions/params.rb', line 86 def self.to_int(input, &block) if input.is_a? String Integer(input, 10) else Integer(input) end rescue ArgumentError, TypeError => e CoercionError.handle(e, &block) end |
.to_nil(input, &_block) ⇒ nil
Returns if the input is an empty string or nil.
29 30 31 32 33 34 35 36 37 |
# File 'lib/dry/types/coercions/params.rb', line 29 def self.to_nil(input, &_block) if input.nil? || empty_str?(input) nil elsif block_given? yield else raise CoercionError, "#{input.inspect} is not nil" end end |
.to_symbol(input, &block) ⇒ Symbol, Object Originally defined in module Dry::Types::Coercions
.to_time(input, &block) ⇒ Time, Object Originally defined in module Dry::Types::Coercions
.to_true(input, &_block) ⇒ Boolean, Object
49 50 51 52 53 54 55 56 57 |
# File 'lib/dry/types/coercions/params.rb', line 49 def self.to_true(input, &_block) BOOLEAN_MAP.fetch(input.to_s) do if block_given? yield else raise CoercionError, "#{input} cannot be coerced to true" end end end |