Sha256: 98ddad34f5afadf34c4c0f34f00d7e4d55d41cfb5792ec8bbcaa17b8721f8f8f

Contents?: true

Size: 977 Bytes

Versions: 2

Compression:

Stored size: 977 Bytes

Contents

# frozen_string_literal: true

class Groupie
  # Group represents a group or category that words can be classified into.
  class Group
    attr_reader :word_counts, :total_word_count

    def initialize(name, groupie)
      @name = name
      @groupie = groupie
      @word_counts = {}
      @total_word_count = 0
    end

    def words
      @word_counts.keys
    end

    # Add new words to the group.
    def add(*words)
      words.flatten.each do |word|
        add_word(word)
      end
      nil
    end

    alias << add

    # Return the count for a specific +word+.
    def count(word)
      @word_counts[word] || 0
    end

    private

    # Add a single word and count it.
    def add_word(word)
      @word_counts[word] ||= 0
      current_count = @word_counts[word] += 1
      @total_word_count += 1
      # If this word is new for this Group, it might be new for the entire Groupie
      @groupie.add_word(word) if current_count == 1
      nil
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
groupie-0.6.0 lib/groupie/group.rb
groupie-0.5.0 lib/groupie/group.rb