Module: Arstotzka::TypeCast

Extended by:
ActiveSupport::Concern
Included in:
Wrapper
Defined in:
lib/arstotzka/type_cast.rb

Overview

Concern with all the type cast methods to be used by Wrapper

Usage of typecast is defined by the configuration of MethodBuilder by the usage of option type

TypeCast can also be extended to include more types

Supported types:

  • integer

  • string

  • float

  • symbol

Examples:

Extending typecast

class Car
  attr_reader :model, :maker

  def initialize(model:, maker:)
    @model = model
    @maker = maker
  end
end

module Arstotzka
  module TypeCast
    def to_car(hash)
      Car.new(hash.symbolize_keys)
    end
  end
end

class CarCollector
  include Arstotzka

  attr_reader :json

  expose :cars, full_path: 'cars.unit', type: :car

  def initialize(hash)
    @json = hash
  end
end

hash = {
  cars: [{
    unit: { model: 'fox', maker: 'volkswagen' }
  }, {
    unit: { 'model' => 'focus', 'maker' => 'ford' }
  }]
}

model = CarCollector.new(hash)

model.cars              # returns [<Car>, <Car>]
model.cars.map(&:model) # returns ['fox', 'focus']

Casting to Integer

class TypeCaster
  include Arstotzka

  expose :age, type: :integer, json: :@hash

  def initialize(hash)
    @hash = hash
  end
end

hash = {
  age: '10',
}

TypeCaster.new(age: '10').age

Casting to String

class TypeCaster
  include Arstotzka

  expose :payload, type: :string, json: :@hash

  def initialize(hash)
    @hash = hash
  end
end

hash = {
  payload: { 'key' => 'value' },
}

model.TypeCaster.new(hash)

model.payload # returns '{"key"=>"value"}'

Casting to Float

class TypeCaster
  include Arstotzka

  expose :price, type: :float, json: :@hash

  def initialize(hash)
    @hash = hash
  end
end

hash = {
  price: '1.75'
}

TypeCaster.new(price: '1.75').price # returns 1.75

Casting to Symbol

class TypeCaster
  include Arstotzka

  expose :type, type: :symbol, json: :@hash

  def initialize(hash)
    @hash = hash
  end
end

hash = {
  type: 'type_a'
}

TypeCaster.new(type: 'type_a').type # returns :type_a

Instance Method Summary collapse

Instance Method Details

#to_float(value) ⇒ Float

Converts value to float

Examples:

Casting to Float

class TypeCaster
  include Arstotzka

  expose :price, type: :float, json: :@hash

  def initialize(hash)
    @hash = hash
  end
end

hash = {
  price: '1.75'
}

TypeCaster.new(price: '1.75').price # returns 1.75

Parameters:

  • value (Object)

    object to be converted to float

Returns:

  • (Float)


151
152
153
# File 'lib/arstotzka/type_cast.rb', line 151

def to_float(value)
  value.to_s.to_f if value.present?
end

#to_integer(value) ⇒ Integer

Converts a value to integer

Examples:

Casting to Integer

class TypeCaster
  include Arstotzka

  expose :age, type: :integer, json: :@hash

  def initialize(hash)
    @hash = hash
  end
end

hash = {
  age: '10',
}

TypeCaster.new(age: '10').age

Parameters:

  • value (Object)

    object to be converted to integer

Returns:

  • (Integer)


95
96
97
# File 'lib/arstotzka/type_cast.rb', line 95

def to_integer(value)
  value.to_s.to_i if value.present?
end

#to_string(value) ⇒ String

Converts value to string

Examples:

Casting to String

class TypeCaster
  include Arstotzka

  expose :payload, type: :string, json: :@hash

  def initialize(hash)
    @hash = hash
  end
end

hash = {
  payload: { 'key' => 'value' },
}

model.TypeCaster.new(hash)

model.payload # returns '{"key"=>"value"}'

Parameters:

  • value (Object)

    object to be converted to string

Returns:

  • (String)


124
125
126
# File 'lib/arstotzka/type_cast.rb', line 124

def to_string(value)
  value.to_s
end

#to_symbol(value) ⇒ Symbol

Converts value to Symbol

Examples:

Casting to Symbol

class TypeCaster
  include Arstotzka

  expose :type, type: :symbol, json: :@hash

  def initialize(hash)
    @hash = hash
  end
end

hash = {
  type: 'type_a'
}

TypeCaster.new(type: 'type_a').type # returns :type_a

Parameters:

  • value (Object)

    object to be converted to symbol

Returns:

  • (Symbol)


178
179
180
# File 'lib/arstotzka/type_cast.rb', line 178

def to_symbol(value)
  value.to_s.to_sym if value.present?
end