Class: RbFind::Table
- Inherits:
-
Object
- Object
- RbFind::Table
- Defined in:
- lib/rbfind/table.rb
Instance Method Summary collapse
- #add(*row) ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(*heads) ⇒ Table
constructor
A new instance of Table.
- #initialize_copy(oth) ⇒ Object
- #make_html(table: nil, row: nil) ⇒ Object
- #make_lines(head: false) ⇒ Object
- #output(head: false, ifempty: nil) ⇒ Object
- #sort_by!(*nums) ⇒ Object
Constructor Details
#initialize(*heads) ⇒ Table
Returns a new instance of Table.
10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/rbfind/table.rb', line 10 def initialize *heads heads.flatten! @heads = heads.map { |h| a = case h when />\z/ then +1 when /\^\z/ then 0 when /<?\z/ then -1 end [ $`, a] } @rows = [] end |
Instance Method Details
#add(*row) ⇒ Object
30 31 32 33 34 35 |
# File 'lib/rbfind/table.rb', line 30 def add *row row.flatten! n = @heads.size row.map! { |r| break if n.zero? ; n -= 1 ; r.to_s } @rows.push row end |
#empty? ⇒ Boolean
41 42 43 |
# File 'lib/rbfind/table.rb', line 41 def empty? @rows.empty? end |
#initialize_copy(oth) ⇒ Object
25 26 27 28 |
# File 'lib/rbfind/table.rb', line 25 def initialize_copy oth @heads = oth.heads @rows = [] end |
#make_html(table: nil, row: nil) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/rbfind/table.rb', line 73 def make_html table: nil, row: nil @html = "" tag :table, table, nl: 2 do tag :tr, row, nl: 1 do @heads.each { |(h,a)| tag :td, h.downcase, align: a do @html << h end } end @rows.each { |r| tag :tr, table, nl: 1 do (@heads.zip r).each { |(g,a),c| tag :td, g.downcase, align: a do @html << c end } end } end @html ensure @html = nil end |
#make_lines(head: false) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/rbfind/table.rb', line 53 def make_lines head: false rs = @rows rs.unshift @heads.map { |(h,a)| h } if head w = calc_widths rs.each { |r| j = (w.zip @heads, r).map { |v,(_,a),c| c ||= "" case a when -1 then c.ljust v when 0 then c.center v when +1 then c.rjust v end } l = j.join " " l.rstrip! yield l } nil end |
#output(head: false, ifempty: nil) ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/rbfind/table.rb', line 45 def output head: false, ifempty: nil if empty? and ifempty then puts ifempty return end make_lines head: head do |l| puts l end end |
#sort_by!(*nums) ⇒ Object
37 38 39 |
# File 'lib/rbfind/table.rb', line 37 def sort_by! *nums @rows.sort_by! { |x| nums.map { |i| x[i] } } end |