Class: Haversack::ItemCollection
- Inherits:
-
Array
- Object
- Array
- Haversack::ItemCollection
- Defined in:
- lib/haversack/itemcollection.rb
Direct Known Subclasses
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(data, &block) ⇒ ItemCollection
constructor
A new instance of ItemCollection.
- #push(obj) ⇒ Object
- #size ⇒ Object
- #weight ⇒ Object
Constructor Details
#initialize(data, &block) ⇒ ItemCollection
Returns a new instance of ItemCollection.
4 5 6 7 8 9 10 11 12 13 |
# File 'lib/haversack/itemcollection.rb', line 4 def initialize(data, &block) if !block_given? raise ArgumentError, "expected #{data} to contain only elements of class Haversack::Item" unless data.is_a?(Array) && ItemCollection.only_items?(data) end @size = self.size @weight = self.weight block_given? ? super(data, block) : super(data) end |
Class Method Details
Instance Method Details
#push(obj) ⇒ Object
23 24 25 26 |
# File 'lib/haversack/itemcollection.rb', line 23 def push(obj) raise Haversack::KnapsackContentError unless obj.is_a? Haversack::Item super end |
#size ⇒ Object
15 16 17 |
# File 'lib/haversack/itemcollection.rb', line 15 def size map(&:size).sum end |
#weight ⇒ Object
19 20 21 |
# File 'lib/haversack/itemcollection.rb', line 19 def weight map(&:weight).sum end |