Class: Validate::Compare::WithAttributes

Inherits:
Object
  • Object
show all
Includes:
Comparable, TransformUsing
Defined in:
lib/validate/compare.rb

Instance Method Summary collapse

Methods included from TransformUsing

#using

Constructor Details

#initialize(attributes) ⇒ WithAttributes

Returns a new instance of WithAttributes.



22
23
24
# File 'lib/validate/compare.rb', line 22

def initialize(attributes)
  @attributes = attributes
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args) ⇒ Object



35
36
37
38
39
# File 'lib/validate/compare.rb', line 35

def method_missing(symbol, *args)
  return super unless args.empty? && respond_to_missing?(symbol)

  @attributes[symbol]
end

Instance Method Details

#<=>(other) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/validate/compare.rb', line 26

def <=>(other)
  @attributes.each do |attribute, value|
    result = value <=> other.send(attribute)
    return result unless result.zero?
  end

  0
end

#respond_to_missing?(attribute, _ = false) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/validate/compare.rb', line 41

def respond_to_missing?(attribute, _ = false)
  @attributes.include?(attribute)
end

#to_sObject



45
46
47
48
# File 'lib/validate/compare.rb', line 45

def to_s
  '<attributes ' + @attributes.map { |attribute, value| "#{attribute}: #{value}"}
                              .join(', ') + '>'
end