Module: Comparability::ComparableBy

Defined in:
lib/comparability/comparable_by.rb

Instance Method Summary collapse

Instance Method Details

#comparable_by(*args) ⇒ Object

Declares the comparison for this class. This defines <=> instance method and includes Comparable module.

Raises:

  • (ArgumentError)


11
12
13
14
15
16
17
# File 'lib/comparability/comparable_by.rb', line 11

def comparable_by(*args)
  raise ArgumentError if args.empty?
  comparator = Comparator.chain(*args)
  const_set(:COMPARATOR, comparator)
  define_method(:<=>) { |other| self.class::COMPARATOR.compare(self, other) }
  include Comparable
end