module Peregrine::Collections::Tagged

Provides methods for filtering collections by item tags. This module is intended to be an extension to existing collection instances.

Public Instance Methods

any_tagged(*list) { |item| ... } click to toggle source

Returns an array of objects within this collection that contain any of the given tags. Yields the tagged items in the collection if a block is given.

# File lib/peregrine/collections/tagged.rb, line 20
def any_tagged(*list)
  items = select do |item|
    next unless item.respond_to?(:tags)
    !(item.tags & list).empty?
  end
  items.each { |item| yield item } if block_given?
  items.extend(Collections)
end
tagged(*list) { |item| ... } click to toggle source

Returns an array of objects within this collection with all of the given tags. Yields the tagged items in the collection if a block is given.

# File lib/peregrine/collections/tagged.rb, line 8
def tagged(*list)
  items = select do |item|
    next unless item.respond_to?(:tags)
    list.all? { |tag| item.tags.include?(tag) }
  end
  items.each { |item| yield item } if block_given?
  items.extend(Collections)
end
untagged() { |item| ... } click to toggle source

Returns an array of objects in this collection which are not tagged. Yields the untagged items in the collection if a block is given.

# File lib/peregrine/collections/tagged.rb, line 31
def untagged
  items = select { |i| i.respond_to?(:tags) ? i.tags.empty? : true }
  items.each { |item| yield item } if block_given?
  items.extend(Collections)
end