Sha256: b78acde588d4cedd3b1597d0700de81d9c820e389461b780010f190bb67c7d47

Contents?: true

Size: 810 Bytes

Versions: 1

Compression:

Stored size: 810 Bytes

Contents

require 'date'
require 'bigdecimal'
require 'bigdecimal/util'

module Transproc
  TRUE_VALUES = [true, 1, '1', 'on', 't', 'true', 'y', 'yes'].freeze
  FALSE_VALUES = [false, 0, '0', 'off', 'f', 'false', 'n', 'no'].freeze

  BOOLEAN_MAP = Hash[
    TRUE_VALUES.product([true]) + FALSE_VALUES.product([false])
  ].freeze

  register(:to_string) do |value|
    value.to_s
  end

  register(:to_integer) do |value|
    value.to_i
  end

  register(:to_float) do |value|
    value.to_f
  end

  register(:to_decimal) do |value|
    value.to_d
  end

  register(:to_boolean) do |value|
    BOOLEAN_MAP.fetch(value)
  end

  register(:to_date) do |value|
    Date.parse(value)
  end

  register(:to_time) do |value|
    Time.parse(value)
  end

  register(:to_datetime) do |value|
    DateTime.parse(value)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
transproc-0.1.0 lib/transproc/coercions.rb