Sha256: a5b39ca4224a5365fe71b32f8d529046e39bdce14a50f81db3a604e67e984448

Contents?: true

Size: 962 Bytes

Versions: 5

Compression:

Stored size: 962 Bytes

Contents

# frozen_string_literal: true

module SolidusPromotions
  class DistributedAmountsHandler
    attr_reader :line_items, :total_amount

    def initialize(line_items, total_amount)
      @line_items = line_items
      @total_amount = total_amount
    end

    # @param line_item [LineItem] one of the line_items distributed over
    # @return [BigDecimal] the weighted adjustment for this line_item
    def amount(line_item)
      distributed_amounts[line_item.id].to_d
    end

    private

    # @private
    # @return [Hash<Integer, BigDecimal>] a hash of line item IDs and their
    #   corresponding weighted adjustments
    def distributed_amounts
      line_item_ids.zip(allocated_amounts).to_h
    end

    def line_item_ids
      line_items.map(&:id)
    end

    def elligible_amounts
      line_items.map(&:discountable_amount)
    end

    def allocated_amounts
      total_amount.to_money.allocate(elligible_amounts).map(&:to_money)
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
solidus_promotions-4.5.1 app/models/solidus_promotions/distributed_amounts_handler.rb
solidus_promotions-4.5.0 app/models/solidus_promotions/distributed_amounts_handler.rb
solidus_promotions-4.4.2 app/models/solidus_promotions/distributed_amounts_handler.rb
solidus_promotions-4.4.1 app/models/solidus_promotions/distributed_amounts_handler.rb
solidus_promotions-4.4.0 app/models/solidus_promotions/distributed_amounts_handler.rb