diff --git a/lib/aeolus_cli/formatting/format.rb b/lib/aeolus_cli/formatting/format.rb index a6da254..1e587a5 100644 --- a/lib/aeolus_cli/formatting/format.rb +++ b/lib/aeolus_cli/formatting/format.rb @@ -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 @@ -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 diff --git a/lib/aeolus_cli/formatting/human_format.rb b/lib/aeolus_cli/formatting/human_format.rb index 9694d30..00606f4 100644 --- a/lib/aeolus_cli/formatting/human_format.rb +++ b/lib/aeolus_cli/formatting/human_format.rb @@ -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) diff --git a/lib/aeolus_cli/formatting/machine_format.rb b/lib/aeolus_cli/formatting/machine_format.rb index 78087fd..41ba16f 100644 --- a/lib/aeolus_cli/formatting/machine_format.rb +++ b/lib/aeolus_cli/formatting/machine_format.rb @@ -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) diff --git a/spec/aeolus_cli/formatting/format_spec.rb b/spec/aeolus_cli/formatting/format_spec.rb index 07ec127..2c4bf5a 100644 --- a/spec/aeolus_cli/formatting/format_spec.rb +++ b/spec/aeolus_cli/formatting/format_spec.rb @@ -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) @@ -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 diff --git a/spec/aeolus_cli/formatting/human_format_spec.rb b/spec/aeolus_cli/formatting/human_format_spec.rb index 65308d4..cb8ed9d 100644 --- a/spec/aeolus_cli/formatting/human_format_spec.rb +++ b/spec/aeolus_cli/formatting/human_format_spec.rb @@ -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']) diff --git a/spec/aeolus_cli/formatting/machine_format_spec.rb b/spec/aeolus_cli/formatting/machine_format_spec.rb index 9da8f45..f538d56 100644 --- a/spec/aeolus_cli/formatting/machine_format_spec.rb +++ b/spec/aeolus_cli/formatting/machine_format_spec.rb @@ -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