Sha256: 4975c213bcb351dda96d48c56e4294ab0184dd4f9097b31acd6e4c8fd8f1e4d7

Contents?: true

Size: 1.1 KB

Versions: 1

Compression:

Stored size: 1.1 KB

Contents

module Wukong
  module Widget

    class Stringifier < Processor
    end

    class ToJson < Stringifier
      def process(record)
        emit record.to_json
        # emit MultiJson.dump(record)
      end
      register_processor
    end

    class FromJson < Stringifier
      # FIXME some of this belongs in gorillib factories...
      def process(record)
        obj = MultiJson.load(record)
        if obj.respond_to?(:has_key?) && obj.has_key?("_metadata")
          metadata_hash = obj.delete("_metadata")
          obj.define_singleton_method(:_metadata) do
            metadata_hash
          end
        end
        if obj.respond_to?(:has_key?) && obj.has_key?("_type")
          klass = Gorillib::Factory(obj.delete("_type"))
          obj = klass.receive(obj)
        end
        emit obj
      end
      register_processor
    end

    class ToTsv < Stringifier
      def process(record)
        emit record.join("\t")
      end
      register_processor
    end

    class FromTsv < Stringifier
      def process(record)
        emit record.chomp.split(/\t/)
      end
      register_processor
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
wukong-3.0.0.pre lib/wukong/widget/stringifier.rb