Class: DataModel

Inherits:
Wx::ListCtrl
  • Object
show all
Defined in:
lib/MINT-debugger/debugger2.rb

Instance Method Summary collapse

Constructor Details

#initialize(col_labels, modelname, parent) ⇒ DataModel

Returns a new instance of DataModel.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/MINT-debugger/debugger2.rb', line 24

def initialize(col_labels,modelname,parent)
  super(parent, :style => Wx::LC_REPORT | Wx::LC_VIRTUAL)
  @col_labels = col_labels
  @modelname = modelname
  col_labels.each_with_index { |c,i|
    insert_column(i,c,Wx::LIST_FORMAT_LEFT, -1)
    set_column_width(i,175)
  }

  @query = [modelname]
  col_labels.each { 
    @query << nil
  }
  
  read_data(modelname)
  
  #  to fill the data into the list using the on_get_item_text callbacks
  set_item_count @data.size
end

Instance Method Details

#delete_data(items) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/MINT-debugger/debugger2.rb', line 61

def delete_data(items)
  items.sort!{|x,y| y <=> x }
  
  items.each { |i|
    query = { }
    @col_labels.each_with_index do |col,j|
       query[col]=@data[i][j] 
    end
    p "Delete query #{query.inspect}"
    result = @modelname.class.first(query)
    p "Delete result #{result.inspect}"
    result.destroy!
    @data.delete_at(i)
  }
  refresh_data
end

#notfifyNewSize(size) ⇒ Object

used to rearrange columns width’s based on window width



93
94
95
96
97
# File 'lib/MINT-debugger/debugger2.rb', line 93

def notfifyNewSize(size)
  @col_labels.each_with_index { |c,i|
    set_column_width(i,size.get_width/@col_labels.size)
  }
end

#on_get_item_text(item, column) ⇒ Object

use array like indexing of fields in Struct to directly retrieve data for display



87
88
89
90
# File 'lib/MINT-debugger/debugger2.rb', line 87

def on_get_item_text(item, column)
  @data[item][column].to_s

end

#read_data(modelname) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/MINT-debugger/debugger2.rb', line 44

def read_data (modelname)
  
  @data = []
  
  result = modelname.class.all
  result.each do |entry|
     row = []
     @col_labels.each do |col|
       row << entry.method(col.intern).call
     end
     @data << row
   end

   p @data
  @data
end

#refresh_dataObject



78
79
80
81
82
83
# File 'lib/MINT-debugger/debugger2.rb', line 78

def refresh_data
  read_data (@modelname)
  set_item_count @data.size
  refresh_items(0,@data.size)
  p "Refresh to #{@data.size}"
end