Class: TableSetting::Row

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sheet, options = {}) ⇒ Row

Returns a new instance of Row.



4
5
6
7
8
9
10
11
12
# File 'lib/table_setting/row.rb', line 4

def initialize(sheet, options = {})
  @sheet = sheet
  @cells = []
  @sheet.rows.push(self)
  @bold       = options[:bold]       || nil
  @background = options[:background] || nil
  @color      = options[:color]      || nil
  @size       = options[:size]       || nil
end

Instance Attribute Details

#backgroundObject (readonly)

Returns the value of attribute background.



2
3
4
# File 'lib/table_setting/row.rb', line 2

def background
  @background
end

#boldObject (readonly)

Returns the value of attribute bold.



2
3
4
# File 'lib/table_setting/row.rb', line 2

def bold
  @bold
end

#cellsObject

Returns the value of attribute cells.



3
4
5
# File 'lib/table_setting/row.rb', line 3

def cells
  @cells
end

#colorObject (readonly)

Returns the value of attribute color.



2
3
4
# File 'lib/table_setting/row.rb', line 2

def color
  @color
end

#sheetObject (readonly)

Returns the value of attribute sheet.



2
3
4
# File 'lib/table_setting/row.rb', line 2

def sheet
  @sheet
end

#sizeObject (readonly)

Returns the value of attribute size.



2
3
4
# File 'lib/table_setting/row.rb', line 2

def size
  @size
end

Instance Method Details

#add_cells(list) ⇒ Object



49
50
51
# File 'lib/table_setting/row.rb', line 49

def add_cells(list)
  list.map{|contents| self.new_cell(contents)}
end

#bold?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/table_setting/row.rb', line 41

def bold?
  bold
end

#fillObject



73
74
75
76
77
78
79
# File 'lib/table_setting/row.rb', line 73

def fill
  if num_columns < sheet.num_columns and
    !filled?
    filler_columns = sheet.num_columns - num_columns
    filler_cell = self.new_cell('', span: filler_columns).to_html
  end
end

#filled?Boolean

Returns:

  • (Boolean)


66
67
68
69
70
71
# File 'lib/table_setting/row.rb', line 66

def filled?
  cells.each do |cell|
    return true if cell.span == 'all'
  end
  false
end

#new_cell(contents, options = {}) ⇒ Object



45
46
47
# File 'lib/table_setting/row.rb', line 45

def new_cell(contents, options = {})
  TableSetting::Cell.new(self, contents, options)
end

#num_columnsObject



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/table_setting/row.rb', line 14

def num_columns
  total_width = 0
  cells.each do |cell|
    width = 1
    if cell.span and 
      cell.span != 'all' and 
      cell.span > 1
      width = cell.span
    end
    total_width += width
  end
  total_width + num_inherited_columns
end

#num_inherited_columnsObject



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/table_setting/row.rb', line 28

def num_inherited_columns
  spanned_columns = 0
  sheet.rows.each do |row|
    row.cells.each do |cell|
      next unless cell.rowspan > 1
      if (sheet.rows.index(row) + (cell.rowspan - 1)) >= sheet.rows.index(self)
        spanned_columns += cell.span
      end
    end
  end
  spanned_columns
end

#to_aObject



53
54
55
# File 'lib/table_setting/row.rb', line 53

def to_a
  cells.map(&:contents)
end

#to_htmlObject



57
58
59
60
61
62
63
64
# File 'lib/table_setting/row.rb', line 57

def to_html
  fill
  <<-HTML
    <tr>
      #{cells.map(&:to_html).join("\n")}
    </tr>
  HTML
end

#to_xlsObject



81
82
83
84
85
86
87
# File 'lib/table_setting/row.rb', line 81

def to_xls
  <<-XML
    <Row>
      #{cells.map(&:to_xls).join}
    </Row>
  XML
end