Class: Mrtable::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/mrtable.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(header_cols, rows) ⇒ Table

Returns a new instance of Table.



9
10
11
12
# File 'lib/mrtable.rb', line 9

def initialize(header_cols, rows)
  @header_cols = header_cols
  @rows = rows
end

Instance Attribute Details

#header_colsObject (readonly)

Returns the value of attribute header_cols.



7
8
9
# File 'lib/mrtable.rb', line 7

def header_cols
  @header_cols
end

#rowsObject (readonly)

Returns the value of attribute rows.



7
8
9
# File 'lib/mrtable.rb', line 7

def rows
  @rows
end

Instance Method Details

#calc_maxlensObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/mrtable.rb', line 33

def calc_maxlens
  num_cols = @rows[0].size
  maxlens = (0...num_cols).map { |ci|
    cols_at_ci = @rows.map { |cols| cols[ci] }
    if @header_cols
      cols_at_ci << @header_cols[ci]
    end
    cols_at_ci.map { |col|
      Mrtable.col_len(col)
    }.max
  }

  # compatibility for GFM
  min_len = 3
  maxlens.map { |len| [len, min_len].max }
end

#map_col_with_ciObject

ci: column index

Returns:

  • new table



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mrtable.rb', line 16

def map_col_with_ci
  new_rows = @rows.map { |cols|
    new_cols = []
    cols.each_with_index { |col, ci|
      new_cols << yield(col, ci)
    }
    new_cols
  }

  new_header_cols = []
  @header_cols.each_with_index { |col, ci|
    new_header_cols << yield(col, ci)
  }

  Table.new new_header_cols, new_rows
end