Class: RubyCurses::DefaultTableRowSorter

Inherits:
Object
  • Object
show all
Defined in:
lib/rbcurse/experimental/widgets/tablewidget.rb

Overview

This is our default table row sorter. It does a multiple sort and allows for reverse sort also. It’s a pretty simple sorter and uses sort, not sort_by. Improvements welcome. Usage: provide model in constructor or using model method Call toggle_sort_order(column_index) Call sort. Currently, this sorts the provided model in-place. Future versions may maintain a copy, or use a table that provides a mapping of model to result. # TODO check if column_sortable

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data_model = nil) ⇒ DefaultTableRowSorter

model is array of data



83
84
85
86
87
# File 'lib/rbcurse/experimental/widgets/tablewidget.rb', line 83

def initialize data_model=nil
  self.model = data_model
  @columns_sort = []
  @sort_keys = nil
end

Instance Attribute Details

#sort_keysObject (readonly)

Returns the value of attribute sort_keys.



81
82
83
# File 'lib/rbcurse/experimental/widgets/tablewidget.rb', line 81

def sort_keys
  @sort_keys
end

Instance Method Details

#model=(model) ⇒ Object



88
89
90
91
# File 'lib/rbcurse/experimental/widgets/tablewidget.rb', line 88

def model=(model)
  @model = model
  @sort_keys = nil
end

#set_sort_keys(list) ⇒ Object



176
177
178
# File 'lib/rbcurse/experimental/widgets/tablewidget.rb', line 176

def set_sort_keys list
  @sort_keys = list
end

#sortObject

sorts the model based on sort keys and reverse flags



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/rbcurse/experimental/widgets/tablewidget.rb', line 106

def sort
  return unless @model
  return if @sort_keys.empty?
  $log.debug "TABULAR SORT KEYS #{sort_keys} "
  # first row is the header which should remain in place
  # We could have kept column headers separate, but then too much of mucking around
  # with textpad, this way we avoid touching it
  header = @model.delete_at 0
  begin
    # next line often can give error "array within array" - i think on date fields that 
    #  contain nils
  @model.sort!{|x,y| 
    res = 0
    @sort_keys.each { |ee| 
      e = ee.abs-1 # since we had offsetted by 1 earlier
      abse = e.abs
      if ee < 0
        xx = x[abse]
        yy = y[abse]
        # the following checks are since nil values cause an error to be raised
        if xx.nil? && yy.nil?
          res = 0
        elsif xx.nil?
          res = 1
        elsif yy.nil?
          res = -1
        else
        res = y[abse] <=> x[abse]
        end
      else
        xx = x[e]
        yy = y[e]
        # the following checks are since nil values cause an error to be raised
        # whereas we want a nil to be wither treated as a zero or a blank
        if xx.nil? && yy.nil?
          res = 0
        elsif xx.nil?
          res = -1
        elsif yy.nil?
          res = 1
        else
        res = x[e] <=> y[e]
        end
      end
      break if res != 0
    }
    res
  }
  ensure
    @model.insert 0, header if header
  end
end

#sortable(colindex, tf) ⇒ Object



92
93
94
# File 'lib/rbcurse/experimental/widgets/tablewidget.rb', line 92

def sortable colindex, tf
  @columns_sort[colindex] = tf
end

#sortable?(colindex) ⇒ Boolean

Returns:

  • (Boolean)


95
96
97
98
# File 'lib/rbcurse/experimental/widgets/tablewidget.rb', line 95

def sortable? colindex
  return false if @columns_sort[colindex]==false
  return true
end

#toggle_sort_order(index) ⇒ Object

toggle the sort order if given column offset is primary sort key Otherwise, insert as primary sort key, ascending.



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/rbcurse/experimental/widgets/tablewidget.rb', line 160

def toggle_sort_order index
  index += 1 # increase by 1, since 0 won't multiple by -1
  # internally, reverse sort is maintained by multiplying number by -1
  @sort_keys ||= []
  if @sort_keys.first && index == @sort_keys.first.abs
    @sort_keys[0] *= -1 
  else
    @sort_keys.delete index # in case its already there
    @sort_keys.delete(index*-1) # in case its already there
    @sort_keys.unshift index
    # don't let it go on increasing
    if @sort_keys.size > 3
      @sort_keys.pop
    end
  end
end

#use_to_s(colindex) ⇒ Object

should to_s be used for this column



100
101
102
# File 'lib/rbcurse/experimental/widgets/tablewidget.rb', line 100

def use_to_s colindex
  return true # TODO
end