Skip to content

Commit

Permalink
Sort via PresenterSorter when formatting output
Browse files Browse the repository at this point in the history
  • Loading branch information
jistr committed Jan 29, 2013
1 parent be63743 commit f39d027
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 15 deletions.
9 changes: 9 additions & 0 deletions lib/aeolus_cli/formatting/format.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'aeolus_cli/formatting/errors'
require 'aeolus_cli/formatting/presenter_sorter'

module AeolusCli::Formatting
# This class (or more precisely, classes that inherit from this one) can
Expand Down Expand Up @@ -30,6 +31,14 @@ def presenter_for(object, fields_override = nil)
presenter_class.new(object, fields_override)
end

# Gets an array of presenters for array of objects. The array sorted
# according to the sort_by parameter. See PresenterSorter for details how
# sorting works.
def presenters_for(objects, fields_override = nil, sort_by = nil)
presenters = objects.map { |object| presenter_for(object, fields_override) }
PresenterSorter.new(presenters, sort_by).sorted_presenters
end

# Registers a presenter to use for objects of some type.
def register(class_name, presenter)
@presenters[class_name] = presenter
Expand Down
9 changes: 5 additions & 4 deletions lib/aeolus_cli/formatting/human_format.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@ def detail(object, fields_override = nil)
def list(objects, fields_override = nil, sort_by = nil)
return if objects.empty?

table = []
presenters = presenters_for(objects, fields_override, sort_by)

table = []
# table header
table << presenter_for(objects.first, fields_override).list_table_header
table << presenters.first.list_table_header

objects.each do |object|
table << presenter_for(object, fields_override).list_item
presenters.each do |presenter|
table << presenter.list_item
end

print_table(table)
Expand Down
6 changes: 4 additions & 2 deletions lib/aeolus_cli/formatting/machine_format.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ def detail(object, fields_override = nil)
def list(objects, fields_override = nil, sort_by = nil)
return if objects.empty?

presenters = presenters_for(objects, fields_override, sort_by)

list = []
objects.each do |object|
list << presenter_for(object, fields_override).list_item
presenters.each do |presenter|
list << presenter.list_item
end

print_list(list, @separator)
Expand Down
16 changes: 15 additions & 1 deletion spec/aeolus_cli/formatting/format_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

context "with a presenter" do
let(:presenter) { double('presenter') }
let(:presenter_class) { double('presenter_class') }
let(:presenter_class) { double('presenter_class', :new => presenter) }

before do
format.register("String", presenter_class)
Expand All @@ -32,6 +32,20 @@
subject.should == presenter
end
end

context "#presenters_for" do
let(:sorted_presenters) { double('sorted_presenters') }
let(:sorter) { double('sorter', :sorted_presenters => sorted_presenters) }
before do
AeolusCli::Formatting::PresenterSorter
.should_receive(:new)
.with([presenter, presenter], [:name, :asc])
.and_return(sorter)
end

subject { format.presenters_for(['one', 'two'], nil, [:name, :asc]) }
it { should == sorted_presenters }
end
end

context "printing methods" do
Expand Down
10 changes: 5 additions & 5 deletions spec/aeolus_cli/formatting/human_format_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@

context "printing a list" do
before do
format.stub_chain("presenter_for.list_item")
.and_return(['list', 'item'])
format.stub_chain("presenter_for.list_table_header")
.and_return(['table', 'header'])
format.stub("presenters_for").and_return([
double('first', :list_item => ['list', 'item'], :list_table_header => ['table', 'header']),
double('second', :list_item => ['list2', 'item2']),
])
end

it "prints the data" do
format.should_receive(:print_table).with([
["table", "header"],
["list", "item"],
["list", "item"],
["list2", "item2"],
])

format.list(['a', 'b'])
Expand Down
12 changes: 9 additions & 3 deletions spec/aeolus_cli/formatting/machine_format_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,18 @@

context "printing a list" do
before do
format.stub_chain("presenter_for.list_item")
.and_return(['list', 'item'])
format.stub("presenters_for").and_return([
double('first', :list_item => ['list', 'item']),
double('second', :list_item => ['list2', 'item2']),
])
end

it "prints the data" do
format.should_receive(:print).with("list;item").twice
format.should_receive(:print_list)
.with([
['list', 'item'],
['list2', 'item2'],
], ';')

format.list(['a', 'b'])
end
Expand Down

0 comments on commit f39d027

Please sign in to comment.