module ShallowAttributes::Type

Namespace for standard type classes

@since 0.1.0

Constants

DEFAULT_TYPE_OBJECTS

Hash object with cached type objects.

@private

@since 0.1.0

Public Class Methods

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

Convert value object to specific Type class

@private

@param [Class] type the type class object @param [Object] value the value that should be coerced to the necessary type @param [Hash] options the options to create a message with. @option options [String] :of The type of array @option options [boolean] :allow_nil cast `nil` to integer or float

@example Convert integer to sting type

ShallowAttributes::Type.coerce(String, 1)
  # => '1'

@example Convert string to custom hash type

ShallowAttributes::Type.instance_for(JsonType, '{"a"=>1}')
  # => { a: 1 }

@return the converted value object

@since 0.1.0

# File lib/shallow_attributes/type.rb, line 58
def coerce(type, value, options = {})
  type_instance(type).coerce(value, options)
end

Private Class Methods

type_instance(klass) click to toggle source

Returns class object for specific Type class

@private

@param [Class] klass the type class object

@example Returns Sting type class

ShallowAttributes::Type.instance_for(String)
  # => ShallowAttributes::Type::Sting class

@example Returns other type class

ShallowAttributes::Type.instance_for(MySpecialStringType)
  # => MySpecialStringType class

@return [Class]

@since 0.1.0

# File lib/shallow_attributes/type.rb, line 81
def type_instance(klass)
  DEFAULT_TYPE_OBJECTS[klass] || ShallowAttributes::Type.const_get(klass.name).new
end