Skip to content

Commit

Permalink
- Moved yield or eval to Termina::Table#initialize where it belongs
Browse files Browse the repository at this point in the history
- Added specs for instance eval syntax
- Added specs for block syntax
  • Loading branch information
tj committed Jan 16, 2009
1 parent 6dca6d7 commit 306107e
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 7 deletions.
1 change: 1 addition & 0 deletions examples/examples.rb
Expand Up @@ -54,3 +54,4 @@
rows << [nil, ':)', nil]
rows << [nil, nil, nil]
puts table(nil, rows)

4 changes: 1 addition & 3 deletions lib/terminal-table/import.rb
Expand Up @@ -48,8 +48,6 @@ module Kernel
#

def table headings = [], rows = [], &block
table = Terminal::Table.new :headings => headings, :rows => rows
table.yield_or_eval &block
table
table = Terminal::Table.new :headings => headings, :rows => rows, &block
end
end
19 changes: 15 additions & 4 deletions lib/terminal-table/table.rb
Expand Up @@ -29,6 +29,12 @@ module Terminal

class Table

#--
# Exceptions
#++

class Error < StandardError; end

##
# Table characters, x axis, y axis, and intersection.

Expand All @@ -39,9 +45,10 @@ class Table
##
# Generates a ASCII table.

def initialize options = {}
def initialize options = {}, &block
@headings = options.delete(:headings) || []
@rows = options.delete(:rows) || []
@rows = options.delete(:rows) || []
yield_or_eval &block if block_given?
end

##
Expand Down Expand Up @@ -118,8 +125,12 @@ def length_of_column n
##
# Return total number of columns available.

def number_of_columns
rows[0].length
def number_of_columns
if rows[0]
rows[0].length
else
raise Error, 'Your table needs some rows'
end
end

##
Expand Down
30 changes: 30 additions & 0 deletions spec/table_spec.rb
Expand Up @@ -89,6 +89,36 @@
EOF
end

it "should render properly using block syntax" do
table = Terminal::Table.new do |t|
t << ['a', 1]
t << ['b', 2]
t << ['c', 3]
end
table.render.should == <<-EOF.deindent
+---+---+
| a | 1 |
| b | 2 |
| c | 3 |
+---+---+
EOF
end

it "should render properly using instance_eval block syntax" do
table = Terminal::Table.new do
add_row ['a', 1]
add_row ['b', 2]
add_row ['c', 3]
end
table.render.should == <<-EOF.deindent
+---+---+
| a | 1 |
| b | 2 |
| c | 3 |
+---+---+
EOF
end

it "should allows a hash of options for creation" do
headings = ['Char', 'Num']
rows = [['a', 1], ['b', 2], ['c', 3]]
Expand Down

0 comments on commit 306107e

Please sign in to comment.