Class: Wukong::Processor::Recordize

Inherits:
Serializer show all
Defined in:
lib/wukong/widget/serializers.rb

Overview

A widget for turning a record into an instance of some class. The class must provide a "class method" receive which accepts a Hash argument.

Constant Summary

Constants inherited from Wukong::Processor

SerializerError

Instance Attribute Summary

Attributes included from Hanuman::StageInstanceMethods

#graph

Instance Method Summary collapse

Methods inherited from Serializer

#handle_error

Methods inherited from Wukong::Processor

configure, description, #finalize, #perform_action, #receive_action, #setup, #stop

Methods included from Logging

included

Methods inherited from Hanuman::Stage

#clone

Methods included from Hanuman::StageClassMethods

#builder, #label, #register, #set_builder

Methods included from Hanuman::StageInstanceMethods

#add_link, #linkable_name, #root

Instance Method Details

#model_class_for(record) ⇒ Object



303
304
305
306
307
308
309
310
311
312
# File 'lib/wukong/widget/serializers.rb', line 303

def model_class_for(record)
  if explicit_type = (record[:_type] || record["_type"])
    begin
      return explicit_type.constantize
    rescue NameError => e
      log.warn("Could not find a class for <#{explicit_type}>")
    end
  end
  return model if model
end

#process(record) ⇒ Object

Turn the given record into an instance of the class named with the model field.

Parameters:

  • record (Hash, #to_wire)

Returns:

  • (Object)


290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/wukong/widget/serializers.rb', line 290

def process(record)
  wire_format = record.try(:to_wire) || record
  raise SerializerError.new("Can only recordize a Hash-like record") unless wire_format.is_a?(Hash)
  klass = model_class_for(wire_format)
  if klass
    yield klass.receive(wire_format)
  else
    log.error("No default model class and no explicit model for: #{wire_format.inspect}")
  end
rescue => e
  handle_error(record, e)
end