Sha256: 9bd6822ae9f88686a2b90b774fe518f8a81e966a2d69d9b86a27e4a94de740da

Contents?: true

Size: 1.34 KB

Versions: 39

Compression:

Stored size: 1.34 KB

Contents

# frozen_string_literal: true

module Doing
  class WWID
    ##
    ## List all tags that exist on given items
    ##
    ## @param      items   [Array] array of Item
    ## @param      opt     [Hash] additional options
    ## @param      counts  [Boolean] Include tag counts in
    ##                     results
    ##
    ## @return     [Hash] if counts is true, returns a hash with {
    ##             tag: count }.
    ## @return     [Array] If counts is false, returns a simple
    ##             array of tags.
    ##
    def all_tags(items, opt: {}, counts: false)
      if counts
        all_tags = {}
        items.each do |item|
          item.tags.each do |tag|
            if all_tags.key?(tag.downcase)
              all_tags[tag.downcase] += 1
            else
              all_tags[tag.downcase] = 1
            end
          end
        end

        all_tags.sort_by { |_, count| count }
      else
        all_tags = []
        items.each { |item| all_tags.concat(item.tags.map(&:downcase)).uniq! }
        all_tags.sort
      end
    end

    def tag_groups(items, opt: {})
      all_items = filter_items(items, opt: opt)
      tags = all_tags(all_items, opt: {})
      groups = {}
      tags.each do |tag|
        groups[tag] ||= []
        groups[tag] = filter_items(all_items, opt: { tag: tag, tag_bool: :or })
      end

      groups
    end
  end
end

Version data entries

39 entries across 39 versions & 1 rubygems

Version Path
doing-2.1.86 lib/doing/wwid/tags.rb
doing-2.1.85 lib/doing/wwid/tags.rb
doing-2.1.84 lib/doing/wwid/tags.rb
doing-2.1.83 lib/doing/wwid/tags.rb
doing-2.1.82 lib/doing/wwid/tags.rb
doing-2.1.81 lib/doing/wwid/tags.rb
doing-2.1.80 lib/doing/wwid/tags.rb
doing-2.1.79 lib/doing/wwid/tags.rb
doing-2.1.78 lib/doing/wwid/tags.rb
doing-2.1.77 lib/doing/wwid/tags.rb
doing-2.1.76 lib/doing/wwid/tags.rb
doing-2.1.75 lib/doing/wwid/tags.rb
doing-2.1.74 lib/doing/wwid/tags.rb
doing-2.1.73 lib/doing/wwid/tags.rb
doing-2.1.72 lib/doing/wwid/tags.rb
doing-2.1.69 lib/doing/wwid/tags.rb
doing-2.1.68 lib/doing/wwid/tags.rb
doing-2.1.66 lib/doing/wwid/tags.rb
doing-2.1.65 lib/doing/wwid/tags.rb
doing-2.1.64 lib/doing/wwid/tags.rb