Module: MLB::ComparableByAttribute Private

Included in:
Award, Division, League, Season, Sport
Defined in:
lib/mlb/comparable_by_attribute.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Provides comparison behavior for model classes by a specified attribute. Handles nil values gracefully by placing them at the end when sorting.

Examples:

class Award < Shale::Mapper
  include Comparable
  include MLB::ComparableByAttribute

  def comparable_attribute = :sort_order
end

Instance Method Summary collapse

Instance Method Details

#<=>(other) ⇒ Integer?

Compares this object with another by the comparable attribute

Examples:

award1 <=> award2 #=> -1

Parameters:

  • other (Object)

    the object to compare with

Returns:

  • (Integer, nil)

    -1, 0, 1, or nil if not comparable



21
22
23
24
25
26
27
28
# File 'lib/mlb/comparable_by_attribute.rb', line 21

def <=>(other)
  return nil unless other.is_a?(self.class) || is_a?(other.class)

  compare_values(
    public_send(comparable_attribute),
    other.public_send(comparable_attribute)
  )
end