Sha256: 19e3cbcc0bb3e86bf82b26301ad78e8eb354d5f963a785183cd27757bdca4218

Contents?: true

Size: 825 Bytes

Versions: 3

Compression:

Stored size: 825 Bytes

Contents

require 'date'
require 'time'
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

3 entries across 3 versions & 1 rubygems

Version Path
transproc-0.1.3 lib/transproc/coercions.rb
transproc-0.1.2 lib/transproc/coercions.rb
transproc-0.1.1 lib/transproc/coercions.rb