Class: ActiveCart::OrderTotalCollection

Inherits:
Array
  • Object
show all
Defined in:
lib/active_cart/order_total_collection.rb

Instance Method Summary collapse

Constructor Details

#initialize(cart, *seed) ⇒ OrderTotalCollection

Create a new collection



4
5
6
7
8
9
# File 'lib/active_cart/order_total_collection.rb', line 4

def initialize(cart, *seed)
  @cart = cart
  seed.each do |s|
    self.push(s)
  end
end

Instance Method Details

#+(tc) ⇒ Object

Created a new collection that is the concatenations of the old collection and the supplied collection. The supplied collections can be a normal array.



12
13
14
15
16
17
# File 'lib/active_cart/order_total_collection.rb', line 12

def +(tc)
  tmp = OrderTotalCollection.new(@cart)
  self.each { |s| tmp << s }
  tc.each { |s| tmp << s }
  tmp
end

#insert_after(index, *items) ⇒ Object

Inserts the items after the item that is currently at the supplied index



27
28
29
30
31
# File 'lib/active_cart/order_total_collection.rb', line 27

def insert_after(index, *items)
  items.each_with_index do |item, i|
    self.insert(index + i + 1, item)
  end
end

#insert_before(index, *items) ⇒ Object

Inserts the items before the item that is currently at the supplied index



20
21
22
23
24
# File 'lib/active_cart/order_total_collection.rb', line 20

def insert_before(index, *items)
  items.reverse.each do |item|
    self.insert(index, item)
  end
end

#move(from, to) ⇒ Object

Allows you to reorder the order totals. Moves the item at index from to index to



34
35
36
37
# File 'lib/active_cart/order_total_collection.rb', line 34

def move(from, to)
  index = self.delete_at(from)
  self.insert(to, index)
end

#totalObject

Calculates the total variation caused by the order total objects



40
41
42
# File 'lib/active_cart/order_total_collection.rb', line 40

def total
  self.inject(0) { |t, calculator| t + (calculator.active? ? calculator.price(@cart) : 0) }
end