Module: Stomper::Support::Ruby1_8::Headers
- Defined in:
- lib/stomper/support/1.8/headers.rb
Overview
The implementation of the Headers class for Ruby 1.8.7
Instance Attribute Summary collapse
-
#names ⇒ Array<String>
readonly
An array of header names ordered by when they were set.
Instance Method Summary collapse
-
#[](name) ⇒ String?
Gets the principle header value paired with the supplied header name.
-
#[]=(name, val) ⇒ String
Sets the header value paired with the supplied header name.
-
#all_values(name) ⇒ Array
(also: #all)
Retrieves all header values associated with the supplied header name.
-
#append(name, val) ⇒ String
Appends a header value to the specified header name.
-
#delete(name) ⇒ Array
Deletes all of the header values associated with the header name and removes the header name itself.
-
#each {|name, value| ... } ⇒ Headers, Enumerable::Enumerator
Iterates over each header name / value pair in the order in which the headers names were set.
-
#has?(name) ⇒ Boolean
(also: #key?, #include?)
Returns true if a header value has been set for the supplied header name.
-
#initialize(headers = {}) ⇒ Object
Creates a new headers collection, initialized with the optional hash parameter.
-
#merge!(hash) ⇒ Object
Merges a hash into this collection of headers.
-
#reverse_merge!(hash) ⇒ Object
Reverse merges a hash into this collection of headers.
Instance Attribute Details
#names ⇒ Array<String> (readonly)
An array of header names ordered by when they were set.
7 8 9 |
# File 'lib/stomper/support/1.8/headers.rb', line 7 def names @names end |
Instance Method Details
#[](name) ⇒ String?
Gets the principle header value paired with the supplied header name. The name will be converted to a Symbol, so must respond to the to_sym
method. The Stomp 1.1 protocol specifies that in the event of a repeated header name, the first value encountered serves as the principle value.
123 124 125 126 |
# File 'lib/stomper/support/1.8/headers.rb', line 123 def [](name) vals = @values[name.to_sym] vals && vals.first end |
#[]=(name, val) ⇒ String
Sets the header value paired with the supplied header name. The name will be converted to a Symbol and must respond to to_sym
; meanwhile, the value will be converted to a String so must respond to to_s
. Setting a header value in this fashion will overwrite any repeated header values.
140 141 142 143 144 145 146 |
# File 'lib/stomper/support/1.8/headers.rb', line 140 def []=(name, val) name = name.to_sym val = val.to_s @names << name unless @values.key?(name) @values[name] = [val] val end |
#all_values(name) ⇒ Array Also known as: all
Retrieves all header values associated with the supplied header name. In general, this will be an array containing only the principle header value; however, in the event a frame contained repeated header names, this method will return all of the associated values. The first element of the array will be the principle value of the supplied header name.
66 67 68 |
# File 'lib/stomper/support/1.8/headers.rb', line 66 def all_values(name) @values[name.to_sym] || [] end |
#append(name, val) ⇒ String
Appends a header value to the specified header name. If the specified header name is not known, the supplied value will also become the principle value. This method is used internally when constructing frames sent by the broker to capture repeated header names.
102 103 104 105 106 107 108 109 110 111 |
# File 'lib/stomper/support/1.8/headers.rb', line 102 def append(name, val) name = name.to_sym val = val.to_s if @values.key?(name) @values[name] << val else self[name]= val end val end |
#delete(name) ⇒ Array
Deletes all of the header values associated with the header name and removes the header name itself. This is analogous to the delete
method found in Hash objects.
80 81 82 83 84 85 86 |
# File 'lib/stomper/support/1.8/headers.rb', line 80 def delete(name) name = name.to_sym if @values.key? name @names.delete(name) @values.delete(name) end end |
#each {|name, value| ... } ⇒ Headers, Enumerable::Enumerator
Iterates over each header name / value pair in the order in which the headers names were set. If this collection contains repeated header names, the supplied block will receive those header names repeatedly, once for each value. If no block is supplied, then an Enumerator
is returned. All header names yielded through this method will be Strings and not the Symbol keys used internally.
169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/stomper/support/1.8/headers.rb', line 169 def each(&block) if block_given? @names.each do |name| @values[name].each do |val| yield [name.to_s, val] end end self else ::Enumerable::Enumerator.new(self) end end |
#has?(name) ⇒ Boolean Also known as: key?, include?
Returns true if a header value has been set for the supplied header name.
47 48 49 |
# File 'lib/stomper/support/1.8/headers.rb', line 47 def has?(name) @values.key?(name.to_sym) end |
#initialize(headers = {}) ⇒ Object
With Ruby 1.8.7, the order of hash keys may not be preserved
Creates a new headers collection, initialized with the optional hash parameter.
14 15 16 17 18 |
# File 'lib/stomper/support/1.8/headers.rb', line 14 def initialize(headers={}) @values = {} @names = [] merge! headers end |
#merge!(hash) ⇒ Object
With Ruby 1.8.7, the order of hash keys may not be preserved
Merges a hash into this collection of headers. All of the keys used in the hash must be convertable to Symbols through to_sym
.
24 25 26 |
# File 'lib/stomper/support/1.8/headers.rb', line 24 def merge!(hash) hash.each { |k, v| self[k]= v } end |
#reverse_merge!(hash) ⇒ Object
With Ruby 1.8.7, the order of hash keys may not be preserved
Reverse merges a hash into this collection of headers. The hash keys and values are included only if the headers collection does not already have a matching key. All of the keys used in the hash must be convertable to Symbols through to_sym
.
34 35 36 37 38 |
# File 'lib/stomper/support/1.8/headers.rb', line 34 def reverse_merge!(hash) hash.each { |k, v| self[k]= v unless has?(k) } end |