Sha256: c26d638e7a89e19828dffa47697cc80b30d77e190410e7da7b217758cc55dd0d

Contents?: true

Size: 1.66 KB

Versions: 2

Compression:

Stored size: 1.66 KB

Contents

require_relative("group")

module Wukong
  class Processor

    # Concatenates the elements of a group, yielding the group key,
    # the count, and its members.
    #
    # @example Concatenating elements of a group on the command-line.
    #
    #   $ cat input
    #   {"id": 1, "parent_id": 4}
    #   {"id": 2, "parent_id": 3}
    #   {"id": 3, "parent_id": 3}
    #   ...
    #   $ cat input | wu-local group_concat --by=parent_id
    #   4	1	{"id": 1, "parent_id": 4}
    #   3	2	{"id": 2, "parent_id": 3}	{"id": 3, "parent_id": 3}
    #   ...
    #
    # GroupConcat takes all the same options as Group.
    #
    # @see Group
    class GroupConcat < Group

      # The members of the current group.
      attr_accessor :members

      # Initializes the empty members array.
      def setup
        super()
        @members = []
      end

      # Initializes the empty members array.
      #
      # @param [Object] record
      def start record
        super(record)
        self.members = []
      end

      # Accumulate each record, adding it to the current members.
      #
      # @param [Object] record
      def accumulate record
        super(record)
        self.members << record
      end
      
      # Yields the group, including its key, its size, and each
      # member.
      #
      # @yield [key, size, *members]
      # @yieldparam [Object] key the key defining the group
      # @yieldparam [Integer] size the number of members in the group
      # @yieldparam [Array<Object>] the members of the group
      def finalize
        group = [key, size]
        group.concat(members)
        yield group.map(&:to_s).join("\t")
      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/group_concat.rb
wukong-3.0.0.pre2 lib/wukong/widget/reducers/group_concat.rb