Class: Stock::Entity
- Inherits:
-
Ohm::Model
- Object
- Ohm::Model
- Stock::Entity
- Defined in:
- lib/porp/stock/entity.rb
Overview
The StockEntity class represents physical stock. Quantities of StockEntities are represented by StockHoldings.
Class Method Summary (collapse)
- + (Object) const_missing(name)
-
+ (Object) find_by_plu(plu_value)
Find an entity by PLU.
Instance Method Summary (collapse)
-
- (Object) holding(attrs)
Look up a holding corresponding to the passed attributes.
-
- (Object) lookup_target(target)
Check the supplied target is a MovementTarget.
-
- (Object) move(source_target, dest_target, qty, ucost)
Move stock represented by this entity.
Class Method Details
+ (Object) const_missing(name)
90 91 92 93 94 95 96 |
# File 'lib/porp/stock/entity.rb', line 90 def self.const_missing(name) begin Stock.const_get(name) rescue NameError Object.const_get(name) end end |
+ (Object) find_by_plu(plu_value)
Find an entity by PLU
23 24 25 26 27 28 29 30 |
# File 'lib/porp/stock/entity.rb', line 23 def self.find_by_plu(plu_value) plu = PLU.lookup(plu_value) if !plu.nil? self[plu.stock_entity_id] else nil end end |
Instance Method Details
- (Object) holding(attrs)
Look up a holding corresponding to the passed attributes
36 37 38 39 40 41 42 |
# File 'lib/porp/stock/entity.rb', line 36 def holding(attrs) if attrs.kind_of?(Hash) holdings.find(name: attrs[:holder].to_s + "_" + attrs[:status].to_s).first else nil end end |
- (Object) lookup_target(target)
Check the supplied target is a MovementTarget. If a hash is passed, attempt to find a corresponding holding on this entity
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/porp/stock/entity.rb', line 48 def lookup_target(target) if target.kind_of?(Hash) target = holding(target) end unless target.kind_of?(MovementTarget) raise Orp::MovementInvalidTarget, "#{target} is not a valid MovementTarget" end target end |
- (Object) move(source_target, dest_target, qty, ucost)
Move stock represented by this entity
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/porp/stock/entity.rb', line 72 def move(source_target, dest_target, qty, ucost) # Make sure the supplied target information is valid, looking up holdings # as appropriate source_target = lookup_target(source_target) dest_target = lookup_target(dest_target) # Create and conduct movement movement = Movement.move_no_cleanup(source_target: source_target, source_entity_id: id, source_amount: Amount.new(qty, ucost), dest_target: dest_target, dest_entity_id: id) # Return the completion status of the movement movements << movement movement.completed end |