module Interface::Helpers

Public Instance Methods

must_implement(*args) click to toggle source

Errors raised here identify the class_name that is not implementing or is implemented with the wrong arity required by the Interface.

# File lib/interface/helpers.rb, line 7
def must_implement(*args)
  parsed_args(args).each do |method, arity|
    raise_interface_error(method, arity) unless valid_method?(method, arity)
  end
end

Private Instance Methods

expected_arity?(method, arity) click to toggle source
# File lib/interface/helpers.rb, line 36
def expected_arity?(method, arity)
  return true unless arity

  method_arity(method) == arity
end
method_arity(method_name) click to toggle source

Arity of the implemented method.

# File lib/interface/helpers.rb, line 43
def method_arity(method_name)
  method(method_name).arity.abs
end
parsed_args(args) click to toggle source

Transform symbol arguments to a hash of nil arity

# File lib/interface/helpers.rb, line 16
def parsed_args(args)
  args.inject({}) do |memo, method|
    memo.merge(method.is_a?(Hash) ? method : { method => nil })
  end
end
raise_interface_error(method_name, method_arity = nil) click to toggle source
# File lib/interface/helpers.rb, line 22
def raise_interface_error(method_name, method_arity = nil)
  raise Interface::Error::NotImplementedError.new(
    class_name: self.class.name,
    method_name: method_name,
    method_arity: method_arity,
    interface_name: self.class.ancestors.first
  )
end
valid_method?(name, arity = nil) click to toggle source

Check if method is implemented and have the specified arity

# File lib/interface/helpers.rb, line 32
def valid_method?(name, arity = nil)
  respond_to?(name) && expected_arity?(name, arity)
end