Module: Bossy

Defined in:
lib/bossy.rb,
lib/bossy/interface.rb,
lib/bossy/method_signature.rb,
lib/bossy/errors/method_missing.rb

Overview

This class encapsulates the definition of a method signature. That includes the method name, return type, parameters and parameter types. Basically it captures: ‘String my_method(Integer arg1, Float arg2)`. You can instantiate this class by calling: `Bossy::MethodSignature.new(String: :my_method, Integer: :arg1, Float: :arg2)`

Defined Under Namespace

Modules: ClassMethods, Errors Classes: Interface, MethodSignature

Instance Method Summary collapse

Instance Method Details

#included(base) ⇒ Object

Setup the class that is implementing the interface



36
37
38
39
# File 'lib/bossy.rb', line 36

def included(base)
  base.extend(Bossy::ClassMethods)
  base.interface = Bossy::Interface.new(base, @required_methods)
end

#interface(&block) ⇒ Object

This allows users to define an interface in-line in a clas.



27
28
29
30
31
32
33
# File 'lib/bossy.rb', line 27

def interface(&block)
  mod = Module.new
  mod.extend(Bossy)
  mod.instance_eval(&block)
  include mod
  mod
end

#require_method(args) ⇒ Object

This is what you can call to define methods in an interface



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/bossy.rb', line 14

def require_method(args)
  raise "require_method should receive a data type and method name, e.g. 'String: :name'" unless args.is_a?(Hash)

  @required_methods ||= []
  method_name_and_type, *parameters = *args
  @required_methods << Bossy::MethodSignature.new(
    method_name_and_type.first,
    method_name_and_type.last,
    parameters
  )
end