Sha256: 2f3774ef8878f7ea8bda8d223d0840392f456025cde5c679e803ef68a2e1cec5

Contents?: true

Size: 940 Bytes

Versions: 1

Compression:

Stored size: 940 Bytes

Contents

module ShallowAttributes
  module Type
    # Abstract class for typecast object to Time type.
    #
    # @abstract
    #
    # @since 0.1.0
    class Time
      # Convert value to Time type
      #
      # @private
      #
      # @param [Object] value
      # @param [Hash] option
      #
      # @example Convert string to Time value
      #   ShallowAttributes::Type::Time.new.coerce('Thu Nov 29 14:33:20 GMT 2001')
      #     # => '2001-11-29 14:33:20 +0000'
      #
      # @raise [InvalidValueError] if values is not a sting or integer
      #
      # @return [Time]
      #
      # @since 0.1.0
      def coerce(value, options = {})
        case value
        when ::Time then value
        when ::Integer then ::Time.at(value)
        else
          ::Time.parse(value.to_s)
        end
      rescue
        raise ShallowAttributes::Type::InvalidValueError, %(Invalid value "#{value}" for type "Time")
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
shallow_attributes-0.9.0 lib/shallow_attributes/type/time.rb