Sha256: 7b67971ab410da54b386d7fba84ecf45628c87e042b2d71f6749bccbf5776a86

Contents?: true

Size: 970 Bytes

Versions: 1

Compression:

Stored size: 970 Bytes

Contents

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