Class: Rediscover::KeyListCtrl

Inherits:
Wx::ListCtrl
  • Object
show all
Includes:
Wx
Defined in:
lib/rediscover/key_list_ctrl.rb

Constant Summary collapse

COLS =
%w(Key Value Type)

Instance Method Summary collapse

Constructor Details

#initialize(window) ⇒ KeyListCtrl

Returns a new instance of KeyListCtrl.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/rediscover/key_list_ctrl.rb', line 7

def initialize(window)
  @redis = get_app.redis
  @logger = get_app.logger

  super(window, :style => LC_REPORT|LC_VIRTUAL)

  i = 0
  COLS.each do |name|
    insert_column(i += 1, name)
  end
  set_column_width(0, 200)
  set_column_width(1, 300)

  evt_list_item_right_click self, :list_item_right_click_evt
  evt_list_item_activated self, :list_item_activated_evt
end

Instance Method Details

#delete(selections) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/rediscover/key_list_ctrl.rb', line 39

def delete(selections)
  selections.each do |index|
    key = @keys[index]
    @redis.delete(key)
  end
  update
end

#do_on_edit(selection) ⇒ Object



110
111
112
113
# File 'lib/rediscover/key_list_ctrl.rb', line 110

def do_on_edit(selection)
  key = @keys[selection]
  @on_edit_block.call(key, @redis.type?(key), selection)
end

#do_on_status_changeObject



120
121
122
# File 'lib/rediscover/key_list_ctrl.rb', line 120

def do_on_status_change
  @on_status_change_block.call("#{size} keys") if @on_status_change_block
end

#get_ctx_menu_for_selections(selections) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/rediscover/key_list_ctrl.rb', line 84

def get_ctx_menu_for_selections(selections)
  return nil if selections.empty?
  @ctx_menu = Menu.new

  if selections.size == 1 && @redis.type?(@keys[*selections]) != 'none'
    @ctx_edit_item = MenuItem.new(@ctx_menu, -1, 'Edit')
    evt_menu(@ctx_edit_item) { do_on_edit(*selections) }
  end

  # delete menu item
  @ctx_delete_item = MenuItem.new(@ctx_menu, -1, 'Delete')
  evt_menu(@ctx_delete_item) do
    if ID_YES == Dialog::Confirm.new(self, 'Really delete selected key(s)?', 'Really delete?').show_modal
      delete(selections)
    end
  end

  @ctx_menu.append_item(@ctx_edit_item) if @ctx_edit_item
  @ctx_menu.append_item(@ctx_delete_item)
  @ctx_menu
end

#get_item_type(item) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/rediscover/key_list_ctrl.rb', line 57

def get_item_type(item)
  if @cached_item.nil? || @cached_item[:index] != item
    @cached_item = {}
    @cached_item[:index] = item
    @cached_item[:type] = @redis.type?(@keys[item])
  end
  @cached_item[:type]
end

#get_item_value(item) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/rediscover/key_list_ctrl.rb', line 66

def get_item_value(item)
  case get_item_type(item)
    when 'string' then return @redis[@keys[item]]
    when 'list' then return "#{@redis.list_length(@keys[item])} element(s)"
    when 'set' then return "#{@redis.set_count(@keys[item])} element(s)"
    when 'zset' then return "#{@redis.zset_count(@keys[item])} element(s)"
  end
end

#list_item_activated_evtObject



75
76
77
# File 'lib/rediscover/key_list_ctrl.rb', line 75

def list_item_activated_evt
  do_on_edit(*get_selections)
end

#list_item_right_click_evt(event) ⇒ Object



79
80
81
82
# File 'lib/rediscover/key_list_ctrl.rb', line 79

def list_item_right_click_evt(event)
  menu = get_ctx_menu_for_selections(get_selections)
  popup_menu(menu, event.get_point) if menu
end

#on_edit(&block) ⇒ Object



106
107
108
# File 'lib/rediscover/key_list_ctrl.rb', line 106

def on_edit(&block)
  @on_edit_block = block
end

#on_get_item_text(item, column) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/rediscover/key_list_ctrl.rb', line 47

def on_get_item_text(item, column)
  text = case column
    when COLS.index('Key') then @keys[item]
    when COLS.index('Value') then get_item_value(item)
    when COLS.index('Type') then get_item_type(item)
  end

  return text || ''
end

#on_get_keys(&block) ⇒ Object



28
29
30
# File 'lib/rediscover/key_list_ctrl.rb', line 28

def on_get_keys(&block)
  @on_get_keys_block = block
end

#on_status_change(&block) ⇒ Object



115
116
117
118
# File 'lib/rediscover/key_list_ctrl.rb', line 115

def on_status_change(&block)
  @on_status_change_block = block
  do_on_status_change
end

#sizeObject



24
25
26
# File 'lib/rediscover/key_list_ctrl.rb', line 24

def size
  @keys.size
end

#updateObject



32
33
34
35
36
37
# File 'lib/rediscover/key_list_ctrl.rb', line 32

def update
  @keys = @on_get_keys_block.call()
  delete_all_items
  set_item_count(size)
  do_on_status_change
end