dimanche 28 août 2016

use memoization in my code

Been told to use memoization in my code not to call the function over and over. Is my implementation best way to use it? It seems redundant. Please advise how could I get rid of the initialize function.

class OrderService
  def initialize
    @current_orders = current_orders
  end

  def orders_acceptance
    @current_orders.
      with_statuses(:acceptance).
      select do |order|
        order.acceptance? if order.shopper_notified_at?
      end
  end

  def orders_start
    @current_orders.
      with_statuses(:start).
      select do |order|
        order.start?
      end
  end


  private

  def current_orders
    @current_orders ||= begin
      Order.includes(:timestamps).
        with_statuses(
          [
            :acceptance,
            :start
          ]
        )
    end
  end
end

Aucun commentaire:

Enregistrer un commentaire