Class: CashRegister::ListItem

Inherits:
Object
  • Object
show all
Defined in:
lib/cash_register/list_item.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code, count) ⇒ ListItem

Returns a new instance of ListItem.



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/cash_register/list_item.rb', line 7

def initialize(code, count)
  @deal = Deals.find_by_code(code)
  @count = count
  @item ||= if Promotions.is_two_for_one?(deal.code)
                 free_num = count/3
                 @free_item = FreeItem.new(deal, free_num)
                 Two4OneItem.new(deal, count)
               elsif Promotions.is_discount?(deal.code)
                 DiscountItem.new(deal, count)
               else
                 NormalItem.new(deal, count)
               end
end

Instance Attribute Details

#countObject

Returns the value of attribute count.



4
5
6
# File 'lib/cash_register/list_item.rb', line 4

def count
  @count
end

#dealObject

Returns the value of attribute deal.



4
5
6
# File 'lib/cash_register/list_item.rb', line 4

def deal
  @deal
end

#free_itemObject (readonly)

Returns the value of attribute free_item.



5
6
7
# File 'lib/cash_register/list_item.rb', line 5

def free_item
  @free_item
end

#itemObject (readonly)

Returns the value of attribute item.



5
6
7
# File 'lib/cash_register/list_item.rb', line 5

def item
  @item
end

Instance Method Details

#to_sObject



21
22
23
# File 'lib/cash_register/list_item.rb', line 21

def to_s
  item.to_s
end