Class: SimpleMarketplace::Product

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

Constant Summary collapse

@@all =
Hash.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#codeObject (readonly)

Returns the value of attribute code.



3
4
5
# File 'lib/simple_marketplace/product.rb', line 3

def code
  @code
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/simple_marketplace/product.rb', line 3

def name
  @name
end

#priceObject (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_sObject

pretty print



24
25
26
# File 'lib/simple_marketplace/product.rb', line 24

def to_s
  "Product(code: #{@code}, name: #{@name}, price: #{@price})"
end