class Stockr::Part
Constants
- CASH
Attributes
kind[R]
name[R]
pkg[R]
price[R]
qty[R]
Public Class Methods
all()
click to toggle source
# File lib/stockr/part.rb, line 90 def all; search(''); end
create_or_increment(q, name, pr=0)
click to toggle source
# File lib/stockr/part.rb, line 75 def create_or_increment(q, name, pr=0) find_or_create(q, name, pr, true) end
find(txt)
click to toggle source
# File lib/stockr/part.rb, line 96 def find(txt) search(txt, true) end
find_or_create(q, name, pr=0, incr = false)
click to toggle source
# File lib/stockr/part.rb, line 63 def find_or_create(q, name, pr=0, incr = false) part = search(name, true) || new(name) incr ? part.qty += q.to_i : part.qty = q.to_i if pr =~ /\d/ part.price = pr else part.pkg = pr end part.save part end
list(txt = "*")
click to toggle source
# File lib/stockr/part.rb, line 92 def list(txt = "*") search(txt).map(&:line) rescue [] end
missing()
click to toggle source
# File lib/stockr/part.rb, line 100 def missing all.select { |p| p.qty <= 0 } end
new(name, qty = 0, pr = 0)
click to toggle source
# File lib/stockr/part.rb, line 11 def initialize(name, qty = 0, pr = 0) @name = name.upcase @qty = qty.to_i self.price = pr end
search(txt, exact = false)
click to toggle source
# File lib/stockr/part.rb, line 79 def search(txt, exact = false) if res = Store.find(exact ? txt : "*#{txt}*") objs = res.map do |k, r| new(k, r["qty"], r["price"]) end exact ? objs[0] : objs # FIXME: better way? else nil end end
sum(ary = search("*"))
click to toggle source
# File lib/stockr/part.rb, line 59 def sum(ary = search("*")) CASH % ary.reduce(0) { |i, p| i += p.price_total } end
Public Instance Methods
facts()
click to toggle source
# File lib/stockr/part.rb, line 32 def facts out = "#{qty}x #{name} #{pkg} #{CASH} (#{CASH})" % [price, price_total] end
line()
click to toggle source
# File lib/stockr/part.rb, line 22 def line out = "#{qty}#{' ' * (5-qty.to_s.size)}x #{name} #{pkg} " if price && !price.zero? out << ("." * (50 - out.size)) out << CASH % price out << " ($ %.3f)" % (price * qty) #if qty != 1 end out end
pkg=(p)
click to toggle source
# File lib/stockr/part.rb, line 40 def pkg=(p) @pkg = p end
price=(pr)
click to toggle source
# File lib/stockr/part.rb, line 44 def price=(pr) # BigDecimal.new() @price = pr.kind_of?(Numeric) ? pr.to_f : pr.gsub(",", ".").to_f end
price_total()
click to toggle source
# File lib/stockr/part.rb, line 49 def price_total price * qty end
qty=(v)
click to toggle source
# File lib/stockr/part.rb, line 36 def qty=(v) @qty = v end
save()
click to toggle source
# File lib/stockr/part.rb, line 17 def save return false unless name && !name.empty? Store.write(name, { :qty => qty, :price => price, :pkg => pkg }) end
to_json()
click to toggle source
# File lib/stockr/part.rb, line 53 def to_json "{name: '#{name}', qty: '#{qty}', price: '%.3f', total_price: '%.3f'}" % [price, price_total] end