Sha256: b5fb79dcff1851c9aaee56a67b4f1ec133632f14d5b804f009a527dd01d809ef

Contents?: true

Size: 971 Bytes

Versions: 1

Compression:

Stored size: 971 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.2 lib/shallow_attributes/type/date_time.rb