Class: SimpleXLS

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(headers) ⇒ SimpleXLS

Returns a new instance of SimpleXLS.



4
5
6
7
8
9
# File 'lib/simplexls.rb', line 4

def initialize(headers)
  raise ArugmentError('Row must be an Array') unless headers.is_a?(Array)

  @headers = headers.nil? ? [] : headers.dup
  @rows = []
end

Instance Attribute Details

#headersObject

Returns the value of attribute headers.



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

def headers
  @headers
end

#rowsObject

Returns the value of attribute rows.



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

def rows
  @rows
end

Instance Method Details

#push(row) ⇒ Object



11
12
13
14
# File 'lib/simplexls.rb', line 11

def push(row)
  raise ArugmentError('Row must be an Array') unless row.is_a?(Array)
  @rows.push(row.dup)
end

#to_sObject



16
17
18
19
20
21
22
23
24
25
# File 'lib/simplexls.rb', line 16

def to_s
  output = "<table><thead><tr>"
  output << @headers.collect { |hr| "<th>#{hr}</th>" }.join << "</tr></thead><tbody>"
  output << @rows.collect { |row|
    # even out the row if needed
    row += Array.new(headers.size - row.size) if row.size != headers.size
    "<tr>#{row.collect { |cell| "<td>#{cell}</td>"}.join}</tr>"
  }.join
  output << "</tbody></table>"
end