class Lita::Interactors::AddAll

Increases all customers' quantity with the given value or with 1 if nothing is specified.

Constants

DEFAULT_QUANTITY

Attributes

data[R]

Public Class Methods

new(handler, data) click to toggle source
# File lib/lita/interactors/add_all.rb, line 14
def initialize(handler, data)
  @handler = handler
  @data = data
end

Public Instance Methods

perform() click to toggle source
# File lib/lita/interactors/add_all.rb, line 19
def perform
  if service_exists?
    update_all_quantities
  else
    @error = msg_not_found(service_name: name)
  end
  self
end

Private Instance Methods

given_quantity() click to toggle source
# File lib/lita/interactors/add_all.rb, line 34
def given_quantity
  @customer_quantity ||= data[3].to_s
end
increment_quantities() click to toggle source
# File lib/lita/interactors/add_all.rb, line 53
def increment_quantities
  quantity = quantity_calculated
  service[:customers].map do |_key, customer_data|
    customer_data[:quantity] += quantity
  end
end
name() click to toggle source
# File lib/lita/interactors/add_all.rb, line 30
def name
  @name ||= data[1]
end
quantity_calculated() click to toggle source
# File lib/lita/interactors/add_all.rb, line 60
def quantity_calculated
  return given_quantity.to_i unless given_quantity.empty?
  DEFAULT_QUANTITY
end
service() click to toggle source
# File lib/lita/interactors/add_all.rb, line 38
def service
  @service ||= repository.find(name)
end
service_exists?() click to toggle source
# File lib/lita/interactors/add_all.rb, line 42
def service_exists?
  repository.exists?(name)
end
update_all_quantities() click to toggle source
# File lib/lita/interactors/add_all.rb, line 46
def update_all_quantities
  increment_quantities
  repository.update(service)
  @message = I18n.t('lita.handlers.service.add_all.success',
                    quantity: quantity_calculated)
end