Skip to content

Commit

Permalink
StockHolding additions; StockEntity refactoring; rklass method in Orp…
Browse files Browse the repository at this point in the history
…Model
  • Loading branch information
LichP committed Nov 3, 2010
1 parent 1ccb744 commit 1561206
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 31 deletions.
1 change: 1 addition & 0 deletions lib/porp.rb
Expand Up @@ -58,4 +58,5 @@ def self.purge_current_namespace!

require 'porp/orpmodel'
require 'porp/stockentity'
require 'porp/stockholding'
require 'porp/saleentity'
18 changes: 10 additions & 8 deletions lib/porp/orpmodel.rb
Expand Up @@ -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

Expand All @@ -36,25 +40,23 @@ 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}
_#{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
Expand Down
16 changes: 5 additions & 11 deletions lib/porp/stockentity.rb
Expand Up @@ -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
Expand Down
32 changes: 20 additions & 12 deletions lib/porp/stockholding.rb
Expand Up @@ -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
Expand Down

0 comments on commit 1561206

Please sign in to comment.