Class: Ndd::RSpec::Matchers::BeReverseSortedBy

Inherits:
Object
  • Object
show all
Defined in:
lib/ndd/rspec/matchers/be_reverse_sorted_by.rb

Overview


Ensures that an enumerable (responding to Enumerable#collect) is sorted by the given attribute in reverse order

Examples:

[MyObject.new(my_attribute: 2), MyObject.new(my_attribute: 1)].should be_reverse_sorted_by(:my_attribute) }

Instance Method Summary collapse

Constructor Details

#initialize(attribute = nil) ⇒ BeReverseSortedBy

Returns a new instance of BeReverseSortedBy.



18
19
20
# File 'lib/ndd/rspec/matchers/be_reverse_sorted_by.rb', line 18

def initialize(attribute = nil)
  @attribute = attribute
end

Instance Method Details

#==(other) ⇒ Object



40
41
42
# File 'lib/ndd/rspec/matchers/be_reverse_sorted_by.rb', line 40

def ==(other)
  matches?(other)
end

#failure_message_for_shouldObject



30
31
32
33
34
35
36
37
38
# File 'lib/ndd/rspec/matchers/be_reverse_sorted_by.rb', line 30

def failure_message_for_should
  return <<-MESSAGE

expected '#{@actual.inspect}' to be sorted by '#@attribute' in reverse order
expected attributes: #{@sorted_attributes.inspect}
     got attributes: #{@actual_attributes.inspect}

  MESSAGE
end

#matches?(actual) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
25
26
27
28
# File 'lib/ndd/rspec/matchers/be_reverse_sorted_by.rb', line 22

def matches?(actual)
  @actual = actual
  attributes = actual.collect { |e| e.send(@attribute) }
  @actual_attributes = attributes
  @sorted_attributes = attributes.sort.reverse
  @sorted_attributes == @actual_attributes
end