Class: SimpleMarketplace::Product
- Inherits:
-
Object
- Object
- SimpleMarketplace::Product
- Defined in:
- lib/simple_marketplace/product.rb
Constant Summary collapse
- @@all =
Hash.new
Instance Attribute Summary collapse
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#price ⇒ Object
readonly
Returns the value of attribute price.
Class Method Summary collapse
-
.create(*args) ⇒ Object
Fills the products database.
-
.get_by_code(code) ⇒ Object
returns product with the code.
Instance Method Summary collapse
-
#==(other) ⇒ Object
checks equality.
-
#initialize(code, name, price) ⇒ Product
constructor
A new instance of Product.
-
#to_s ⇒ Object
pretty print.
Constructor Details
#initialize(code, name, price) ⇒ Product
Returns a new instance of Product.
7 8 9 |
# File 'lib/simple_marketplace/product.rb', line 7 def initialize(code, name, price) @code, @name, @price = code, name, price end |
Instance Attribute Details
#code ⇒ Object (readonly)
Returns the value of attribute code.
3 4 5 |
# File 'lib/simple_marketplace/product.rb', line 3 def code @code end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/simple_marketplace/product.rb', line 3 def name @name end |
#price ⇒ Object (readonly)
Returns the value of attribute price.
3 4 5 |
# File 'lib/simple_marketplace/product.rb', line 3 def price @price end |
Class Method Details
.create(*args) ⇒ Object
Fills the products database
12 13 14 15 16 |
# File 'lib/simple_marketplace/product.rb', line 12 def self.create(*args) args.each do |hash| @@all[hash[:code]] = Product.new(hash[:code], hash[:name], hash[:price]) end end |
.get_by_code(code) ⇒ Object
returns product with the code
19 20 21 |
# File 'lib/simple_marketplace/product.rb', line 19 def self.get_by_code(code) @@all[code] end |
Instance Method Details
#==(other) ⇒ Object
checks equality
29 30 31 |
# File 'lib/simple_marketplace/product.rb', line 29 def ==(other) return code == other.code, name == other.name, price: other.price end |
#to_s ⇒ Object
pretty print
24 25 26 |
# File 'lib/simple_marketplace/product.rb', line 24 def to_s "Product(code: #{@code}, name: #{@name}, price: #{@price})" end |