Sha256: 477db5bc617020df369ddf56637239b1dd74d822588b6d6b8622a442d45795c7

Contents?: true

Size: 612 Bytes

Versions: 2

Compression:

Stored size: 612 Bytes

Contents

class JsonParser::Wrapper
  include JsonParser::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
json_parser-1.3.1 lib/json_parser/wrapper.rb
json_parser-1.3.0 lib/json_parser/wrapper.rb