Class: ShallowAttributes::Type::String Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/shallow_attributes/type/string.rb

Overview

This class is abstract.

Abstract class for typecast object to String type.

Since:

  • 0.1.0

Instance Method Summary collapse

Instance Method Details

#coerce(value, _options = {}) ⇒ Sting

Convert value to String type

Examples:

Convert intger to string value

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

Parameters:

  • value (Object)
  • option (Hash)

Returns:

  • (Sting)

Since:

  • 0.1.0


23
24
25
26
27
28
29
30
# File 'lib/shallow_attributes/type/string.rb', line 23

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