Class: Stockr::Part

Inherits:
Object
  • Object
show all
Defined in:
lib/stockr/part.rb

Constant Summary collapse

CASH =
"$ %.3f"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, qty = 0, pr = 0) ⇒ Part

Returns a new instance of Part.



11
12
13
14
15
# 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

Instance Attribute Details

#kindObject (readonly)

Returns the value of attribute kind.



7
8
9
# File 'lib/stockr/part.rb', line 7

def kind
  @kind
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/stockr/part.rb', line 7

def name
  @name
end

#pkgObject

Returns the value of attribute pkg.



7
8
9
# File 'lib/stockr/part.rb', line 7

def pkg
  @pkg
end

#priceObject

Returns the value of attribute price.



7
8
9
# File 'lib/stockr/part.rb', line 7

def price
  @price
end

#qtyObject

Returns the value of attribute qty.



7
8
9
# File 'lib/stockr/part.rb', line 7

def qty
  @qty
end

Class Method Details

.allObject



90
# File 'lib/stockr/part.rb', line 90

def all;      search('');    end

.create_or_increment(q, name, pr = 0) ⇒ Object



75
76
77
# 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) ⇒ Object



96
97
98
# File 'lib/stockr/part.rb', line 96

def find(txt)
  search(txt, true)
end

.find_or_create(q, name, pr = 0, incr = false) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
# 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 = "*") ⇒ Object



92
93
94
# File 'lib/stockr/part.rb', line 92

def list(txt = "*")
  search(txt).map(&:line) rescue []
end

.missingObject



100
101
102
# File 'lib/stockr/part.rb', line 100

def missing
  all.select { |p| p.qty <= 0 }
end

.search(txt, exact = false) ⇒ Object



79
80
81
82
83
84
85
86
87
88
# 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("*")) ⇒ Object



59
60
61
# File 'lib/stockr/part.rb', line 59

def sum(ary = search("*"))
  CASH % ary.reduce(0) { |i, p| i += p.price_total }
end

Instance Method Details

#factsObject



32
33
34
# File 'lib/stockr/part.rb', line 32

def facts
  out = "#{qty}x #{name} #{pkg} #{CASH} (#{CASH})" % [price, price_total]
end

#lineObject



22
23
24
25
26
27
28
29
30
# 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

#price_totalObject



49
50
51
# File 'lib/stockr/part.rb', line 49

def price_total
  price * qty
end

#saveObject



17
18
19
20
# 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_jsonObject



53
54
55
# File 'lib/stockr/part.rb', line 53

def to_json
  "{name: '#{name}', qty: '#{qty}', price: '%.3f', total_price: '%.3f'}" % [price, price_total]
end