diff --git a/lib/porp.rb b/lib/porp.rb index b9a3827..a2a7645 100755 --- a/lib/porp.rb +++ b/lib/porp.rb @@ -58,4 +58,5 @@ def self.purge_current_namespace! require 'porp/orpmodel' require 'porp/stockentity' +require 'porp/stockholding' require 'porp/saleentity' diff --git a/lib/porp/orpmodel.rb b/lib/porp/orpmodel.rb index 609edda..748ee81 100755 --- a/lib/porp/orpmodel.rb +++ b/lib/porp/orpmodel.rb @@ -19,13 +19,17 @@ def initialize(id) def ==(other) @id.to_s == other.id.to_s end + + # Formats the class name of the object for use in the redis keyspace + def self.rklass + self.name.downcase.match(/(\w+)$/)[0] + end # Fetches the next available id for a new record. This method sets a # record key with this id so effectively creates the record def self.new_id - klass = self.name.downcase - id = redis.incr("#{Porp.ns}:#{klass}:uid") - redis.set("#{Porp.ns}:#{klass}:id:#{id.to_s}:created", 1) + id = redis.incr("#{Porp.ns}:#{rklass}:uid") + redis.set("#{Porp.ns}:#{rklass}:id:#{id.to_s}:created", 1) id end @@ -36,13 +40,11 @@ def self.find_by_id(id) # Checks whether a record exists def self.exists?(id) - klass = self.name.downcase - redis.key?("#{Porp.ns}:#{klass}:id:#{id.to_s}:created") + redis.key?("#{Porp.ns}:#{rklass}:id:#{id.to_s}:created") end # Creates accessor methods for attributes stored as simple values in the db def self.property(*names) - klass = self.name.downcase names.each do |name| self.class_eval <<-EOCE def #{name} @@ -50,11 +52,11 @@ def #{name} end def _#{name} - redis.get("#{Porp.ns}:#{klass}:id:" + id.to_s + ":#{name}") + redis.get("#{Porp.ns}:#{rklass}:id:" + id.to_s + ":#{name}") end def #{name}=(val) - redis.set("#{Porp.ns}:#{klass}:id:" + id.to_s + ":#{name}", val) + redis.set("#{Porp.ns}:#{rklass}:id:" + id.to_s + ":#{name}", val) end EOCE end diff --git a/lib/porp/stockentity.rb b/lib/porp/stockentity.rb index 7bc7e8d..2c0f0c5 100755 --- a/lib/porp/stockentity.rb +++ b/lib/porp/stockentity.rb @@ -43,21 +43,15 @@ def sale_entities end # Associates the StockEntity with a StockHolding - def add_stock_holding(stkh_id) - if StockHolding.exists?(stkh_id) - stkh = StockHolding.new(stkh_id) - # Don't associate with other StockEntities' StockHoldings - raise if stkh.stock_entity != id - redis.sadd("#{Porp.ns}:stockholding:id:#{id}:stockholdings", stkh_id) - else - false - end + def add_new_stock_holding(quantity, cost) + stkh = Porp::StockHolding.create(self, quantity, cost) + redis.sadd("#{Porp.ns}:stockholding:id:#{id}:stockholdings", stkh.id) end # Archive an end-of-life StockHolding - def archive_stock_holding(stkh_id) + def archive_stock_holding(stkh) redis.smove("#{Porp.ns}:stockholding:id:#{id}:stockholdings", - "#{Porp.ns}:stockholding:id:#{id}:archivestockholdings", stkh_id) + "#{Porp.ns}:stockholding:id:#{id}:archivestockholdings", stkh.id) end end end diff --git a/lib/porp/stockholding.rb b/lib/porp/stockholding.rb index 0dd16e3..26ea8bd 100755 --- a/lib/porp/stockholding.rb +++ b/lib/porp/stockholding.rb @@ -22,19 +22,27 @@ class Porp in similar fashion. =end class StockHolding < OrpModel - property :stock_entity + property :stock_entity_id + property :quantity + property :unit_cost - # Creates a new StockHolding record linked to StockEntity with id stke_id - def self.create(stke_id) - if StockEntity.exists?(stke_id) - new_stock_holding = self.new(self.new_id) - new_stock_holding.stock_entity = stke_id - stock_entity = StockEntity.new(stke_id) - stock_entity.add_stock_holding(new_stock_holding.id) - new_stock_holding - else - false - end + # Creates a new StockHolding record linked to StockEntity stke + def self.create(stke, quantity, unit_cost) + new_stkh = self.new(self.new_id) + new_stkh.stock_entity_id = stke.id + new_stkh.quantity = quantity + new_stkh.unit_cost = unit_cost + new_stkh + end + + def stock_entity + Porp::StockEntity.new(stock_entity_id) + end + + # Returns whether the StockHolding is end of life. True when quantity is + # zero (and StockMovements > 0?) + def eol? + quantity == 0 end end end