Sha256: f131167ffd398c2f76b4884ea5e858087d9de71d87e0210d33182e30bf9c7461

Contents?: true

Size: 1.17 KB

Versions: 2

Compression:

Stored size: 1.17 KB

Contents

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

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

      primitive ::Date

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

      # Creates a Date instance from a Hash with keys :year, :month, :day
      #
      # @param [Hash, #to_mash] value
      #   value to be typecast
      #
      # @return [Date]
      #   Date constructed from hash
      #
      # @api private
      def typecast_hash_to_date(value)
        ::Date.new(*extract_time(value)[0, 3])
      end
    end # class Date
  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.rb
dm-core-1.1.0.rc3 lib/dm-core/property/date.rb