Sha256: 881d339f94f208abf36403773a7856f2bb8fb73e4bb954be0167285b4b39de6b

Contents?: true

Size: 1.42 KB

Versions: 18

Compression:

Stored size: 1.42 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} (#{[klass, fields].inspect})"
          return
        rescue Exception => e
          raise [e, rsrc, fields].inspect
        end
      end

      #
      #
      #
      def recordize line
        StructRecordizer.recordize *line.split("\t") unless line.blank?
      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

18 entries across 18 versions & 1 rubygems

Version Path
wukong-3.0.0.pre old/wukong/streamer/struct_streamer.rb
wukong-2.0.2 lib/wukong/streamer/struct_streamer.rb
wukong-2.0.1 lib/wukong/streamer/struct_streamer.rb
wukong-2.0.0 lib/wukong/streamer/struct_streamer.rb
wukong-1.5.4 lib/wukong/streamer/struct_streamer.rb
wukong-1.5.3 lib/wukong/streamer/struct_streamer.rb
wukong-1.5.2 lib/wukong/streamer/struct_streamer.rb
wukong-1.5.1 lib/wukong/streamer/struct_streamer.rb
wukong-1.5.0 lib/wukong/streamer/struct_streamer.rb
wukong-1.4.12 lib/wukong/streamer/struct_streamer.rb
wukong-1.4.11 lib/wukong/streamer/struct_streamer.rb
wukong-1.4.10 lib/wukong/streamer/struct_streamer.rb
wukong-1.4.9 lib/wukong/streamer/struct_streamer.rb
wukong-1.4.7 lib/wukong/streamer/struct_streamer.rb
wukong-1.4.6 lib/wukong/streamer/struct_streamer.rb
wukong-1.4.5 lib/wukong/streamer/struct_streamer.rb
wukong-1.4.2 lib/wukong/streamer/struct_streamer.rb
wukong-1.4.1 lib/wukong/streamer/struct_streamer.rb