lib/terminal-table/table.rb in visionmedia-terminal-table-1.0.1 vs lib/terminal-table/table.rb in visionmedia-terminal-table-1.0.4
- old
+ new
@@ -27,23 +27,30 @@
# +------------+-----------------+
#
class Table
+ #--
+ # Exceptions
+ #++
+
+ class Error < StandardError; end
+
##
# Table characters, x axis, y axis, and intersection.
X, Y, I = '-', '|', '+'
attr_accessor :headings, :rows
##
# 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
##
# Render the table. Often you will simply call _puts_ with an
# instance in order to output it to the terminal.
@@ -116,11 +123,15 @@
end
##
# 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
##
# Align column +n+ to +alignment+ of :center, :left, or :right.
\ No newline at end of file