class Protocol::Message

A Message consists of the name of the message (=method name), and the method argument's arity.

Attributes

arity[RW]

Arity of this message = the number of arguments.

name[R]

Name of this message.

protocol[R]

The protocol this message was defined in.

Public Class Methods

new(protocol, name, arity = nil, block_expected = false) click to toggle source

Creates a Message instance named name, with the arity arity. If arity is nil, the arity isn't checked during conformity tests.

# File lib/protocol/message.rb, line 9
def initialize(protocol, name, arity = nil, block_expected = false)
  name = name.to_s
  @protocol, @name, @arity, @block_expected =
    protocol, name, arity, !!block_expected
end

Public Instance Methods

<=>(other) click to toggle source

Message order is alphabetic name order.

# File lib/protocol/message.rb, line 35
def <=>(other)
  name <=> other.name
end
==(other) click to toggle source

Returns true if this message equals the message other.

# File lib/protocol/message.rb, line 40
def ==(other)
  name == other.name && arity == other.arity
end
block_expected=(block_expected) click to toggle source

Set to true if this message should expect a block.

# File lib/protocol/message.rb, line 25
def block_expected=(block_expected)
  @block_expected = !!block_expected
end
block_expected?() click to toggle source

Returns true if this message is expected to include a block argument.

# File lib/protocol/message.rb, line 30
def block_expected?
  @block_expected
end
check(object, checked) click to toggle source

The class klass is checked against this Message instance. A CheckError exception will called, if either a required method isn't found in the klass, or it doesn't have the required arity (if a fixed arity was demanded).

# File lib/protocol/message.rb, line 79
def check(object, checked)
  check_message = object.is_a?(Class) ? :check_class : :check_object
  if checked.key?(name)
    true
  else
    checked[name] = __send__(check_message, object)
  end
end
shortcut() click to toggle source

Returns the shortcut for this message of the form “methodname(arity)”.

# File lib/protocol/message.rb, line 45
def shortcut
  "#{name}(#{arity}#{block_expected? ? '&' : ''})"
end
to_ruby(result = '') click to toggle source

Concatenates a method signature as ruby code to the result string and returns it.

# File lib/protocol/message.rb, line 57
def to_ruby(result = '')
  if arity
    result << "  def #{name}("
    args = if arity >= 0
      (1..arity).map { |i| "x#{i}" }
    else
      (1..~arity).map { |i| "x#{i}" } << '*rest'
    end
    if block_expected?
      args << '&block'
    end
    result << args * ', '
    result << ") end\n"
  else
    result << "  understand :#{name}\n"
  end
end
to_s() click to toggle source

Return a string representation of this message, in the form Protocol#name(arity).

# File lib/protocol/message.rb, line 51
def to_s
  "#{protocol.name}##{shortcut}"
end

Private Instance Methods

check_class(klass) click to toggle source

Check class klass against this Message instance, and raise a CheckError exception if necessary.

# File lib/protocol/message.rb, line 92
def check_class(klass)
  unless klass.method_defined?(name)
    raise NotImplementedErrorCheckError.new(self,
        "method '#{name}' not implemented in #{klass}")
  end
  check_method = klass.instance_method(name)
  if arity and (check_arity = check_method.arity) != arity
    raise ArgumentErrorCheckError.new(self,
          "wrong number of arguments for protocol"\
          " in method '#{name}' (#{check_arity} for #{arity}) of #{klass}")
  end
  if block_expected?
    modul = Utilities.find_method_module(name, klass.ancestors)
    parser = MethodParser.new(modul, name)
    parser.block_arg? or raise BlockCheckError.new(self,
      "expected a block argument for #{klass}")
  end
  arity and wrap_method(klass)
  true
end