Class: CompareSupermarkets::Product
- Inherits:
-
Object
- Object
- CompareSupermarkets::Product
- Defined in:
- lib/compare_supermarkets/product.rb
Constant Summary collapse
- @@all =
[]
Instance Attribute Summary collapse
-
#cent_value ⇒ Object
Returns the value of attribute cent_value.
-
#dollar_value ⇒ Object
Returns the value of attribute dollar_value.
-
#name ⇒ Object
Returns the value of attribute name.
-
#price ⇒ Object
Returns the value of attribute price.
-
#supermarket ⇒ Object
Returns the value of attribute supermarket.
-
#unit_size ⇒ Object
Returns the value of attribute unit_size.
-
#url ⇒ Object
Returns the value of attribute url.
Class Method Summary collapse
- .all ⇒ Object
- .all_items_sorted_by_price ⇒ Object
- .all_top_10_sorted_by_price ⇒ Object
- .clear_all ⇒ Object
- .coles_sorted_by_price ⇒ Object
- .count ⇒ Object
- .iga_sorted_by_price ⇒ Object
- .woolworths_sorted_by_price ⇒ Object
Instance Method Summary collapse
-
#initialize(supermarket = nil, name = nil, price = nil, unit_size = nil, url = nil, dollar_value = nil, cent_value = nil) ⇒ Product
constructor
A new instance of Product.
- #supermarket_name ⇒ Object
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_value ⇒ Object
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_value ⇒ Object
Returns the value of attribute dollar_value.
2 3 4 |
# File 'lib/compare_supermarkets/product.rb', line 2 def dollar_value @dollar_value end |
#name ⇒ Object
Returns the value of attribute name.
2 3 4 |
# File 'lib/compare_supermarkets/product.rb', line 2 def name @name end |
#price ⇒ Object
Returns the value of attribute price.
2 3 4 |
# File 'lib/compare_supermarkets/product.rb', line 2 def price @price end |
#supermarket ⇒ Object
Returns the value of attribute supermarket.
2 3 4 |
# File 'lib/compare_supermarkets/product.rb', line 2 def supermarket @supermarket end |
#unit_size ⇒ Object
Returns the value of attribute unit_size.
2 3 4 |
# File 'lib/compare_supermarkets/product.rb', line 2 def unit_size @unit_size end |
#url ⇒ Object
Returns the value of attribute url.
2 3 4 |
# File 'lib/compare_supermarkets/product.rb', line 2 def url @url end |
Class Method Details
.all ⇒ Object
17 18 19 |
# File 'lib/compare_supermarkets/product.rb', line 17 def self.all @@all end |
.all_items_sorted_by_price ⇒ Object
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_price ⇒ Object
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_all ⇒ Object
65 66 67 |
# File 'lib/compare_supermarkets/product.rb', line 65 def self.clear_all @@all.clear end |
.coles_sorted_by_price ⇒ Object
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 |
.count ⇒ Object
21 22 23 |
# File 'lib/compare_supermarkets/product.rb', line 21 def self.count @@all.count end |
.iga_sorted_by_price ⇒ Object
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_price ⇒ Object
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_name ⇒ Object
25 26 27 |
# File 'lib/compare_supermarkets/product.rb', line 25 def supermarket_name self.supermarket ? self.supermarket.name : nil end |