module ComparableByAttr
Allow simple definitions for object comparisons
Public Class Methods
attr_compare(*prec)
click to toggle source
attr_compare
allows selection of specific instance variables and order to use within the comparison of objects created off the class
Example:
class IgnoreC include ComparableByAttr attr_compare :a, :b def initialize(a, b, c) @a = a @b = b @c = c end end
Arguments:
*prec = (Optional Precedence Map)
# File lib/comparable_by_attr.rb, line 27 def self.attr_compare(*prec) attr_prec = prec.map do |attr| "@#{attr}".to_sym end @__attr_precedence = attr_prec end
included(klass)
click to toggle source
rubocop:disable Metrics/MethodLength class_eval wrapper
# File lib/comparable_by_attr.rb, line 6 def self.included(klass) klass.class_eval do @__attr_precedence = nil # attr_compare allows selection of specific instance variables and order # to use within the comparison of objects created off the class # # Example: # class IgnoreC # include ComparableByAttr # attr_compare :a, :b # # def initialize(a, b, c) # @a = a # @b = b # @c = c # end # end # # Arguments: # *prec = (Optional Precedence Map) def self.attr_compare(*prec) attr_prec = prec.map do |attr| "@#{attr}".to_sym end @__attr_precedence = attr_prec end def <=>(other) prec = self.class.instance_variable_get(:@__attr_precedence) comp_order = prec || instance_variables.sort this = comp_order.map { |field| instance_variable_get(field) } that = comp_order.map { |field| other.instance_variable_get(field) } this <=> that end end end
Public Instance Methods
<=>(other)
click to toggle source
# File lib/comparable_by_attr.rb, line 34 def <=>(other) prec = self.class.instance_variable_get(:@__attr_precedence) comp_order = prec || instance_variables.sort this = comp_order.map { |field| instance_variable_get(field) } that = comp_order.map { |field| other.instance_variable_get(field) } this <=> that end