Class: OnStomp::Components::FrameHeaders
- Inherits:
-
Object
- Object
- OnStomp::Components::FrameHeaders
- Includes:
- Enumerable
- Defined in:
- lib/onstomp/components/frame_headers.rb
Overview
A specialized container for storing header name / value pairs for a
frame. This container behaves much like a Hash
, but
is specialized for the Stomp protocol. Header names are always converted
into String
s through the use of to_s
and may have more than one value
associated with them.
Instance Attribute Summary (collapse)
-
- (Object) names
readonly
Returns the value of attribute names.
Instance Method Summary (collapse)
-
- (String?) [](name)
Gets the principle header value paired with the supplied header name.
-
- (String) []=(name, val)
Sets the header value paired with the supplied header name.
-
- (Array) all_values(name)
Retrieves all header values associated with the supplied header name.
-
- (String) append(name, val)
Appends a header value to the specified header name.
-
- (Array) delete(name)
Deletes all of the header values associated with the header name and removes the header name itself.
-
- (Object) each {|header_name, header_value| ... }
Iterates over header name / value pairs, yielding them as a pair of strings to the supplied block.
-
- (FrameHeaders) initialize(headers = {})
constructor
Creates a new headers collection, initialized with the optional hash parameter.
-
- (Object) merge!(hash)
Merges a hash into this collection of headers.
-
- (Boolean) present?(name)
Returns true if a header value has been set for the supplied header, and the value is neither
nil
nor an empty string. -
- (Object) reverse_merge!(hash)
Reverse merges a hash into this collection of headers.
-
- (Boolean) set?(name)
Returns true if a header value has been set for the supplied header name.
-
- (Hash) to_hash
Returns a new
Hash
object associating symbolized header names and their principle values.
Constructor Details
- (FrameHeaders) initialize(headers = {})
Creates a new headers collection, initialized with the optional hash parameter.
15 16 17 18 19 |
# File 'lib/onstomp/components/frame_headers.rb', line 15 def initialize(headers={}) @values = {} initialize_names merge! headers end |
Instance Attribute Details
- (Object) names (readonly)
Returns the value of attribute names
180 |
# File 'lib/onstomp/components/frame_headers.rb', line 180 def names; @values.keys; end |
Instance Method Details
- (String?) [](name)
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.
133 134 135 136 |
# File 'lib/onstomp/components/frame_headers.rb', line 133 def [](name) name = name.to_sym @values[name] && @values[name].first end |
- (String) []=(name, val)
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.
150 151 152 153 154 155 156 |
# File 'lib/onstomp/components/frame_headers.rb', line 150 def []=(name, val) name = name.to_sym val = val.to_s add_name name @values[name] = [val] val end |
- (Array) all_values(name)
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.
77 78 79 |
# File 'lib/onstomp/components/frame_headers.rb', line 77 def all_values(name) @values[name.to_sym] || [] end |
- (String) append(name, val)
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.
95 96 97 98 99 100 101 102 103 104 |
# File 'lib/onstomp/components/frame_headers.rb', line 95 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 |
- (Array) delete(name)
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.
115 116 117 118 119 120 121 |
# File 'lib/onstomp/components/frame_headers.rb', line 115 def delete(name) name = name.to_sym if @values.key? name delete_name name @values.delete name end end |
- (Object) each {|header_name, header_value| ... }
Iterates over header name / value pairs, yielding them as a pair of strings to the supplied block.
170 171 172 173 174 175 176 177 |
# File 'lib/onstomp/components/frame_headers.rb', line 170 def each &block if block_given? iterate_each &block self else OnStomp::ENUMERATOR_KLASS.new(self) end end |
- (Object) merge!(hash)
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
.
25 26 27 |
# File 'lib/onstomp/components/frame_headers.rb', line 25 def merge!(hash) hash.each { |k, v| self[k]= v } end |
- (Boolean) present?(name)
Returns true if a header value has been set for the supplied header, and
the value is neither nil
nor an empty string.
59 60 61 62 |
# File 'lib/onstomp/components/frame_headers.rb', line 59 def present?(name) val = self[name] !(val.nil? || val.empty?) end |
- (Object) reverse_merge!(hash)
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
.
35 36 37 38 39 |
# File 'lib/onstomp/components/frame_headers.rb', line 35 def reverse_merge!(hash) hash.each { |k, v| self[k]= v unless set?(k) } end |
- (Boolean) set?(name)
Returns true if a header value has been set for the supplied header name.
46 47 48 |
# File 'lib/onstomp/components/frame_headers.rb', line 46 def set?(name) @values.key?(name.to_sym) end |
- (Hash) to_hash
Returns a new Hash
object associating symbolized header names and their
principle values.
161 162 163 |
# File 'lib/onstomp/components/frame_headers.rb', line 161 def to_hash @values.inject({}) { |h, (k,v)| h[k] = v.first; h } end |