Class: Qrack::Protocol::Class::Method

Inherits:
Object
  • Object
show all
Defined in:
lib/ext/bunny-0.6.0/lib/qrack/protocol/protocol08.rb,
lib/ext/bunny-0.6.0/lib/qrack/protocol/spec08.rb

Overview

:stopdoc:

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Method

Returns a new instance of Method.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/ext/bunny-0.6.0/lib/qrack/protocol/protocol08.rb', line 5

def initialize *args
  opts = args.pop if args.last.is_a? Hash
  opts ||= {}
 
  if args.size == 1 and args.first.is_a? Transport::Buffer
    buf = args.shift
  else
    buf = nil
  end

  self.class.arguments.each do |type, name|
    val = buf ? buf.read(type) :
                args.shift || opts[name] || opts[name.to_s]
    instance_variable_set("@#{name}", val)
  end
end

Class Method Details

.argumentsObject



75
# File 'lib/ext/bunny-0.6.0/lib/qrack/protocol/spec08.rb', line 75

def arguments() @arguments ||= [] end

.idObject



78
# File 'lib/ext/bunny-0.6.0/lib/qrack/protocol/spec08.rb', line 78

def id()     self::ID end

.nameObject



79
# File 'lib/ext/bunny-0.6.0/lib/qrack/protocol/spec08.rb', line 79

def name()   self::NAME end

.parentObject



77
# File 'lib/ext/bunny-0.6.0/lib/qrack/protocol/spec08.rb', line 77

def parent() Protocol.const_get(self.to_s[/Protocol::(.+?)::/,1]) end

Instance Method Details

#==(b) ⇒ Object



82
83
84
85
86
# File 'lib/ext/bunny-0.6.0/lib/qrack/protocol/spec08.rb', line 82

def == b
  self.class.arguments.inject(true) do |eql, (type, name)|
    eql and __send__("#{name}") == b.__send__("#{name}")
  end
end

#argumentsObject



22
23
24
25
26
# File 'lib/ext/bunny-0.6.0/lib/qrack/protocol/protocol08.rb', line 22

def arguments
  self.class.arguments.inject({}) do |hash, (type, name)|
    hash.update name => instance_variable_get("@#{name}")
  end
end

#to_binaryObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ext/bunny-0.6.0/lib/qrack/protocol/protocol08.rb', line 28

def to_binary
  buf = Transport::Buffer.new
  buf.write :short, self.class.parent.id
  buf.write :short, self.class.id

  bits = []

  self.class.arguments.each do |type, name|
    val = instance_variable_get("@#{name}")
    if type == :bit
      bits << (val || false)
    else
      unless bits.empty?
        buf.write :bit, bits
        bits = []
      end
      buf.write type, val
    end
  end

  buf.write :bit, bits unless bits.empty?
  buf.rewind

  buf
end

#to_frame(channel = 0) ⇒ Object



58
59
60
# File 'lib/ext/bunny-0.6.0/lib/qrack/protocol/protocol08.rb', line 58

def to_frame channel = 0
  Transport::Method.new(self, channel)
end

#to_sObject



54
55
56
# File 'lib/ext/bunny-0.6.0/lib/qrack/protocol/protocol08.rb', line 54

def to_s
  to_binary.to_s
end