Sha256: 73300607f206bafcf03e5079bc2303ba5d1e51a3b01e084a931355d1dfa0a0e7

Contents?: true

Size: 1.12 KB

Versions: 6

Compression:

Stored size: 1.12 KB

Contents

require "bigdecimal"
require "date"
require "json"
require "time"
require "set"

module Ohm
  module DataTypes
    module Type
      Integer   = ->(x) { x.to_i }
      Decimal   = ->(x) { BigDecimal(x.to_s) }
      Float     = ->(x) { x.to_f }
      Symbol    = ->(x) { x && x.to_sym }
      Boolean   = ->(x) { !!x }
      Time      = ->(t) { t && (t.kind_of?(::Time) ? t : ::Time.parse(t)) }
      Date      = ->(d) { d && (d.kind_of?(::Date) ? d : ::Date.parse(d)) }
      Timestamp = ->(t) { t && UnixTime.at(t.to_i) }
      Hash      = ->(h) { h && SerializedHash[h.kind_of?(::Hash) ? h : JSON(h)] }
      Array     = ->(a) { a && SerializedArray.new(a.kind_of?(::Array) ? a : JSON(a)) }
      Set       = ->(s) { s && SerializedSet.new(s.kind_of?(::Set) ? s : JSON(s)) }
    end

    class UnixTime < Time
      def to_s
        to_i.to_s
      end
    end

    class SerializedHash < Hash
      def to_s
        JSON.dump(self)
      end
    end

    class SerializedArray < Array
      def to_s
        JSON.dump(self)
      end
    end

    class SerializedSet < ::Set
      def to_s
        JSON.dump(to_a.sort)
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
ohm-contrib-3.0.0 lib/ohm/datatypes.rb
ohm-contrib-2.2.0 lib/ohm/datatypes.rb
ohm-contrib-2.0.1 lib/ohm/datatypes.rb
ohm-contrib-2.0.0 lib/ohm/datatypes.rb
ohm-contrib-2.0.0.rc2 lib/ohm/datatypes.rb
ohm-contrib-2.0.0.rc1 lib/ohm/datatypes.rb