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