Module: EasyParams::Types

Defined in:
lib/easy_params/types.rb,
lib/easy_params/types/struct.rb,
lib/easy_params/types/generic.rb,
lib/easy_params/types/collection.rb

Defined Under Namespace

Modules: Struct Classes: CoercionError, Collection, Generic, StructsCollection

Constant Summary collapse

BOOLEAN_MAP =
{ '1' => true, 't' => true, 'true' => true, 'True' => true, 'TRUE' => true, 'T' => true }.merge(
  { '0' => false, 'f' => false, 'false' => false, 'False' => false, 'FALSE' => false, 'F' => false }
).freeze
Array =
Collection.new(:array)
Each =
StructsCollection.new(:array_of_structs)
Integer =
Generic.new(:integer, &:to_i)
Float =
Generic.new(:float, &:to_f)
String =
Generic.new(:string, &:to_s)
Decimal =
Generic.new(:decimal) { |v| v.to_f.to_d }
Bool =
Generic.new(:bool) do |v|
  BOOLEAN_MAP.fetch(v.to_s) { raise CoercionError }
end
Date =
Generic.new(:date) do |v|
  ::Date.parse(v)
rescue ArgumentError, RangeError
  raise CoercionError, 'cannot be coerced'
end
DateTime =
Generic.new(:datetime) do |v|
  ::DateTime.parse(v)
rescue ArgumentError
  raise CoercionError, 'cannot be coerced'
end
Time =
Generic.new(:time) do |v|
  ::Time.parse(v)
rescue ArgumentError
  raise CoercionError, 'cannot be coerced'
end