Sha256: a7d7a9b3c83ee02bcbd221aa3578cfbef8b596ef36944fbd354ea01ffd532ed8

Contents?: true

Size: 1.26 KB

Versions: 1

Compression:

Stored size: 1.26 KB

Contents

module Coercible
  class Coercer

    # Common time coercion methods
    module TimeCoercions

      # Coerce given value to String
      #
      # @example
      #   coercer[Time].to_string(time)  # => "Wed Jul 20 10:30:41 -0700 2011"
      #
      # @param [Date,Time,DateTime] value
      #
      # @return [String]
      #
      # @api public
      def to_string(value)
        value.to_s
      end

      # Coerce given value to Time
      #
      # @example
      #   coercer[DateTime].to_time(datetime)  # => Time object
      #
      # @param [Date,DateTime] value
      #
      # @return [Time]
      #
      # @api public
      def to_time(value)
        coerce_with_method(value, :to_time)
      end

      private

      # Try to use native coercion method on the given value
      #
      # Falls back to String-based parsing
      #
      # @param [Date,DateTime,Time] value
      # @param [Symbol] method
      #
      # @return [Date,DateTime,Time]
      #
      # @api private
      def coerce_with_method(value, method)
        if value.respond_to?(method)
          value.public_send(method)
        else
          coercers[::String].public_send(method, to_string(value))
        end
      end

    end # module TimeCoercions

  end # class Coercer
end # module Coercible

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
motion_coercible-0.2.0 lib/project/coercer/time_coercions.rb