class ShallowAttributes::Type::String

Abstract class for typecast object to String type.

@abstract

@since 0.1.0

Public Instance Methods

coerce(value, options = {}) click to toggle source

Convert value to String type

@private

@param [Object] value @param [Hash] _options

@example Convert integer to string value

ShallowAttributes::Type::String.new.coerce(2001)
  # => '2001'

@return [Sting]

@since 0.1.0

# File lib/shallow_attributes/type/string.rb, line 23
def coerce(value, options = {})
  case value
  when nil then options[:allow_nil] ? nil : ''
  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 Instance Methods

error(value) click to toggle source
# File lib/shallow_attributes/type/string.rb, line 35
def error(value)
  raise ShallowAttributes::Type::InvalidValueError, %(Invalid value "#{value}" for type "String")
end