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
-
#names ⇒ Object
readonly
Returns the value of attribute names.
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
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 {|header_name, header_value| ... } ⇒ Object
Iterates over header name / value pairs, yielding them as a pair of strings to the supplied block.
-
#initialize(headers = {}) ⇒ FrameHeaders
constructor
Creates a new headers collection, initialized with the optional hash parameter.
-
#merge!(hash) ⇒ Object
Merges a hash into this collection of headers.
-
#present?(name) ⇒ Boolean
Returns true if a header value has been set for the supplied header, and the value is neither
nil
nor an empty string. -
#reverse_merge!(hash) ⇒ Object
Reverse merges a hash into this collection of headers.
-
#set?(name) ⇒ Boolean
Returns true if a header value has been set for the supplied header name.
-
#to_hash ⇒ Hash
Returns a new
Hash
object associating symbolized header names and their principle values.
Constructor Details
#initialize(headers = {}) ⇒ FrameHeaders
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
#names ⇒ Object (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
#[](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.
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 |
#[]=(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.
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 |
#all_values(name) ⇒ Array
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 |
#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.
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 |
#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.
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 |
#each {|header_name, header_value| ... } ⇒ Object
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 self.to_enum end 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
.
25 26 27 |
# File 'lib/onstomp/components/frame_headers.rb', line 25 def merge!(hash) hash.each { |k, v| self[k]= v } end |
#present?(name) ⇒ Boolean
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 |
#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
.
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 |
#set?(name) ⇒ Boolean
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 |
#to_hash ⇒ 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 |