Class: DynportTools::AsciiTable
- Inherits:
-
Object
- Object
- DynportTools::AsciiTable
- Defined in:
- lib/dynport_tools/ascii_table.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
- #html2ascii(html) ⇒ Object
- #html_table_cell(text_or_array, node = "td") ⇒ Object
-
#initialize(attributes = {}) ⇒ AsciiTable
constructor
A new instance of AsciiTable.
- #to(format) ⇒ Object
- #to_ascii ⇒ Object
- #to_html ⇒ Object
- #to_tsv ⇒ Object
Constructor Details
#initialize(attributes = {}) ⇒ AsciiTable
Returns a new instance of AsciiTable.
7 8 9 10 |
# File 'lib/dynport_tools/ascii_table.rb', line 7 def initialize(attributes = {}) self.headers = attributes[:headers] || [] self.rows = attributes[:rows] || [] end |
Instance Attribute Details
#headers ⇒ Object
Returns the value of attribute headers.
4 5 6 |
# File 'lib/dynport_tools/ascii_table.rb', line 4 def headers @headers end |
#rows ⇒ Object
Returns the value of attribute rows.
5 6 7 |
# File 'lib/dynport_tools/ascii_table.rb', line 5 def rows @rows end |
Instance Method Details
#html2ascii(html) ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/dynport_tools/ascii_table.rb', line 36 def html2ascii(html) tempfile = Tempfile.new("html2ascii") tempfile.print(html) tempfile.close ascii = Kernel.send(:`, "links -dump #{tempfile.path}") tempfile.delete ascii end |
#html_table_cell(text_or_array, node = "td") ⇒ Object
23 24 25 26 |
# File 'lib/dynport_tools/ascii_table.rb', line 23 def html_table_cell(text_or_array, node = "td") text, = text_or_array "<#{node}#{ ? .map { |key, value| " #{key}=#{value}" }.join("") : ""}>#{text}" end |
#to(format) ⇒ Object
32 33 34 |
# File 'lib/dynport_tools/ascii_table.rb', line 32 def to(format) send(:"to_#{format}") end |
#to_ascii ⇒ Object
28 29 30 |
# File 'lib/dynport_tools/ascii_table.rb', line 28 def to_ascii html2ascii(to_html) end |
#to_html ⇒ Object
16 17 18 19 20 21 |
# File 'lib/dynport_tools/ascii_table.rb', line 16 def to_html html = "<table border=1 align=center>" html << "<tr>" + headers.map { |header| html_table_cell(header, "th") }.join("") if headers.any? html << rows.map { |row| "<tr>" + row.map { |r| html_table_cell(r, "td") }.join("") }.join("") html + "</table>" end |
#to_tsv ⇒ Object
12 13 14 |
# File 'lib/dynport_tools/ascii_table.rb', line 12 def to_tsv ([headers] + rows).map { |line| line.join("\t") }.join("\n") end |