Sha256: 1d02941ecad3abeab704eaa25f9b6fe2c6dd6f3f9e0cbc19b52c1bfb7078630d

Contents?: true

Size: 610 Bytes

Versions: 2

Compression:

Stored size: 610 Bytes

Contents

class Arstotzka::Wrapper
  include Arstotzka::TypeCast

  attr_reader :clazz, :type

  def initialize(clazz: nil, type: nil)
    @clazz = clazz
    @type = type
  end

  def wrap(value)
    return wrap_array(value) if value.is_a?(Array)
    wrap_element(value)
  end

  private

  def wrap_element(value)
    value = cast(value) if has_type? && !value.nil?
    return if value.nil?

    clazz ? clazz.new(value) : value
  end

  def wrap_array(array)
    array.map { |v| wrap v }
  end

  def has_type?
    type.present? && type != :none
  end

  def cast(value)
    public_send("to_#{type}", value)
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
arstotzka-1.0.1 lib/arstotzka/wrapper.rb
arstotzka-1.0.0 lib/arstotzka/wrapper.rb