Class: Qrack::Protocol09::Class::Method

Inherits:
Object
  • Object
show all
Defined in:
lib/ext/bunny-0.6.0/lib/qrack/protocol/protocol09.rb,
lib/ext/bunny-0.6.0/lib/qrack/protocol/spec09.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/protocol09.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? Transport09::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



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

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

.idObject



80
# File 'lib/ext/bunny-0.6.0/lib/qrack/protocol/spec09.rb', line 80

def id()     self::ID end

.nameObject



81
# File 'lib/ext/bunny-0.6.0/lib/qrack/protocol/spec09.rb', line 81

def name()   self::NAME end

.parentObject



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

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

Instance Method Details

#==(b) ⇒ Object



84
85
86
87
88
# File 'lib/ext/bunny-0.6.0/lib/qrack/protocol/spec09.rb', line 84

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/protocol09.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/protocol09.rb', line 28

def to_binary
  buf = Transport09::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/protocol09.rb', line 58

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

#to_sObject



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

def to_s
  to_binary.to_s
end