Class: SimpleXLS
- Inherits:
-
Object
- Object
- SimpleXLS
- Defined in:
- lib/simplexls.rb
Instance Attribute Summary collapse
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#rows ⇒ Object
Returns the value of attribute rows.
Instance Method Summary collapse
-
#initialize(headers) ⇒ SimpleXLS
constructor
A new instance of SimpleXLS.
- #push(row) ⇒ Object
- #to_s ⇒ Object
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
#headers ⇒ Object
Returns the value of attribute headers.
2 3 4 |
# File 'lib/simplexls.rb', line 2 def headers @headers end |
#rows ⇒ Object
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_s ⇒ Object
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 |