Permalink
Browse files

Restore original behaviour of Table#align_column; Fix some tests

  • Loading branch information...
rrrene committed Oct 17, 2011
1 parent 9e8940d commit 5e9a7f12d3bc541ff1bfdcabeaf5ae01b6cb666a
Showing with 21 additions and 8 deletions.
  1. +19 −7 lib/terminal-table/cell.rb
  2. +2 −1 lib/terminal-table/table.rb
View
@@ -13,11 +13,6 @@ class Cell
attr_reader :value
- ##
- # Cell alignment.
-
- attr_accessor :alignment
-
##
# Column span.
@@ -29,13 +24,30 @@ class Cell
def initialize options = nil
@value, options = options, {} unless Hash === options
@value = options.fetch :value, value
- @alignment = options.fetch :alignment, :left
+ @alignment = options.fetch :alignment, nil
@colspan = options.fetch :colspan, 1
@width = options.fetch :width, @value.to_s.size
@index = options.fetch :index
@table = options.fetch :table
end
+ def alignment?
+ !@alignment.nil?
+ end
+
+ def alignment
+ @alignment || :left
+ end
+
+ def alignment=(val)
+ supported = %w(left center right)
+ if supported.include?(val.to_s)
+ @alignment = val
+ else
+ raise "Aligment must be one of: #{supported.join(' ')}"
+ end
+ end
+
def lines
@value.to_s.split(/\n/)
end
@@ -44,7 +56,7 @@ def lines
# Render the cell.
def render(line = 0)
- " #{lines[line]} ".align alignment, width + 2
+ " #{lines[line]} ".align(alignment, width + 2)
end
alias :to_s :render
@@ -35,7 +35,8 @@ def initialize options = {}, &block
def align_column n, alignment
r = rows
column(n).each_with_index do |col, i|
- r[i][n].alignment = alignment
+ cell = r[i][n]
+ cell.alignment = alignment unless cell.alignment?
end
end

0 comments on commit 5e9a7f1

Please sign in to comment.