Class: RbFind::Table

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

Instance Method Summary collapse

Constructor Details

#initialize(*heads) ⇒ Table

Returns a new instance of Table.



10
11
12
13
14
# File 'lib/rbfind/table.rb', line 10

def initialize *heads
  heads.flatten!
  @heads = heads
  @rows = []
end

Instance Method Details

#add(*row) ⇒ Object



20
21
22
23
24
25
# File 'lib/rbfind/table.rb', line 20

def add *row
  row.flatten!
  n = @heads.size
  row[ 0, n] = row[ 0, n].map { |r| r.to_s }
  @rows.push row
end

#empty?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/rbfind/table.rb', line 31

def empty?
  @rows.empty?
end

#make_html(table: nil, row: nil) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/rbfind/table.rb', line 58

def make_html table: nil, row: nil
  @html = ""
  tag :table, table, nl: 2 do
    tag :tr, row, nl: 1 do
      (@heads.zip heads_plain).each { |h,c|
        tag :td, c.downcase, align: (html_align h) do @html << c end
      }
    end
    @rows.each { |r|
      tag :tr, table, nl: 1 do
        (@heads.zip heads_plain, r).each { |h,g,c|
          tag :td, g.downcase, align: (html_align h) do @html << c end
        }
      end
    }
  end
  @html
ensure
  @html = nil
end

#make_lines(head: false) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rbfind/table.rb', line 39

def make_lines head: false
  rs = @rows
  rs.unshift heads_plain if head
  w = calc_widths
  rs.each { |r|
    j = (w.zip @heads, r).map { |v,h,c|
      case h
        when />\z/  then c.rjust  v
        when /\^\z/ then c.center v
        when /<?\z/ then c.ljust  v
      end
    }
    l = j.join " "
    l.rstrip!
    yield l
  }
  nil
end

#output(head: false) ⇒ Object



35
36
37
# File 'lib/rbfind/table.rb', line 35

def output head: false
  make_lines head: head do |l| puts l end
end

#sort_by!(*nums) ⇒ Object



27
28
29
# File 'lib/rbfind/table.rb', line 27

def sort_by! *nums
  @rows.sort_by! { |x| nums.map { |i| x[i] } }
end

#spawnObject



16
17
18
# File 'lib/rbfind/table.rb', line 16

def spawn
  self.class.new *@heads
end