class Pyper::Pipes::Model::VirtusDeserializer

Provides a way to deserialize serialized fields from an item. This is intended to be used with a Virtus model class, and will use the attribute names and type information from that model to determine how to deserialize.

All serialization is as JSON.

Attributes

type_mapping[R]

Public Class Methods

new(attribute_set) click to toggle source

@param attribute_set [Virtus::AttributeSet] A Virtus AttributeSet

# File lib/pyper/pipes/model/virtus_deserializer.rb, line 14
def initialize(attribute_set)
  @type_mapping = Hash[attribute_set.map { |attr| [attr.name.to_s, attr.type.primitive] }]
end

Public Instance Methods

deserialize(value, type) click to toggle source
# File lib/pyper/pipes/model/virtus_deserializer.rb, line 32
def deserialize(value, type)
  if type == Array || type == Hash then JSON.parse(value)
  elsif type == Integer then value.to_i
  elsif type == Float then value.to_f
  else value end
end
pipe(items, status = {}) click to toggle source

@param items [Enumerator<Hash>] A list of items @param status [Hash] The mutable status field @return [Enumerator<Hash>] A list of items, deserialized according to the type mapping

# File lib/pyper/pipes/model/virtus_deserializer.rb, line 21
def pipe(items, status = {})
  items.map do |item|
    new_item = item.dup
    type_mapping.each do |field, type|
      new_item[field] = deserialize(new_item[field], type) if new_item[field]
    end

    new_item
  end
end