Sha256: 501958f57a6b19357fed305ce7f5ca7ed3ab49fcdf740bb8f86bbfe7ef7b9ca7

Contents?: true

Size: 1.4 KB

Versions: 2

Compression:

Stored size: 1.4 KB

Contents

require_relative("accumulator")

module Wukong
  class Processor

    # A processor which counts the total number of its input records.
    #
    # On it's own, this widget is really just a poor man's `wc -l`.
    # It's really intended to serve as a superclass for more complex
    # accumulators.
    #
    # @example Count the total number of input records on the command-line.
    #
    #   $ wc -l input
    #   283 input
    #   $ cat input | wu-local count
    #   283
    class Count < Accumulator

      # The total size of the input recors.
      attr_accessor :size

      # Initializes the count to 0.
      def setup
        super()
        @size = 0
      end

      # Accumulate a `record` by incrmenting the total size.
      #
      # @param [Object] record
      def accumulate record
        self.size += 1
      end

      # Keeps all records in the same group so that one count is
      # emitted at the end.
      #
      # Overriding this method and returning different keys for
      # different records is the beginning of constructing a "group
      # by" type widget.
      #
      # @param [Object] record
      # @return [:__first__group__]
      # @see Group
      def get_key record
        :__first_group__
      end

      # Yields the total size.
      #
      # @yield [size]
      # @yieldparam [Integer] size
      def finalize
        yield self.size
      end

      register
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
wukong-3.0.0.pre3 lib/wukong/widget/reducers/count.rb
wukong-3.0.0.pre2 lib/wukong/widget/reducers/count.rb