Class: ActiveCart::OrderTotalCollection
- Inherits:
-
Array
- Object
- Array
- ActiveCart::OrderTotalCollection
- Defined in:
- lib/active_cart/order_total_collection.rb
Instance Method Summary collapse
-
#+(order_total_collection) ⇒ Object
Concatenation.Returns a new OrderTotalCollection built by concatenating the two OrderTotalCollections together to produce a third OrderTotalCollection.
-
#initialize(cart, *seed) ⇒ OrderTotalCollection
constructor
Returns a new OrderTotalCollection.
-
#insert_after(index, *items) ⇒ Object
Inserts the items after the item that is currently at the supplied index.
-
#insert_before(index, *items) ⇒ Object
Inserts the items before the item that is currently at the supplied index.
-
#move(from, to) ⇒ Object
Allows you to reorder the order totals.
-
#total ⇒ Object
Calculates the total price applied by all the order_total objects.
Constructor Details
#initialize(cart, *seed) ⇒ OrderTotalCollection
Returns a new OrderTotalCollection
You must pass in an cart object.
collection = OrderTotalCollection.new(@cart)
You can also pass in an array of order_total objects
cart = ActiveCart::Cart.instance
collection = OrderTotalCollection.new(cart, order_total_1, order_total_2) # => [ order_total_1, order_total_2 ]
14 15 16 17 18 19 |
# File 'lib/active_cart/order_total_collection.rb', line 14 def initialize(cart, *seed) @cart = cart seed.each do |s| self.push(s) end end |
Instance Method Details
#+(order_total_collection) ⇒ Object
Concatenation.Returns a new OrderTotalCollection built by concatenating the two OrderTotalCollections together to produce a third OrderTotalCollection. (The supplied collection can be a regular array)
[ order_total_1, order_total_2, order_total_3 ] + [ order_total_4, order_total_5 ] #=> [ order_total_1, order_total_2, order_total_3, order_total_4, order_total_5 ]
25 26 27 28 29 30 |
# File 'lib/active_cart/order_total_collection.rb', line 25 def +(order_total_collection) tmp = OrderTotalCollection.new(@cart) self.each { |s| tmp << s } order_total_collection.each { |s| tmp << s } tmp end |
#insert_after(index, *items) ⇒ Object
Inserts the items after the item that is currently at the supplied index
items = [ order_total_1, order_total_2 ]
items.insert_after(0, order_total_2) #=> [ order_total_1, order_total_3, order_total_2 ]
49 50 51 52 53 54 |
# File 'lib/active_cart/order_total_collection.rb', line 49 def insert_after(index, *items) items.each_with_index do |item, i| self.insert(index + i + 1, item) end self end |
#insert_before(index, *items) ⇒ Object
Inserts the items before the item that is currently at the supplied index
items = [ order_total_1, order_total_2 ]
items.insert_before(1, order_total_2) #=> [ order_total_1, order_total_3, order_total_2 ]
37 38 39 40 41 42 |
# File 'lib/active_cart/order_total_collection.rb', line 37 def insert_before(index, *items) items.reverse.each do |item| self.insert(index, item) end self end |
#move(from, to) ⇒ Object
Allows you to reorder the order totals. Moves the item at index from to index to
items = [ order_total_1, order_total_2 ]
items.move(0, 1) #=> [ order_total_2, order_total_1 ]
61 62 63 64 |
# File 'lib/active_cart/order_total_collection.rb', line 61 def move(from, to) index = self.delete_at(from) self.insert(to, index) end |
#total ⇒ Object
Calculates the total price applied by all the order_total objects.
order_total_collection = OrderTotalCollection.new(nil, order_total_1, order_total_2)
order_total_collection.total # => 10
71 72 73 |
# File 'lib/active_cart/order_total_collection.rb', line 71 def total self.inject(0) { |t, calculator| t + (calculator.active? ? calculator.price(@cart) : 0) } end |