Sha256: d0d95cbd4614773c8968ff6eddc8ff9eb047eb680d775f7f605de6a9675dbbc3

Contents?: true

Size: 1.48 KB

Versions: 1

Compression:

Stored size: 1.48 KB

Contents

# frozen_string_literal: true

module Doing
  class WWID
    # Tag methods for WWID class
    module Tags
      ##
      ## 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
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
doing-2.1.40 lib/doing/wwid/tags.rb