add sorted_column

This commit is contained in:
simon lehericey 2024-09-23 10:35:00 +02:00
parent 693629afc8
commit 305b31e53b
No known key found for this signature in database
GPG key ID: CDE670D827C7B3C5
2 changed files with 44 additions and 0 deletions

View file

@ -0,0 +1,16 @@
# frozen_string_literal: true
class SortedColumn
attr_reader :column
def initialize(column:, order:)
@column = column
@order = order
end
def ascending? = @order == 'asc'
def ==(other)
other&.column == column && other.order == order
end
end

View file

@ -0,0 +1,28 @@
# frozen_string_literal: true
describe SortedColumn do
let(:column) { Column.new(procedure_id: 1, table: 'table', column: 'column') }
let(:sorted_column) { SortedColumn.new(column: column, order: 'asc') }
describe '==' do
it 'returns true for the same sorted column' do
other = SortedColumn.new(column: column, order: 'asc')
expect(sorted_column == other).to eq(true)
end
it 'returns false if the order is different' do
other = SortedColumn.new(column: column, order: 'desc')
expect(sorted_column == other).to eq(false)
end
it 'returns false if the column is different' do
other_column = Column.new(procedure_id: 1, table: 'table', column: 'other')
other = SortedColumn.new(column: other_column, order: 'asc')
expect(sorted_column == other).to eq(false)
end
it 'returns false if the other is nil' do
expect(sorted_column == nil).to eq(false)
end
end
end