Sha256: 26ca932903219194882b943e6ecf4160fab493ee96277ab9cc2746a6d76b104d

Contents?: true

Size: 1.06 KB

Versions: 5

Compression:

Stored size: 1.06 KB

Contents

# frozen_string_literal: true

module SolidusFriendlyPromotions
  class EligibilityResults
    include Enumerable
    attr_reader :results, :promotion
    def initialize(promotion)
      @promotion = promotion
      @results = []
    end

    def add(item:, rule:, success:, code:, message:)
      results << EligibilityResult.new(
        item: item,
        rule: rule,
        success: success,
        code: code,
        message: message
      )
    end

    def success?
      return true if results.empty?
      promotion.actions.any? do |action|
        action.relevant_rules.all? do |rule|
          results_for_rule = results.select { |result| result.rule == rule }
          results_for_rule.any?(&:success)
        end
      end
    end

    def error_messages
      return [] if results.empty?
      results.group_by(&:rule).map do |rule, results|
        next if results.any?(&:success)
        results.detect { |r| !r.success }&.message
      end.compact
    end

    def each(&block)
      results.each(&block)
    end

    def last
      results.last
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
solidus_friendly_promotions-1.0.0 app/models/solidus_friendly_promotions/eligibility_results.rb
solidus_friendly_promotions-1.0.0.rc.3 app/models/solidus_friendly_promotions/eligibility_results.rb
solidus_friendly_promotions-1.0.0.rc.2 app/models/solidus_friendly_promotions/eligibility_results.rb
solidus_friendly_promotions-1.0.0.rc.1 app/models/solidus_friendly_promotions/eligibility_results.rb
solidus_friendly_promotions-1.0.0.pre app/models/solidus_friendly_promotions/eligibility_results.rb