memory adapter: order list even if one element is nil

did committed Feb 18, 2016
commit 8f1e079e6603b0e0eb230d1e77af3ff0c4fbb923
Showing 2 changed files with 17 additions and 2 deletions
locomotive/steam/adapters/memory/order.rb b/lib/locomotive/steam/adapters/memory/order.rb +2 -2
@@ @@ -51,11 +51,11 @@ module Locomotive::Steam
end
class Asc < Direction
- def <=>(other); @obj <=> other.obj; end
+ def <=>(other) @obj && other.obj ? @obj <=> other.obj : @obj ? -1 : 1; end
end
class Desc < Direction
- def <=>(other); other.obj <=> @obj; end
+ def <=>(other); @obj && other.obj ? other.obj <=> @obj : @obj ? -1 : 1; end
end
end
spec/unit/adapters/memory/order_spec.rb +15 -0
@@ @@ -71,6 +71,21 @@ describe Locomotive::Steam::Adapters::Memory::Order do
it { expect(subject.map(&:id)).to eq([3, 2, 1, 4]) }
+ context 'nil value in the array' do
+
+ let(:array) {
+ [
+ instance_double('Entry1', id: 1, title: 'b', position: 1),
+ instance_double('Entry2', id: 2, title: 'b', position: 2),
+ instance_double('Entry3', id: 3, title: nil, position: 3),
+ instance_double('Entry3', id: 4, title: 'c', position: 1)
+ ]
+ }
+
+ it { expect(subject.map(&:id)).to eq([2, 1, 4, 3]) }
+
+ end
+
end
end