Class: Cart

Inherits:
Object
  • Object
show all
Defined in:
lib/cart/simple.rb,
lib/cart/config.rb,
lib/cart/advanced.rb

Overview

NOTE: all public methods works with products, not with their IDs

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*items) ⇒ Cart

Returns a new instance of Cart.



35
36
37
38
# File 'lib/cart/simple.rb', line 35

def initialize
  @items = Array.new
  @config = self.class
end

Class Attribute Details

.metadata_modelObject



12
13
14
# File 'lib/cart/config.rb', line 12

def 
  @metadata_model || raise(NotInitializedError)
end

.product_modelObject



8
9
10
# File 'lib/cart/config.rb', line 8

def product_model
  @product_model || raise(NotInitializedError)
end

Instance Attribute Details

#itemsObject

returns array of products



20
21
22
# File 'lib/cart/simple.rb', line 20

def items
  @items.map { |item| @config.product_model.get(item) }
end

Class Method Details

.load(data) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/cart/simple.rb', line 10

def self.load(data)
  cart = Cart.new
  unless data.nil?
    data = YAML::load(data)
    cart.items = data.map { |id| @config.product_model.get(id) }
  end
  return cart
end

.loggerObject



16
17
18
# File 'lib/cart/config.rb', line 16

def logger
  @logger || raise(NotInitializedError)
end

.logger=(logger) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/cart/config.rb', line 20

def logger=(logger)
  if logger.respond_to?(:to_proc)
    @logger = LoggerStub.new(logger)
  elsif logger.respond_to?(:debug)
    @logger = logger
  else
    raise "Given logger must be callable object (Proc or Method for example) or an object with debug method."
  end
end

Instance Method Details

#add(*params_list) ⇒ Object

params_list must be list of hashes or list of @config.metadata_model instances



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/cart/advanced.rb', line 30

def add(product, count = 1)
  raise ArgumentError unless product.kind_of(@config.product_model)
  if item = find(product)
    item.count += count
  else
    struct = CartItem.new
    struct.id = id
    struct.count = count
    @items.push(struct)
  end
end

#countObject



105
106
107
# File 'lib/cart/advanced.rb', line 105

def count
  @items.length
end

#each(&block) ⇒ Object



95
96
97
98
99
# File 'lib/cart/advanced.rb', line 95

def each(&block)
  @items.each do |item|
    block.call(item)
  end
end

#empty?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/cart/simple.rb', line 70

def empty?
  @items.empty?
end

#inspectObject



24
25
26
# File 'lib/cart/simple.rb', line 24

def inspect
  %{<cart @items=#{@config.product_model}#{@items.map { |i| i.id }.inspect}>}
end

#items_equal?(old, new) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/cart/advanced.rb', line 50

def items_equal?(old, new)
  # this is important, we must compare all the properties exclude count
  old = old.dup.tap { |old| old.count = 1 }
  new = new.dup.tap { |new| new.count = 1 }
  # from form empty values goes as "", but deserialized empty items are nil
  properties = new.class.properties.map(&:name)
  properties.each do |property|
    value = new.send(property)
    new.send("#{property}=", nil) if value.respond_to?(:empty?) && value.empty?
  end
  new.eql?(old)
end

#priceObject



77
78
79
# File 'lib/cart/advanced.rb', line 77

def price
  @items.map { |item| item.price }.inject(:+)
end

#price_without_vatObject



85
86
87
# File 'lib/cart/advanced.rb', line 85

def price_without_vat
  @items.map { |item| item.price_without_vat }.inject(:+)
end

#remove(params) ⇒ Object

remove all products with id 1 cart.remove(1) remove 2 products with id 1 cart.remove(1, 2)

Raises:

  • (ArgumentError)


60
61
62
63
64
65
66
67
68
# File 'lib/cart/simple.rb', line 60

def remove(product, count = nil)
  if item = find(product)
    if count.nil? || item.count <= count
      @items.delete(item)
    elsif item.count > count
      item.count = (item.count - count)
    end
  end
end

#saveObject



40
41
42
# File 'lib/cart/simple.rb', line 40

def save
  @items.map { |product| product.id }.to_yaml
end

#vatObject



81
82
83
# File 'lib/cart/advanced.rb', line 81

def vat
  @items.map { |item| item.vat }.inject(:+)
end