Class: Redix::Logic

Inherits:
Qt::Object
  • Object
show all
Defined in:
lib/redix/logic.rb

Overview

/

Class Method Summary collapse

Class Method Details

.build_dbsObject



29
30
31
32
33
34
35
36
37
# File 'lib/redix/logic.rb', line 29

def build_dbs
  0.upto(15) do |i|
    a = Qt::Action.new(@u.mainWindow)
    a.connect(SIGNAL('triggered()')) { reconnect(@db = i) }
    # a.objectName("action#{i}")
    @u.menuChange.addAction(a)
    a.text = Qt::Application.translate("MainWindow", "##{i}", nil, Qt::Application::UnicodeUTF8)
  end
end

.build_keysObject



39
40
41
42
43
44
45
46
47
# File 'lib/redix/logic.rb', line 39

def build_keys
  all = r.keys.sort
  @u.listWidget.clear
  @u.listWidget.addItems(all)
  puts r.info
  message "DB Loaded #{all.size} keys. Used memory #{r.info['used_memory_human']}"
rescue
  failure  "Could not conect to redis"
end

.commandObject



108
109
110
111
112
113
114
115
116
117
# File 'lib/redix/logic.rb', line 108

def command
  comm = @u.lineEdit.text
  @u.lineEdit.clear
  puts "Exec #{comm}"
  res = eval("r.#{comm}") rescue "FAIL"
  @u.textBrowser.setHtml("=> #{res}")# += res

rescue Exception => e
  @u.textBrowser.setHtml e.to_s
end

.connectObject



49
50
51
52
53
54
55
56
57
# File 'lib/redix/logic.rb', line 49

def connect
  c = ConnectDialog.new
  c.setupUi
  redis = "#{r.client.host}:#{r.client.port}/#{r.client.db}"
  redis = "#{r.client.password}@" + redis if r.client.password
  c.lineEdit.setText(redis)
  c.buttonBox.connect(SIGNAL('accepted()')) { reconnect(c.lineEdit.text) }
  c.show
end

.create_key(k, type = nil, v = nil) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/redix/logic.rb', line 74

def create_key(k, type = nil, v = nil)

  unless type
    type = k.input_type.current_text
    v = k.input_value.text
    k = k.input_name.text
  end
  case type
  when 'string' then r.set(k, v)
  when 'hash' then r.hmset(k, v)
  when 'list' then r.add(k, v)
  when 'set' then  r.sadd(k, v)
  end
end

.failure(f) ⇒ Object Also known as: message



24
25
26
# File 'lib/redix/logic.rb', line 24

def failure(f)
  @u.statusbar.showMessage(f)
end

.for(u) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/redix/logic.rb', line 89

def for(u)
  @u = u
  build_keys
  build_dbs
  u.actionNew.connect(SIGNAL('triggered()')) { new_key }
  u.actionConnect.connect(SIGNAL('triggered()')) { connect }
  u.actionReconnect.connect(SIGNAL('triggered()')) { reconnect }
  u.actionAbout.connect(SIGNAL('triggered()')) { help }
  u.actionQuit.connect(SIGNAL('triggered()'), App, SLOT('quit()'))

  # Qt::ListWidgetItem.new(u.listWidget)
  # Qt::ListWidgetItem.new(u.listWidget)
  # u.listWidget.item(0).text = "foo"
  u.listWidget.connect(SIGNAL('itemClicked(QListWidgetItem *)')) do |i|
    zoom(i)
  end
  u.lineEdit.connect(SIGNAL('returnPressed()')) { command }
end

.helpObject



59
60
61
62
63
64
65
# File 'lib/redix/logic.rb', line 59

def help
  a = AboutDialog.new
  a.setupUi
  # a.title.setText("RediX")
  a.textBrowser.setHtml("About <a href='http://github.com/nofxx/redix'>Redix</a><p>Marcos Piccinini</p>")
  a.show
end

.new_keyObject



67
68
69
70
71
72
# File 'lib/redix/logic.rb', line 67

def new_key
  k = NewKeyDialog.new
  k.setupUi
  k.buttonBox.connect(SIGNAL('accepted()')) { create_key(k) }
  k.show
end

.r(db = 0) ⇒ Object



9
10
11
12
13
# File 'lib/redix/logic.rb', line 9

def r(db = 0)
  @r ||= Redis.new(:db => db)
rescue
  failure("Can't find redis on localhost:6379")
end

.reconnect(db = @db) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/redix/logic.rb', line 15

def reconnect(db = @db)
  puts "Reconnecting to DB ##{db}"
  params = db =~ /\./ ? { :url => "redis://#{db}" } : { :db => db }
  @r = Redis.connect(params)
  build_keys
rescue
  failure("Failure connecting to #{params}")
end

.zoom(i) ⇒ Object



119
120
121
122
123
124
# File 'lib/redix/logic.rb', line 119

def zoom(i)
  key = i.text
  model = DataModel.new.for(key)
  @u.tableView.model = model
  @u.tableView.setColumnWidth(0, 400)
end