Sha256: 5fb67516193f0ef75a68afb393eb6321474a0bfe295bfe81247c0947ae8b0f53

Contents?: true

Size: 883 Bytes

Versions: 7

Compression:

Stored size: 883 Bytes

Contents

require 'dm-core'

module DataMapper
  class Property
    class EpochTime < Integer

      def load(value)
        if value.kind_of?(::Numeric)
          ::Time.at(value.to_i)
        else
          value
        end
      end

      def dump(value)
        value.to_i if value
      end

      def custom?
        true
      end

      def typecast(value)
        case value
          when ::Time               then value
          when ::Numeric, /\A\d+\z/ then ::Time.at(value.to_i)
          when ::DateTime           then datetime_to_time(value)
          when ::String             then ::Time.parse(value)
        end
      end

    private

      def datetime_to_time(datetime)
        utc = datetime.new_offset(0)
        ::Time.utc(utc.year, utc.month, utc.day, utc.hour, utc.min, utc.sec)
      end

    end # class EpochTime
  end # class Property
end # module DataMapper

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
ardm-types-1.2.3 lib/dm-types/epoch_time.rb
ardm-types-1.2.2 lib/dm-types/epoch_time.rb
dm-types-1.2.2 lib/dm-types/epoch_time.rb
dm-types-1.2.1 lib/dm-types/epoch_time.rb
dm-types-1.2.0 lib/dm-types/epoch_time.rb
dm-types-1.2.0.rc2 lib/dm-types/epoch_time.rb
dm-types-1.2.0.rc1 lib/dm-types/epoch_time.rb