Class: RubyCurses::TabularWidget::TableRowSorter

Inherits:
Object
  • Object
show all
Defined in:
lib/rbcurse/core/widgets/tabularwidget.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(model = nil) ⇒ TableRowSorter

Returns a new instance of TableRowSorter.



973
974
975
976
977
# File 'lib/rbcurse/core/widgets/tabularwidget.rb', line 973

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

Instance Attribute Details

#sort_keysObject (readonly)

Returns the value of attribute sort_keys.



972
973
974
# File 'lib/rbcurse/core/widgets/tabularwidget.rb', line 972

def sort_keys
  @sort_keys
end

Instance Method Details

#model=(model) ⇒ Object



978
979
980
981
# File 'lib/rbcurse/core/widgets/tabularwidget.rb', line 978

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

#set_sort_keys(list) ⇒ Object



1033
1034
1035
# File 'lib/rbcurse/core/widgets/tabularwidget.rb', line 1033

def set_sort_keys list
  @sort_keys = list
end

#sortObject

sorts the model based on sort keys and reverse flags



996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
# File 'lib/rbcurse/core/widgets/tabularwidget.rb', line 996

def sort
  return unless @model
  return if @sort_keys.empty?
  $log.debug "TABULAR SORT KEYS #{sort_keys} "
  @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
        res = y[abse] <=> x[abse]
      else
        res = x[e] <=> y[e]
      end
      break if res != 0
    }
    res
  }
end

#sortable(colindex, tf) ⇒ Object



982
983
984
# File 'lib/rbcurse/core/widgets/tabularwidget.rb', line 982

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

#sortable?(colindex) ⇒ Boolean

Returns:

  • (Boolean)


985
986
987
988
# File 'lib/rbcurse/core/widgets/tabularwidget.rb', line 985

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.



1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
# File 'lib/rbcurse/core/widgets/tabularwidget.rb', line 1017

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



990
991
992
# File 'lib/rbcurse/core/widgets/tabularwidget.rb', line 990

def use_to_s colindex
  return true # TODO
end