Class: Pyradise::Cli

Inherits:
Object
  • Object
show all
Extended by:
I18n
Defined in:
lib/pyradise/cli.rb

Constant Summary

Constants included from I18n

I18n::I

Class Method Summary collapse

Methods included from I18n

t

Class Method Details

.clearObject

Wipe all data



69
70
71
72
73
74
# File 'lib/pyradise/cli.rb', line 69

def clear
  print t(:delete)
  return puts unless read_char =~ /y/
  Product.dataset.delete
  puts "\nDB cleared."
end

.fetchObject



16
17
18
# File 'lib/pyradise/cli.rb', line 16

def fetch
  create from_stores
end

.info(sid = nil) ⇒ Object Also known as: show



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/pyradise/cli.rb', line 50

def info(sid=nil)
  if !sid
    puts red("Use: pyradise view <ID>")
  elsif !prod = Product.filter(:sid => sid.to_i).first
    puts yellow("Product not found.")
  else
    w = terminal_size[0] - 20
    prices = prod.prices
    max = prices.values.max.to_f
    prices.keys.sort.each do |k|
      rel = "=" * (prices[k]  / max * w)
      puts "#{Time.at(k).strftime('%M/%d')} #{rel}| #{prices[k]}"
    end
  end
end

.list(*query) ⇒ Object Also known as: search



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/pyradise/cli.rb', line 20

def list(*query)
  t = Time.now
  w = terminal_size[0]
  s = w - 36
  puts "\n#{t(:search)} #{'"' + green(query.join(" ")) + '"' if query[0]}... #{t(:order)}: #{green(Options[:order])}"
  # puts "_" * w
  puts
  prods = Product.filter(:name.like("%#{query.join("%")}%")).order(Options[:order])
  prods.each_with_index do |prod, i|
    name = prod.name.length > s ? prod.name[0..s] + ".. " : prod.name + " "
    #
    # Replace each occurrence with color
    # Need to ensure the length of the string for some reason.. =/
    for q in query
      b = i % 2 == 0 ? "\e[0m\e[2m" : "\e[0m\e[0m"
      replaces = name.scan(/#{q}/i).length
      name.gsub!(/#{q}/i, "\e[32m\\0#{b}")
      name += "." until name.length > w - 23
    end
    total_price = prod.price * Options[:rate] * Options[:tax]
    #
    # Format the output
    out = sprintf("%-6s | %-6s | %-#{s}s %-3d |  R$ %s", prod.store, prod.sid,  name,  prod.price, yellow(total_price.to_i))
    puts i % 2 == 0 ? bold(out) : out
  end
  puts "_" * w
  puts red("Total: #{prods.all.length} (#{Time.now - t}s)")
end

.run!(comm) ⇒ Object



7
8
9
10
11
12
13
14
# File 'lib/pyradise/cli.rb', line 7

def run! comm
  if respond_to? comm[0]
    send(*comm)
  else
    puts "Can't do that..."
    exit
  end
end