Class: AMQP::Protocol::Header

Inherits:
Object
  • Object
show all
Defined in:
lib/amqp/protocol.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Header

Returns a new instance of Header.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/amqp/protocol.rb', line 68

def initialize *args
  opts = args.pop if args.last.is_a? Hash
  opts ||= {}
  
  first = args.shift
  
  if first.is_a? ::Class and first.ancestors.include? Protocol::Class
    @klass = first
    @size = args.shift || 0
    @weight = args.shift || 0
    @properties = opts

  elsif first.is_a? Buffer or first.is_a? String
    buf = first
    buf = Buffer.new(buf) unless buf.is_a? Buffer
    
    @klass = Protocol.classes[buf.read(:short)]
    @weight = buf.read(:short)
    @size = buf.read(:longlong)

    props = buf.read(:properties, *klass.properties.map{|type,_| type })
    @properties = Hash[*klass.properties.map{|_,name| name }.zip(props).reject{|k,v| v.nil? }.flatten]

  else
    raise ArgumentError, 'Invalid argument'
  end
  
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &blk) ⇒ Object



124
125
126
127
# File 'lib/amqp/protocol.rb', line 124

def method_missing meth, *args, &blk
  @properties.has_key?(meth) || @klass.properties.find{|_,name| name == meth } ? @properties[meth] :
                                                                                 super
end

Instance Attribute Details

#klassObject

Returns the value of attribute klass.



96
97
98
# File 'lib/amqp/protocol.rb', line 96

def klass
  @klass
end

#propertiesObject

Returns the value of attribute properties.



96
97
98
# File 'lib/amqp/protocol.rb', line 96

def properties
  @properties
end

#sizeObject

Returns the value of attribute size.



96
97
98
# File 'lib/amqp/protocol.rb', line 96

def size
  @size
end

#weightObject

Returns the value of attribute weight.



96
97
98
# File 'lib/amqp/protocol.rb', line 96

def weight
  @weight
end

Instance Method Details

#==(header) ⇒ Object



118
119
120
121
122
# File 'lib/amqp/protocol.rb', line 118

def == header
  [ :klass, :size, :weight, :properties ].inject(true) do |eql, field|
    eql and __send__(field) == header.__send__(field)
  end
end

#to_binaryObject



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/amqp/protocol.rb', line 98

def to_binary
  buf = Buffer.new
  buf.write :short, klass.id
  buf.write :short, weight # XXX rabbitmq only supports weight == 0
  buf.write :longlong, size
  buf.write :properties, (klass.properties.map do |type, name|
                           [ type, properties[name] || properties[name.to_s] ]
                         end)
  buf.rewind
  buf
end

#to_frame(channel = 0) ⇒ Object



114
115
116
# File 'lib/amqp/protocol.rb', line 114

def to_frame channel = 0
  Frame::Header.new(self, channel)
end

#to_sObject



110
111
112
# File 'lib/amqp/protocol.rb', line 110

def to_s
  to_binary.to_s
end