Class: Context

Inherits:
Object
  • Object
show all
Defined in:
lib/Moby.rb

Overview

The Context class is used to define a DCI context with roles and their role methods to define a context call define with the name of the context (this name will become the name of the class that defines the context) the name should be a symbol and since it’s going to be used as a class name, use class naming conventions follow the name with a block. With in this block you can define roles and interactions and interaction is defined by write the name of the interaction (hello in the below example) followed by a block the block will become the method body a role can be defined much like a context. instead of calling the define method call the role method followed by the role name (as a symbol) the role will be used for a private instance variable and the naming convention should match this With in the block supplied to the role method you can define role methods the same way as you define interactions. See the method who in the below example

Example

Context::define :Greeter do
    role :who do
      say do
        @who #could be self as well to refer to the current role player of the 'who' role
      end
    end
  greeting do
    p "Hello #{who.say}!"
  end
end

class Greeter
  def initialize(player)
     #@who = player
  end
end

Greeter.new('world').greeting #Will print "Hello world!"

Moby is base on Marvin which was the first injectionless language for DCI being injectionless there’s no runtime extend or anything else impacting the performance. There’ only regular method invocation even when using role methods

Author

Rune Funch Søltoft ([email protected])

License

Same as for Ruby

Class Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missingObject (private)



160
161
162
163
164
165
166
167
# File 'lib/Moby.rb', line 160

def role_or_interaction_method(method_name, &b)
  raise "method with out block #{method_name}" unless b

  args, block = block2source b.to_ruby, method_name
  args = "|#{args}|" if args
  source = "(proc do #{args}\n #{block}\nend)"
  methods[method_name] = source
end

Class Method Details

.define(name, &block) ⇒ Object

define is the only exposed method and can be used to define a context (class) if Moby/kernel is required calling context of Context::define are equivalent params

name

the name of the context. Since this is used as the name of a class, class naming convetions should be used

block

the body of the context. Can include definitions of roles (through the role method) or definitions of interactions

by simply calling a method with the name of the interaction and passing a block as the body of the interaction



54
55
56
57
58
# File 'lib/Moby.rb', line 54

def self.define(name, &block)
  ctx = Context.new
  ctx.instance_eval &block
  return ctx.send(:finalize, name)
end