Class: Promotional::Rule::ItemQuantityPriceRule
- Inherits:
-
PromotionalRule
- Object
- PromotionalRule
- Promotional::Rule::ItemQuantityPriceRule
- Defined in:
- lib/wunder/promotional/rule/item_quantity_price_rule.rb
Constant Summary
Constants included from RuleValidations
RuleValidations::DISCOUNT_TYPES
Instance Attribute Summary collapse
-
#minimum_quantity ⇒ Object
readonly
Returns the value of attribute minimum_quantity.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Attributes inherited from PromotionalRule
#discount_type, #label, #on_item, #rule
Instance Method Summary collapse
- #adjustable?(_total) ⇒ Boolean
- #calculate_discounted_price(basket_item, discount_type) ⇒ Object
- #calculate_total_discounted_price(total, discount_type) ⇒ Object
- #eligible?(item) ⇒ Boolean
-
#initialize(minimum_quantity, value, no_validate = false) ⇒ ItemQuantityPriceRule
constructor
A new instance of ItemQuantityPriceRule.
- #validate ⇒ Object
Methods inherited from PromotionalRule
Methods included from RuleValidations
#boolean?, #check_discount_type, #either_should_be_present, #should_be_a_boolean, #should_be_a_number, #should_be_more_than, #should_be_present
Constructor Details
#initialize(minimum_quantity, value, no_validate = false) ⇒ ItemQuantityPriceRule
Returns a new instance of ItemQuantityPriceRule.
6 7 8 9 10 11 |
# File 'lib/wunder/promotional/rule/item_quantity_price_rule.rb', line 6 def initialize(minimum_quantity, value, no_validate = false) @minimum_quantity = minimum_quantity @value = value validate if no_validate == false end |
Instance Attribute Details
#minimum_quantity ⇒ Object (readonly)
Returns the value of attribute minimum_quantity.
4 5 6 |
# File 'lib/wunder/promotional/rule/item_quantity_price_rule.rb', line 4 def minimum_quantity @minimum_quantity end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
4 5 6 |
# File 'lib/wunder/promotional/rule/item_quantity_price_rule.rb', line 4 def value @value end |
Instance Method Details
#adjustable?(_total) ⇒ Boolean
17 18 19 |
# File 'lib/wunder/promotional/rule/item_quantity_price_rule.rb', line 17 def adjustable?(_total) false end |
#calculate_discounted_price(basket_item, discount_type) ⇒ Object
21 22 23 24 25 26 27 28 |
# File 'lib/wunder/promotional/rule/item_quantity_price_rule.rb', line 21 def calculate_discounted_price(basket_item, discount_type) if discount_type == "percentage" discount = compute_discount(basket_item) basket_item.product.price = discount elsif discount_type == "flat_rate" basket_item.product.price = value end end |
#calculate_total_discounted_price(total, discount_type) ⇒ Object
30 31 32 33 34 35 36 37 38 |
# File 'lib/wunder/promotional/rule/item_quantity_price_rule.rb', line 30 def calculate_total_discounted_price(total, discount_type) if discount_type == "percentage" discount_price = total - ((total * value) / 100) elsif discount_type == "flat_rate" discount_price = total end discount_price end |
#eligible?(item) ⇒ Boolean
13 14 15 |
# File 'lib/wunder/promotional/rule/item_quantity_price_rule.rb', line 13 def eligible?(item) item.quantity >= minimum_quantity end |
#validate ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/wunder/promotional/rule/item_quantity_price_rule.rb', line 40 def validate should_be_present("ItemQuantityPriceRule::Value", value) should_be_a_number("ItemQuantityPriceRule::MinimumQuantity", minimum_quantity) should_be_more_than("ItemQuantityPriceRule::MinQuantity", minimum_quantity, 1) end |