Module: ShallowAttributes::Type

Defined in:
lib/shallow_attributes/type.rb,
lib/shallow_attributes/type/time.rb,
lib/shallow_attributes/type/array.rb,
lib/shallow_attributes/type/float.rb,
lib/shallow_attributes/type/string.rb,
lib/shallow_attributes/type/boolean.rb,
lib/shallow_attributes/type/integer.rb,
lib/shallow_attributes/type/date_time.rb

Overview

Namespace for standart type classes

Since:

  • 0.1.0

Defined Under Namespace

Classes: Array, Boolean, DateTime, Float, Integer, InvalidValueError, String, Time

Constant Summary collapse

DEFAULT_TYPE_OBJECTS =

Hash object with cached type objects.

Since:

  • 0.1.0

{
  ::Array    => ShallowAttributes::Type::Array.new,
  ::DateTime => ShallowAttributes::Type::DateTime.new,
  ::Float    => ShallowAttributes::Type::Float.new,
  ::Integer  => ShallowAttributes::Type::Integer.new,
  ::String   => ShallowAttributes::Type::String.new,
  ::Time     => ShallowAttributes::Type::Time.new
}.freeze

Class Method Summary collapse

Class Method Details

.coerce(type, value, options = {}) ⇒ Object

Convert value object to specific Type class

Examples:

Convert integer to sting type

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

Convert string to custom hash type

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

Options Hash (options):

  • :of (String)

    The type of array

  • :allow_nil (boolean)

    cast `nil` to integer or float

Since:

  • 0.1.0


56
57
58
# File 'lib/shallow_attributes/type.rb', line 56

def coerce(type, value, options = {})
  type_instance(type).coerce(value, options)
end