Module: Wukong::Streamer::StructRecordizer

Included in:
StructStreamer
Defined in:
lib/wukong/streamer/struct_streamer.rb

Overview

Mix StructRecordizer into any streamer to make it accept a stream of objects – the first field in each line is turned into a class and used to instantiate an object using the remaining fields on that line.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.recordize(rsrc, *fields) ⇒ Object

Turned the first field into a class name, then use the remaining fields on that line to instantiate the object to process.



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/wukong/streamer/struct_streamer.rb', line 15

def self.recordize rsrc, *fields
  klass_name, suffix = rsrc.split('-', 2)
  klass = Wukong.class_from_resource(klass_name) or return
  # instantiate the class using the remaining fields on that line
  begin
    [ klass.new(*fields), suffix ]
  rescue ArgumentError => e
    warn "Couldn't instantiate: #{e} (#{[klass, fields].inspect})"
    return
  rescue Exception => e
    raise [e, rsrc, fields].inspect
  end
end

Instance Method Details

#recordize(line) ⇒ Object



32
33
34
# File 'lib/wukong/streamer/struct_streamer.rb', line 32

def recordize line
  StructRecordizer.recordize *line.split("\t") unless line.blank?
end