Sha256: cdc12fbb89cbd76d9d2ee3ba3dcc2c56f699744923fbd62ea0db80ff70f7b987

Contents?: true

Size: 1.87 KB

Versions: 12

Compression:

Stored size: 1.87 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

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

      # Coerce given value to Date
      #
      # @example
      #   coercer[Time].to_date(time)  # => Date object
      #
      # @param [Time,DateTime] value
      #
      # @return [Date]
      #
      # @api public
      def to_date(value)
        coerce_with_method(value, :to_date)
      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

12 entries across 10 versions & 5 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/coercible-1.0.0/lib/coercible/coercer/time_coercions.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/coercible-1.0.0/lib/coercible/coercer/time_coercions.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/coercible-1.0.0/lib/coercible/coercer/time_coercions.rb
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/coercible-1.0.0/lib/coercible/coercer/time_coercions.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.4.0/gems/coercible-1.0.0/lib/coercible/coercer/time_coercions.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/coercible-1.0.0/lib/coercible/coercer/time_coercions.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/coercible-1.0.0/lib/coercible/coercer/time_coercions.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/coercible-1.0.0/lib/coercible/coercer/time_coercions.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/coercible-1.0.0/lib/coercible/coercer/time_coercions.rb
coercible-1.0.0 lib/coercible/coercer/time_coercions.rb
motion_coercible-0.2.0.2 lib/project/coercer/time_coercions.rb
motion_coercible-0.2.0.1 lib/project/coercer/time_coercions.rb