class Protocol::Message
A Message
consists of the name of the message (=method name), and the method argument’s arity.
Attributes
Arity of this message = the number of arguments.
Name of this message.
The protocol this message was defined in.
Public Class Methods
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
Message
order is alphabetic name order.
# File lib/protocol/message.rb, line 35 def <=>(other) name <=> other.name end
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
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
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
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
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
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
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
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