On 14/08/2012, at 2:02 PM, Dmytrii Nagirniak <[email protected]> wrote:
> I wonder what approaches you take to spec-ing ordering.
> Often the ordering doesn't matter much and I tend to skip that part.
>
> But when it is a feature and doesn't map directly to SQLs "ORDER BY" clause
> then it must be spec-ed.
> This is especially the case when a mix of DB sorting + memory sorting should
> be applied.
Hi Dmytrii
It depends on what kind of ordering you're talking about. If you're ordering
after-the-fact, post-SQL query, you could extract your ordering logic into a
class that orders "things" given to it in a specific way:
class OrderedFooList
include Enumerable
def initialise(foos)
@foos = foos
end
def ordered
@ordered ||= order(@foos)
end
def order
@foos.sort_by { |foo| foo.name.to_lower }
end
def each(&block)
ordered.each(&block)
end
end
describe OrderedFooList
let(:shorts) { stub(:name => "Shorts") }
let(:socks) { stub(:name => "socks") }
let(:foos) { [socks, shorts] }
subject { OrderedFooList.new(foos) }
it "should order by lowercase name" do
subject.to_a.should == [shorts, socks]
end
end
I'm unsure if this is overkill or over-engineered for your needs, but from your
message it did sound like you were trying to do something more than a simple
MyActiveRecordModel.order("name ASC").all.
Dave
smime.p7s
Description: S/MIME cryptographic signature
