Sha256: e04fb8376845f5b3209ab1084d23ea6561b2b224781732d65c3b82743e532279

Contents?: true

Size: 1.18 KB

Versions: 2

Compression:

Stored size: 1.18 KB

Contents

require 'dm-core/property/typecast/time'

module DataMapper
  class Property
    class DateTime < Object
      include PassThroughLoadDump
      include Typecast::Time

      primitive ::DateTime

      # Typecasts an arbitrary value to a DateTime.
      # Handles both Hashes and DateTime instances.
      #
      # @param [Hash, #to_mash, #to_s] value
      #   value to be typecast
      #
      # @return [DateTime]
      #   DateTime constructed from value
      #
      # @api private
      def typecast_to_primitive(value)
        if value.is_a?(::Hash) || value.respond_to?(:to_mash)
          typecast_hash_to_datetime(value)
        else
          ::DateTime.parse(value.to_s)
        end
      rescue ArgumentError
        value
      end

      # Creates a DateTime instance from a Hash with keys :year, :month, :day,
      # :hour, :min, :sec
      #
      # @param [Hash, #to_mash] value
      #   value to be typecast
      #
      # @return [DateTime]
      #   DateTime constructed from hash
      #
      # @api private
      def typecast_hash_to_datetime(value)
        ::DateTime.new(*extract_time(value))
      end
    end # class DateTime
  end # class Property
end # module DataMapper

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dm-core-1.1.0 lib/dm-core/property/date_time.rb
dm-core-1.1.0.rc3 lib/dm-core/property/date_time.rb