Class: Protocol::Message

Inherits:
Object show all
Includes:
Comparable
Defined in:
lib/protocol/message.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(protocol, name, arity = nil, block_expected = false) ⇒ Message

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



9
10
11
12
13
# 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

Instance Attribute Details

#arityObject

Arity of this message = the number of arguments.



22
23
24
# File 'lib/protocol/message.rb', line 22

def arity
  @arity
end

#nameObject (readonly)

Name of this message.



19
20
21
# File 'lib/protocol/message.rb', line 19

def name
  @name
end

#protocolObject (readonly)

The protocol this message was defined in.



16
17
18
# File 'lib/protocol/message.rb', line 16

def protocol
  @protocol
end

Instance Method Details

#<=>(other) ⇒ Object

Message order is alphabetic name order.



35
36
37
# File 'lib/protocol/message.rb', line 35

def <=>(other)
  name <=> other.name
end

#==(other) ⇒ Object

Returns true if this message equals the message other.



40
41
42
# File 'lib/protocol/message.rb', line 40

def ==(other)
  name == other.name && arity == other.arity
end

#block_expected=(block_expected) ⇒ Object

Set to true if this message should expect a block.



25
26
27
# File 'lib/protocol/message.rb', line 25

def block_expected=(block_expected)
  @block_expected = !!block_expected
end

#block_expected?Boolean

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



30
31
32
# File 'lib/protocol/message.rb', line 30

def block_expected?
  @block_expected
end

#check(object, checked) ⇒ Object

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).



79
80
81
82
83
84
85
86
# 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

#shortcutObject

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



45
46
47
# File 'lib/protocol/message.rb', line 45

def shortcut
  "#{name}(#{arity}#{block_expected? ? '&' : ''})"
end

#to_ruby(result = '') ⇒ Object

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



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# 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_sObject

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



51
52
53
# File 'lib/protocol/message.rb', line 51

def to_s
  "#{protocol.name}##{shortcut}"
end