Class: Machine

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(products:, available_change:) ⇒ Machine

Returns a new instance of Machine.



14
15
16
17
# File 'lib/machine/machine.rb', line 14

def initialize(products:, available_change:)
  @products = products
  @available_change = transform_keys_to_cash(available_change)
end

Instance Attribute Details

#available_changeObject

Returns the value of attribute available_change.



12
13
14
# File 'lib/machine/machine.rb', line 12

def available_change
  @available_change
end

#productsObject

Returns the value of attribute products.



12
13
14
# File 'lib/machine/machine.rb', line 12

def products
  @products
end

Instance Method Details

#refill_cash(cash) ⇒ Object



31
32
33
# File 'lib/machine/machine.rb', line 31

def refill_cash(cash)
  reload_cash(cash_refills: cash)
end

#refill_products(products) ⇒ Object



27
28
29
# File 'lib/machine/machine.rb', line 27

def refill_products(products)
  reload_products(product_refills: products)
end

#select_product(product:, coins:) ⇒ Object

Raises:

  • (ArgumentError)


19
20
21
22
23
24
25
# File 'lib/machine/machine.rb', line 19

def select_product(product:, coins:)
  raise ArgumentError unless coins.is_a? Array
  raise NoMoneyError if coins.empty?
  product = products[product]
  raise OutOfSelectedProductError if product.nil? || product[:available].zero?
  find_product(product: product[:product], cash_values: Cash.cash_values(coins))
end