Class: RMatrix::PrintTable

Inherits:
Object
  • Object
show all
Defined in:
lib/rmatrix/printing/print_table.rb

Direct Known Subclasses

MatrixTable

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePrintTable

Returns a new instance of PrintTable.



4
5
6
7
8
9
# File 'lib/rmatrix/printing/print_table.rb', line 4

def initialize
  self.row_count = self.column_count = 0
  self.cells = {}
  self.column_justifications = {}
  self.separators = Hash.new(', ')
end

Instance Attribute Details

#cellsObject

Returns the value of attribute cells.



3
4
5
# File 'lib/rmatrix/printing/print_table.rb', line 3

def cells
  @cells
end

#column_countObject

Returns the value of attribute column_count.



3
4
5
# File 'lib/rmatrix/printing/print_table.rb', line 3

def column_count
  @column_count
end

#column_justificationsObject

Returns the value of attribute column_justifications.



3
4
5
# File 'lib/rmatrix/printing/print_table.rb', line 3

def column_justifications
  @column_justifications
end

#row_countObject

Returns the value of attribute row_count.



3
4
5
# File 'lib/rmatrix/printing/print_table.rb', line 3

def row_count
  @row_count
end

#separatorsObject

Returns the value of attribute separators.



3
4
5
# File 'lib/rmatrix/printing/print_table.rb', line 3

def separators
  @separators
end

Instance Method Details

#[](column, row) ⇒ Object



66
67
68
# File 'lib/rmatrix/printing/print_table.rb', line 66

def [](column, row)
  self.cells[[column, row]]
end

#[]=(column, row, value) ⇒ Object



70
71
72
73
74
# File 'lib/rmatrix/printing/print_table.rb', line 70

def []=(column, row, value)
  build_column!(column)
  build_row!(row)
  self.cells[[column, row]] = value
end

#build_column!(idx) ⇒ Object



76
77
78
# File 'lib/rmatrix/printing/print_table.rb', line 76

def build_column!(idx)
  self.column_count = [self.column_count || 0, idx.succ].max
end

#build_row!(idx) ⇒ Object



80
81
82
# File 'lib/rmatrix/printing/print_table.rb', line 80

def build_row!(idx)
  self.row_count = [self.row_count || 0 , idx.succ].max
end

#cell_repr(cell) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/rmatrix/printing/print_table.rb', line 54

def cell_repr(cell)
  case cell
  when nil then ''
  when Numeric then numeric_to_truncated_string(cell)
  else "#{cell}"
  end
end

#column_justification(i) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/rmatrix/printing/print_table.rb', line 27

def column_justification(i)
  case self.column_justifications[i]
  when nil then :right
  when :right then :right
  when :left then :left
  else raise "Unexpected justification for column #{self.column_justifications[i] }"
  end
end

#column_width(column) ⇒ Object



48
49
50
51
52
# File 'lib/rmatrix/printing/print_table.rb', line 48

def column_width(column)
  self.row_count.times.reduce(0) do |agg, row|
    [agg, self.cell_repr(self.cells[[column, row]]).length].max
  end
end

#column_widthsObject



42
43
44
45
46
# File 'lib/rmatrix/printing/print_table.rb', line 42

def column_widths
  column_count.times.map do |i|
    column_width(i)
  end
end

#numeric_to_truncated_string(numeric) ⇒ Object



62
63
64
# File 'lib/rmatrix/printing/print_table.rb', line 62

def numeric_to_truncated_string(numeric)
  numeric.to_s
end

#set_column_separator(column, separator) ⇒ Object



36
37
38
39
40
# File 'lib/rmatrix/printing/print_table.rb', line 36

def set_column_separator(column, separator)
  self.row_count.times do |row|
    self.separators[[column, row]] =  separator
  end
end

#to_sObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rmatrix/printing/print_table.rb', line 11

def to_s
  widths = self.column_widths
  self.row_count.times.map do |row|
    self.column_count.times.flat_map do |column|
      cell_text = cell_repr(self.cells[[column, row]])
      justification = column_justification(column)
      width = widths[column]
      contents = case justification
      when :left  then cell_text.ljust(width)
      when :right then cell_text.rjust(width)
      end
      [contents,self.separators[[column, row]]]
    end[0...-1].join
  end.join("\n")
end