Permalink
Browse files

Remove method_missing from Row and provide real implementation

  • Loading branch information...
1 parent d8667ac commit 0921de4338f018e9d0bb7ca5ce0bebd6b4fd2829 @rrrene rrrene committed Oct 19, 2011
Showing with 24 additions and 28 deletions.
  1. +19 −24 lib/terminal-table/row.rb
  2. +5 −4 lib/terminal-table/table.rb
View
@@ -1,7 +1,6 @@
module Terminal
class Table
class Row
- include Enumerable
##
# Row cells
@@ -14,42 +13,38 @@ class Row
# Initialize with _width_ and _options_.
def initialize table, array
+ @cell_index = 0
@table = table
- @cells = if array == :separator
- array
+ if array == :separator
+ @cells = array
else
- index = 0
- array.map do |item|
- options = item.is_a?(Hash) ? item : {:value => item}
- cell = Cell.new(options.merge(:index => index, :table => @table))
- index += cell.colspan
- cell
- end
+ @cells = []
+ array.each { |item| self << item }
end
end
- def [] index
- cells[index] unless separator?
+ def add_cell item
+ options = item.is_a?(Hash) ? item : {:value => item}
+ cell = Cell.new(options.merge(:index => @cell_index, :table => @table))
+ @cell_index += cell.colspan
+ @cells << cell
end
+ alias << add_cell
- def []= index, value
- cells[index] = value
- end
-
- def each &block
- cells.each &block unless separator?
+ def [] index
+ cells[index] unless separator?
end
def height
cells.map { |c| c.lines.count }.max
end
- def method_missing m, *args, &block
- if cells.respond_to?(m)
- cells.__send__(m, *args, &block)
- else
- super
- end
+ def empty?
+ cells.empty?
+ end
+
+ def size
+ cells.size
end
def render
@@ -32,8 +32,9 @@ def align_column n, alignment
# Add a row.
def add_row array
- @rows << Row.new(self, array)
- recalc_column_widths @rows.last
+ row = array == :separator ? Row.new(self, :separator) : Row.new(self, array)
+ @rows << row
+ recalc_column_widths row
end
alias :<< :add_row
@@ -190,9 +191,9 @@ def additional_column_widths
end
def recalc_column_widths row
- if row.is_a?(Symbol) then return end
+ return if row.separator?
i = 0
- row.each do |cell|
+ row.cells.each do |cell|
colspan = cell.colspan
cell_value = cell.value_for_column_width_recalc
colspan.downto(1) do |j|

0 comments on commit 0921de4

Please sign in to comment.