Class: Munge::Conglomerate::Collection
- Inherits:
-
Object
- Object
- Munge::Conglomerate::Collection
- Includes:
- Enumerable
- Defined in:
- lib/munge/conglomerate/collection.rb
Overview
This class is effectively an Array of Items
Instance Method Summary collapse
- #[](id) ⇒ Item
- #each {|Item| ... } ⇒ Enumerator
- #freeze ⇒ Object
-
#initialize(items:) ⇒ Collection
constructor
A new instance of Collection.
- #push(item) ⇒ void
Constructor Details
#initialize(items:) ⇒ Collection
Returns a new instance of Collection.
9 10 11 12 13 14 |
# File 'lib/munge/conglomerate/collection.rb', line 9 def initialize(items:) @items = items .map { |item| [item.id, item] } .to_h end |
Instance Method Details
#[](id) ⇒ Item
38 39 40 41 42 43 44 |
# File 'lib/munge/conglomerate/collection.rb', line 38 def [](id) if @items.key?(id) @items[id] else raise Error::ItemNotFoundError, id end end |
#each {|Item| ... } ⇒ Enumerator
18 19 20 21 22 23 24 |
# File 'lib/munge/conglomerate/collection.rb', line 18 def each return enum_for(:each) unless block_given? @items.each_value do |item| yield item end end |
#freeze ⇒ Object
46 47 48 49 50 |
# File 'lib/munge/conglomerate/collection.rb', line 46 def freeze @items.freeze super end |
#push(item) ⇒ void
This method returns an undefined value.
28 29 30 31 32 33 34 |
# File 'lib/munge/conglomerate/collection.rb', line 28 def push(item) if @items.key?(item.id) raise Error::DuplicateItemError, item.id else @items[item.id] = item end end |