module InterfaceComparator

Module that checks if two objects has the same interface including public methods list and their arity

Constants

VERSION

Public Class Methods

diff(a, b) click to toggle source

show me the difference between interface of these two objects

# File lib/interface_comparator.rb, line 12
def self.diff(a, b)
  methods_diff = diff_methods(a, b)
  return methods_diff unless methods_diff.empty?
  arity_diff = methods_arity_diff(a, b)
  return arity_diff unless arity_diff.empty?
  []
end
same?(a, b) click to toggle source

is the interface of this two objects are the same?

# File lib/interface_comparator.rb, line 7
def self.same?(a, b)
  diff(a, b).empty?
end

Private Class Methods

arity_hash_factory(method, first, second) click to toggle source
# File lib/interface_comparator.rb, line 55
def self.arity_hash_factory(method, first, second)
  {
    method: method,
    first_object_arity: first,
    second_object_arity: second
  }
end
diff_methods(a, b) click to toggle source
# File lib/interface_comparator.rb, line 20
def self.diff_methods(a, b)
  a_methods = a.public_methods
  b_methods = b.public_methods
  a_minus_b = (a_methods - b_methods).map do |method|
    hash_factory(method, true, false)
  end
  b_minus_a = (b_methods - a_methods).map do |method|
    hash_factory(method, false, true)
  end
  a_minus_b + b_minus_a
end
hash_factory(method, first, second) click to toggle source
# File lib/interface_comparator.rb, line 46
def self.hash_factory(method, first, second)
  {
    method: method,
    first_object: first,
    second_object: second
  }
end
methods_arity_diff(a, b) click to toggle source
# File lib/interface_comparator.rb, line 33
def self.methods_arity_diff(a, b)
  public_methods = a.public_methods
  public_methods.reject! do |method|
    a.method(method).arity == b.method(method).arity
  end
  public_methods.map do |method|
    arity_hash_factory(
      method, a.method(method).arity, b.method(method).arity
    )
  end
end