Sha256: 8f7f6c9ea87c9ee4203d67c57283a829690809aa049ab0625d8b649f5827b9cf

Contents?: true

Size: 1.06 KB

Versions: 4

Compression:

Stored size: 1.06 KB

Contents

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

      load_as ::Date
      dump_as ::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
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
sbf-dm-core-1.5.0 lib/dm-core/property/date.rb
sbf-dm-core-1.4.0 lib/dm-core/property/date.rb
sbf-dm-core-1.3.0 lib/dm-core/property/date.rb
sbf-dm-core-1.3.0.beta lib/dm-core/property/date.rb