Class: Spree::Stock::Quantifier
- Inherits:
-
Object
- Object
- Spree::Stock::Quantifier
- Defined in:
- app/models/spree/stock/quantifier.rb
Instance Attribute Summary collapse
-
#stock_items ⇒ Object
readonly
Returns the value of attribute stock_items.
Instance Method Summary collapse
-
#backorderable? ⇒ Boolean
Checks if any of its stock items are backorderable.
-
#can_supply?(required) ⇒ Boolean
Checks if it is possible to supply a given number of units.
-
#initialize(variant, stock_location_or_id = nil) ⇒ Quantifier
constructor
A new instance of Quantifier.
- #positive_stock ⇒ Object
-
#total_on_hand ⇒ Fixnum
Returns the total number of inventory units on hand for the variant.
Constructor Details
#initialize(variant, stock_location_or_id = nil) ⇒ Quantifier
Returns a new instance of Quantifier.
12 13 14 15 16 |
# File 'app/models/spree/stock/quantifier.rb', line 12 def initialize(variant, stock_location_or_id = nil) @variant = variant @stock_location_or_id = stock_location_or_id @stock_items = variant_stock_items end |
Instance Attribute Details
#stock_items ⇒ Object (readonly)
Returns the value of attribute stock_items.
6 7 8 |
# File 'app/models/spree/stock/quantifier.rb', line 6 def stock_items @stock_items end |
Instance Method Details
#backorderable? ⇒ Boolean
Checks if any of its stock items are backorderable.
33 34 35 |
# File 'app/models/spree/stock/quantifier.rb', line 33 def backorderable? stock_items.any?(&:backorderable) end |
#can_supply?(required) ⇒ Boolean
Checks if it is possible to supply a given number of units.
42 43 44 |
# File 'app/models/spree/stock/quantifier.rb', line 42 def can_supply?(required) total_on_hand >= required || backorderable? end |
#positive_stock ⇒ Object
46 47 48 49 50 51 |
# File 'app/models/spree/stock/quantifier.rb', line 46 def positive_stock return unless stock_location on_hand = stock_location.count_on_hand(variant) on_hand.positive? ? on_hand : 0 end |
#total_on_hand ⇒ Fixnum
Returns the total number of inventory units on hand for the variant.
22 23 24 25 26 27 28 |
# File 'app/models/spree/stock/quantifier.rb', line 22 def total_on_hand if variant.should_track_inventory? stock_items.sum(&:count_on_hand) else Float::INFINITY end end |