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.



37
38
39
40
# File 'lib/cart/simple.rb', line 37

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

Class Attribute Details

.metadata_modelObject



14
15
16
# File 'lib/cart/config.rb', line 14

def 
  @metadata_model || raise(NotInitializedError)
end

.product_modelObject



10
11
12
# File 'lib/cart/config.rb', line 10

def product_model
  @product_model || raise(NotInitializedError)
end

Instance Attribute Details

#itemsObject

returns array of products



22
23
24
# File 'lib/cart/simple.rb', line 22

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

Class Method Details

.load(data) ⇒ Object



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

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



18
19
20
# File 'lib/cart/config.rb', line 18

def logger
  @logger || raise(NotInitializedError)
end

.logger=(logger) ⇒ Object



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

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



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

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
108
109
# File 'lib/cart/advanced.rb', line 105

def count
  @items.inject(0) do |sum, item|
    sum + item.count
  end
end

#each(&block) ⇒ Object



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

def each(&block)
  @items.each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/cart/simple.rb', line 72

def empty?
  @items.empty?
end

#inspectObject



26
27
28
# File 'lib/cart/simple.rb', line 26

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

#items_equal?(one, other) ⇒ Boolean

Returns:

  • (Boolean)


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

def items_equal?(one, other)
  # this is important, we must compare all the properties exclude count
  block = Proc.new { |item| item.count = 1 }
  one.tap(&block) == other.tap(&block)

  # # 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



79
80
81
# File 'lib/cart/advanced.rb', line 79

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

#price_without_vatObject



87
88
89
# File 'lib/cart/advanced.rb', line 87

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)


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

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



42
43
44
# File 'lib/cart/simple.rb', line 42

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

#vatObject



83
84
85
# File 'lib/cart/advanced.rb', line 83

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