Skip to content

Commit

Permalink
added a convenient array argument syntax, so you can use: pt Foo.all,…
Browse files Browse the repository at this point in the history
… :id, :name, :another_field
  • Loading branch information
willbryant committed Aug 13, 2009
1 parent 827a3bc commit d6c69d9
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
6 changes: 6 additions & 0 deletions README
Expand Up @@ -59,6 +59,12 @@ Example
| 2 | "Blog the plugin" | nil |
+----+------------------------+---------------+

# There's a convenient equivalent syntax for displaying an ordered list of columns, like :only and :methods:
>> puts Customer.find(31).purchases.to_table :id, :description, :met_due_date?
# which provides:
>> pt Customer.find(31).purchases, :id, :description, :met_due_date?
# resulting in the same output as above.


# If :inspect => false is used, the values will be shown in #to_s form rather than #inspect form:
>> pt Customer.find(31).purchases, :only => [:id, :description, :due_on, :completed_at]
Expand Down
11 changes: 7 additions & 4 deletions lib/table_display.rb
@@ -1,10 +1,13 @@
module TableDisplay
def to_table(options = {})
extra_methods = Array(options.delete(:methods) || []).collect(&:to_s)
def to_table(*args)
options = args.last.is_a?(Hash) ? args.pop : {}
extra_methods = args.length > 0 ? args.collect(&:to_s) : []
extra_methods += Array(options.delete(:methods)).collect(&:to_s) if options[:methods]
only_attributes = Array(options.delete(:only)).collect(&:to_s) if options[:only]
only_attributes ||= [] if args.length > 0
except_attributes = Array(options.delete(:except)).collect(&:to_s) if options[:except]
display_inspect = !options.has_key?(:inspect) || options.delete(:inspect)
raise "unknown options passed to to_table: #{options.keys.to_sentence}" unless options.empty?
display_inspect = options.nil? || !options.has_key?(:inspect) || options.delete(:inspect)
raise "unknown options passed to to_table: #{options.keys.to_sentence}" unless options.blank?

column_lengths = ActiveSupport::OrderedHash.new

Expand Down
22 changes: 22 additions & 0 deletions test/table_display_test.rb
Expand Up @@ -124,6 +124,28 @@ def setup
END
end

test "#to_table accepts an unnamed list of arguments for column names" do
assert_equal <<END.strip, @project.tasks.to_table('id', :due_on, :completed?).join("\n")
+----+------------------+------------+
| id | due_on | completed? |
+----+------------------+------------+
| 1 | Wed, 25 Mar 2009 | true |
| 2 | Sun, 05 Apr 2009 | false |
+----+------------------+------------+
END
end

test "#to_table allows auxiliary named arguments with the array format" do
assert_equal <<END.strip, @project.tasks.to_table('id', :due_on, :completed?, :inspect => false).join("\n")
+----+------------+------------+
| id | due_on | completed? |
+----+------------+------------+
| 1 | 2009-03-25 | true |
| 2 | 2009-04-05 | false |
+----+------------+------------+
END
end

test "#to_table also shows any :methods given as columns" do
assert_equal <<END.strip, @project.tasks.to_table(:methods => [:completed?, 'project_name']).join("\n")
+----+------------+------------------------+------------------+--------------------------------+--------------------------------+--------------------------------+------------+------------------------+
Expand Down

0 comments on commit d6c69d9

Please sign in to comment.