Sha256: 62975cc1e0580bee50f11498d55e1c867c4a8b6de050fe403443b0a15c77ce42

Contents?: true

Size: 1.64 KB

Versions: 5

Compression:

Stored size: 1.64 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

      description <<EOF
This processor counts the number of input records it receives.

  $ wc -l input
  283 input
  $ cat input | wu-local count
  283

This processor will not output any records until it receives its final
input record.
EOF

      # 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

5 entries across 5 versions & 2 rubygems

Version Path
ul-wukong-4.1.1 lib/wukong/widget/reducers/count.rb
ul-wukong-4.1.0 lib/wukong/widget/reducers/count.rb
wukong-4.0.0 lib/wukong/widget/reducers/count.rb
wukong-3.0.1 lib/wukong/widget/reducers/count.rb
wukong-3.0.0 lib/wukong/widget/reducers/count.rb