Module: Flows::Plugin::Interface

Includes:
Util::InheritableSingletonVars::DupStrategy.make_module( '@interface_methods' => {} ), Flows::Util::PrependToClass.make_module do def initialize(*) klass = self.class required_methods = klass.instance_variable_get(:@interface_methods).keys missing_methods = required_methods - methods raise MissingMethodsError.new(klass, missing_methods) if missing_methods.any? super end end
Defined in:
lib/flows/plugin/interface.rb

Overview

Class extension to define Java/C#-like interfaces in Ruby.

On target class initialization will check defined methods for existence.

Currently interface composition is not supported. You cannot define 2 interface modules and include it into one class.

Examples:

Simple interface

class MyAction
  extend Flows::Plugin::Interface

  defmethod :perform
end

class InvalidAction < MyAction; end
InvalidAction.new
# will raise an error

class ValidAction < MyAction
  def perfrom
    puts 'Hello!'
  end
end
ValidAction.new.perform
# => Hello!

Interface as module

module MyBehavior
  extend Flows::Plugin::Interface

  defmethod :my_method
end

class MyImplementation
  include MyBehaviour

  def my_method; end
end

Since:

  • 0.4.0

Defined Under Namespace

Classes: Error, MissingMethodsError

Constant Summary collapse

SingletonVarsSetup =

Since:

  • 0.4.0

Flows::Util::InheritableSingletonVars::DupStrategy.make_module(
  '@interface_methods' => {}
)
InitializePatch =

Since:

  • 0.4.0

Flows::Util::PrependToClass.make_module do
  def initialize(*)
    klass = self.class

    required_methods = klass.instance_variable_get(:@interface_methods).keys
    missing_methods = required_methods - methods

    raise MissingMethodsError.new(klass, missing_methods) if missing_methods.any?

    super
  end
end

Instance Method Summary collapse

Instance Method Details

#defmethod(method_name) ⇒ Object

Since:

  • 0.4.0



78
79
80
81
# File 'lib/flows/plugin/interface.rb', line 78

def defmethod(method_name)
  method_list = instance_variable_get(:@interface_methods)
  method_list[method_name.to_sym] = { required_by: self }
end