Module: LLT::Helpers::Equality

Defined in:
lib/llt/helpers/equality.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &blk) ⇒ Object

Intended to be uses as Class Extension. Responds to class methods in form of .equality_of_***_defined_by and takes several params, which define equality operators for the classes instance. These instances then respond to #same_***_as?(object).



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/llt/helpers/equality.rb', line 11

def method_missing(meth, *args, &blk)
  if meth.to_s.match(/^equality_of_(.*)_defined_by/)
    equality_definition = "equality_definition_for_#{$1}"
    define_method(equality_definition) do
      args.map { |arg| send(arg) }
    end

    equality_comparator = "same_#{$1}_as?"
    define_method(equality_comparator) do |other|
      send(equality_definition) == other.send(equality_definition)
    end
  else
    super
  end
end