Class: SolidusPromotions::EligibilityResults
- Inherits:
-
Object
- Object
- SolidusPromotions::EligibilityResults
- Includes:
- Enumerable
- Defined in:
- app/models/solidus_promotions/eligibility_results.rb
Instance Attribute Summary collapse
-
#promotion ⇒ Object
readonly
Returns the value of attribute promotion.
-
#results ⇒ Object
readonly
Returns the value of attribute results.
Instance Method Summary collapse
- #add(item:, condition:, success:, code:, message:) ⇒ Object
- #each(&block) ⇒ Object
- #error_messages ⇒ Object
-
#initialize(promotion) ⇒ EligibilityResults
constructor
A new instance of EligibilityResults.
- #success? ⇒ Boolean
Constructor Details
#initialize(promotion) ⇒ EligibilityResults
Returns a new instance of EligibilityResults.
8 9 10 11 |
# File 'app/models/solidus_promotions/eligibility_results.rb', line 8 def initialize(promotion) @promotion = promotion @results = [] end |
Instance Attribute Details
#promotion ⇒ Object (readonly)
Returns the value of attribute promotion.
6 7 8 |
# File 'app/models/solidus_promotions/eligibility_results.rb', line 6 def promotion @promotion end |
#results ⇒ Object (readonly)
Returns the value of attribute results.
6 7 8 |
# File 'app/models/solidus_promotions/eligibility_results.rb', line 6 def results @results end |
Instance Method Details
#add(item:, condition:, success:, code:, message:) ⇒ Object
13 14 15 16 17 18 19 20 21 |
# File 'app/models/solidus_promotions/eligibility_results.rb', line 13 def add(item:, condition:, success:, code:, message:) results << EligibilityResult.new( item: item, condition: condition, success: success, code: code, message: ) end |
#each(&block) ⇒ Object
41 42 43 |
# File 'app/models/solidus_promotions/eligibility_results.rb', line 41 def each(&block) results.each(&block) end |
#error_messages ⇒ Object
33 34 35 36 37 38 39 |
# File 'app/models/solidus_promotions/eligibility_results.rb', line 33 def return [] if results.empty? results.group_by(&:condition).map do |_condition, results| next if results.any?(&:success) results.detect { |r| !r.success }&. end.compact end |
#success? ⇒ Boolean
23 24 25 26 27 28 29 30 31 |
# File 'app/models/solidus_promotions/eligibility_results.rb', line 23 def success? return true if results.empty? promotion.benefits.any? do |benefit| benefit.conditions.all? do |condition| results_for_condition = results.select { |result| result.condition == condition } results_for_condition.any?(&:success) end end end |