Sha256: ce451640f63097f5d83f3d30e518987d75ff2acc1f7f9b7a79cbfedeeef7a221

Contents?: true

Size: 897 Bytes

Versions: 1

Compression:

Stored size: 897 Bytes

Contents

module ShallowAttributes
  module Type
    # Abstract class for typecast object to String type.
    #
    # @abstract
    #
    # @since 0.1.0
    class String
      # Convert value to String type
      #
      # @private
      #
      # @param [Object] value
      # @param [Hash] option
      #
      # @example Convert intger to string value
      #   ShallowAttributes::Type::String.new.coerce(2001)
      #     # => '2001'
      #
      # @return [Sting]
      #
      # @since 0.1.0
      def coerce(value, options = {})
        case value
        when ::Array then value.join
        when ::Hash, ::Class then error(value)
        else
          value.respond_to?(:to_s) ? value.to_s : error(value)
        end
      end

    private

      def error(value)
        raise ShallowAttributes::Type::InvalidValueError, %(Invalid value "#{value}" for type "String")
      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/string.rb