Class: SolidusFriendlyPromotions::EligibilityResults
- Inherits:
-
Object
- Object
- SolidusFriendlyPromotions::EligibilityResults
- Includes:
- Enumerable
- Defined in:
- app/models/solidus_friendly_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.
- #last ⇒ Object
- #success? ⇒ Boolean
Constructor Details
#initialize(promotion) ⇒ EligibilityResults
Returns a new instance of EligibilityResults.
7 8 9 10 |
# File 'app/models/solidus_friendly_promotions/eligibility_results.rb', line 7 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_friendly_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_friendly_promotions/eligibility_results.rb', line 6 def results @results end |
Instance Method Details
#add(item:, condition:, success:, code:, message:) ⇒ Object
12 13 14 15 16 17 18 19 20 |
# File 'app/models/solidus_friendly_promotions/eligibility_results.rb', line 12 def add(item:, condition:, success:, code:, message:) results << EligibilityResult.new( item: item, condition: condition, success: success, code: code, message: ) end |
#each(&block) ⇒ Object
40 41 42 |
# File 'app/models/solidus_friendly_promotions/eligibility_results.rb', line 40 def each(&block) results.each(&block) end |
#error_messages ⇒ Object
32 33 34 35 36 37 38 |
# File 'app/models/solidus_friendly_promotions/eligibility_results.rb', line 32 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 |
#last ⇒ Object
44 45 46 |
# File 'app/models/solidus_friendly_promotions/eligibility_results.rb', line 44 def last results.last end |
#success? ⇒ Boolean
22 23 24 25 26 27 28 29 30 |
# File 'app/models/solidus_friendly_promotions/eligibility_results.rb', line 22 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 |