forked from aeolus-incubator/thor-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request aeolus-incubator#38 from jistr/output_sorting
Output sorting
- Loading branch information
Showing
13 changed files
with
170 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
module AeolusCli::Formatting | ||
# Sorts array of Presenter objects by their field values. | ||
class PresenterSorter | ||
# Parameter sort_by is expected like e.g. [[:status, :desc], [:name, :asc]], | ||
# which means "sort by status descending, and if status is the same, sort | ||
# by name ascending". | ||
def initialize(presenters, sort_by) | ||
@presenters = presenters | ||
@sort_by = sort_by | ||
end | ||
|
||
def sorted_presenters | ||
return @presenters if @sort_by.nil? || @sort_by.empty? | ||
@presenters.sort do |presenter1, presenter2| | ||
compare(presenter1, presenter2, @sort_by) | ||
end | ||
end | ||
|
||
private | ||
|
||
def compare(presenter1, presenter2, sort_by) | ||
return 0 if sort_by.empty? | ||
next_sort_by = sort_by.dup | ||
field_name, direction = next_sort_by.shift | ||
|
||
current_comparison = | ||
presenter1.send(:field, field_name) <=> presenter2.send(:field, field_name) | ||
|
||
if current_comparison == 0 | ||
compare(presenter1, presenter2, next_sort_by) | ||
else | ||
direction == :desc ? -current_comparison : current_comparison | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
require 'aeolus_cli/formatting/presenter_sorter' | ||
|
||
describe AeolusCli::Formatting::PresenterSorter do | ||
|
||
def stub_fields(presenter, status, owner) | ||
presenter.stub(:field) do |param| | ||
# If I could get local variable by name without using eval, I would :) | ||
# Something like local_variable_get would be handy here. | ||
case param | ||
when :status | ||
status | ||
when :owner | ||
owner | ||
end | ||
end | ||
end | ||
|
||
let(:p1) { double('p1').tap { |p| stub_fields(p, 'stopped', 'alice') } } | ||
let(:p2) { double('p2').tap { |p| stub_fields(p, 'stopped', 'joe') } } | ||
let(:p3) { double('p3').tap { |p| stub_fields(p, 'running', 'alice') } } | ||
let(:p4) { double('p4').tap { |p| stub_fields(p, 'running', 'joe') } } | ||
|
||
let(:presenters) { [p4, p1, p3, p2] } | ||
let(:sorted_presenters) { [p1, p2, p3, p4] } | ||
let(:sort_by) { [[:status, :desc], [:owner, :asc]] } | ||
let(:sorter) { AeolusCli::Formatting::PresenterSorter.new(presenters, sort_by) } | ||
|
||
context "#sorted_data" do | ||
subject { sorter.sorted_presenters } | ||
|
||
it { should == sorted_presenters } | ||
end | ||
end |