Module: Peregrine::Collections::Tagged
- Included in:
- Common
- Defined in:
- lib/peregrine/collections/tagged.rb
Overview
Provides methods for filtering collections by item tags. This module is intended to be an extension to existing collection instances.
Instance Method Summary collapse
-
#any_tagged(*list) ⇒ Object
Returns an array of objects within this collection that contain any of the given tags.
-
#tagged(*list) ⇒ Object
Returns an array of objects within this collection with all of the given tags.
-
#untagged ⇒ Object
Returns an array of objects in this collection which are not tagged.
Instance Method Details
#any_tagged(*list) ⇒ Object
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.
20 21 22 23 24 25 26 27 |
# File 'lib/peregrine/collections/tagged.rb', line 20 def any_tagged(*list) items = select do |item| next unless item.respond_to?(:tags) !(item. & list).empty? end items.each { |item| yield item } if block_given? items.extend(Collections) end |
#tagged(*list) ⇒ Object
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.
8 9 10 11 12 13 14 15 |
# 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..include?(tag) } end items.each { |item| yield item } if block_given? items.extend(Collections) end |
#untagged ⇒ Object
Returns an array of objects in this collection which are not tagged. Yields the untagged items in the collection if a block is given.
31 32 33 34 35 |
# File 'lib/peregrine/collections/tagged.rb', line 31 def untagged items = select { |i| i.respond_to?(:tags) ? i..empty? : true } items.each { |item| yield item } if block_given? items.extend(Collections) end |