Sha256: cbef20acf4f4226909496f3d5649604e3832bb75401f09acd38ce762bee62bd3

Contents?: true

Size: 1.4 KB

Versions: 4

Compression:

Stored size: 1.4 KB

Contents

# -*- coding: utf-8 -*-
module Wukong
  module Streamer
    #
    # 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.
    #
    module StructRecordizer

      #
      # Turned the first field into a class name, then use the remaining fields
      # on that line to instantiate the object to process.
      #
      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} (#{[rsrc, fields].inspect})"
          return
        rescue Exception => e
          raise [e, rsrc, fields].inspect
        end
      end

      #
      #
      #
      def recordize line
        StructRecordizer.recordize *line.split("\t")
      end
    end

    #
    # Processes file as 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.
    #
    # See [StructRecordizer] for more.
    #
    class StructStreamer < Wukong::Streamer::Base
      include StructRecordizer
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
mrflip-wukong-0.1.0 lib/wukong/streamer/struct_streamer.rb
wukong-1.4.0 lib/wukong/streamer/struct_streamer.rb
wukong-0.1.4 lib/wukong/streamer/struct_streamer.rb
wukong-0.1.1 lib/wukong/streamer/struct_streamer.rb