Class: CompareSupermarkets::Product

Inherits:
Object
  • Object
show all
Defined in:
lib/compare_supermarkets/product.rb

Constant Summary collapse

@@all =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(supermarket = nil, name = nil, price = nil, unit_size = nil, url = nil, dollar_value = nil, cent_value = nil) ⇒ Product

Returns a new instance of Product.



6
7
8
9
10
11
12
13
14
15
# File 'lib/compare_supermarkets/product.rb', line 6

def initialize (supermarket = nil, name = nil, price=nil, unit_size = nil, url = nil, dollar_value=nil, cent_value=nil)
    @supermarket = supermarket
    @name = name
    @price = price
    @unit_size = unit_size
    @dollar_value = dollar_value
    @cent_value = cent_value
    @url = url
    @@all << self
end

Instance Attribute Details

#cent_valueObject

Returns the value of attribute cent_value.



2
3
4
# File 'lib/compare_supermarkets/product.rb', line 2

def cent_value
  @cent_value
end

#dollar_valueObject

Returns the value of attribute dollar_value.



2
3
4
# File 'lib/compare_supermarkets/product.rb', line 2

def dollar_value
  @dollar_value
end

#nameObject

Returns the value of attribute name.



2
3
4
# File 'lib/compare_supermarkets/product.rb', line 2

def name
  @name
end

#priceObject

Returns the value of attribute price.



2
3
4
# File 'lib/compare_supermarkets/product.rb', line 2

def price
  @price
end

#supermarketObject

Returns the value of attribute supermarket.



2
3
4
# File 'lib/compare_supermarkets/product.rb', line 2

def supermarket
  @supermarket
end

#unit_sizeObject

Returns the value of attribute unit_size.



2
3
4
# File 'lib/compare_supermarkets/product.rb', line 2

def unit_size
  @unit_size
end

#urlObject

Returns the value of attribute url.



2
3
4
# File 'lib/compare_supermarkets/product.rb', line 2

def url
  @url
end

Class Method Details

.allObject



17
18
19
# File 'lib/compare_supermarkets/product.rb', line 17

def self.all
    @@all
end

.all_items_sorted_by_priceObject



30
31
32
33
34
35
# File 'lib/compare_supermarkets/product.rb', line 30

def self.all_items_sorted_by_price
    sorted = self.all.sort_by! do |s|
        price_to_sort = s.dollar_value + '.' + s.cent_value
        price_to_sort.to_f
    end
end

.all_top_10_sorted_by_priceObject



37
38
39
# File 'lib/compare_supermarkets/product.rb', line 37

def self.all_top_10_sorted_by_price
    self.all_items_sorted_by_price.first(10)
end

.clear_allObject



65
66
67
# File 'lib/compare_supermarkets/product.rb', line 65

def self.clear_all
    @@all.clear
end

.coles_sorted_by_priceObject



41
42
43
44
45
46
47
# File 'lib/compare_supermarkets/product.rb', line 41

def self.coles_sorted_by_price
    coles_items = self.all.select{|product| product.supermarket_name == "Coles"}
    coles_items.sort_by! do |s|
    price_to_sort = s.dollar_value + '.' + s.cent_value
    price_to_sort.to_f
    end
end

.countObject



21
22
23
# File 'lib/compare_supermarkets/product.rb', line 21

def self.count
    @@all.count
end

.iga_sorted_by_priceObject



57
58
59
60
61
62
63
# File 'lib/compare_supermarkets/product.rb', line 57

def self.iga_sorted_by_price
    iga_items = self.all.select{|product| product.supermarket_name == "IGA"}
    iga_items.sort_by! do |s|
        price_to_sort = s.dollar_value + '.' + s.cent_value
        price_to_sort.to_f
    end
end

.woolworths_sorted_by_priceObject



49
50
51
52
53
54
55
# File 'lib/compare_supermarkets/product.rb', line 49

def self.woolworths_sorted_by_price
    woolworths_items = self.all.select{|product| product.supermarket_name == "Woolworths"}
    woolworths_items.sort_by! do |s|
        price_to_sort = s.dollar_value + '.' + s.cent_value
        price_to_sort.to_f
    end
end

Instance Method Details

#supermarket_nameObject



25
26
27
# File 'lib/compare_supermarkets/product.rb', line 25

def supermarket_name
    self.supermarket ? self.supermarket.name : nil
end