Module: Module::Interface

Defined in:
lib/module/interface.rb

Instance Method Summary collapse

Instance Method Details

#interface(mod) ⇒ void (private)

This method returns an undefined value.

Examples:

module Runnable
  def walk; end
  def run; end
end

class Person
  include Runnable
  extend Module::Interface
  interface Runnable do
    def run; end
  end #=> NotImplementedError "shortage methods: [:walk]"
end

Parameters:

Yield Returns:

  • (self)

26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/module/interface.rb', line 26

def interface(mod)
  wants = mod.instance_methods(false) | mod.private_instance_methods(false)

  yield

  havings = instance_methods(false) | private_instance_methods(false)
  shortages = wants - havings
    
  unless shortages.empty?
    raise NotImplementedError, "shortage methods: #{shortages.inspect}"
  end
  
  self
end