Class: Resourceful::Header
- Includes:
- Enumerable
- Defined in:
- lib/resourceful/header.rb
Defined Under Namespace
Classes: FieldDesc
Constant Summary collapse
- @@known_fields =
FieldDesc
Set.new
- @@known_fields_lookup =
Hash.new
Class Method Summary collapse
-
.header_field(name, options = {}) ⇒ Object
Declares a common header field.
- .hop_by_hop_fields ⇒ Object
- .non_modifiable_fields ⇒ Object
Instance Method Summary collapse
- #[](k) ⇒ Object
- #[]=(k, v) ⇒ Object
- #delete(k) ⇒ Object
- #dup ⇒ Object
- #each(&blk) ⇒ Object
-
#each_field(&blk) ⇒ Object
Iterates through the fields with values provided as message ready strings.
- #has_key?(k) ⇒ Boolean (also: #has_field?)
-
#initialize(hash = {}) ⇒ Header
constructor
A new instance of Header.
- #merge(another) ⇒ Object
- #merge!(another) ⇒ Object
- #reverse_merge(another) ⇒ Object
- #to_hash ⇒ Object
Constructor Details
#initialize(hash = {}) ⇒ Header
Returns a new instance of Header.
36 37 38 39 |
# File 'lib/resourceful/header.rb', line 36 def initialize(hash={}) @raw_fields = {} hash.each { |k, v| self[k] = v } end |
Class Method Details
.header_field(name, options = {}) ⇒ Object
Declares a common header field. Header fields do not have to be defined this way but accessing them is easier, safer and faster if you do. Declaring a field does the following things:
* defines accessor methods (e.g. `#content_type` and
`#content_type=`) on `Header`
* defines constant that can be used to reference there field
name (e.g. `some_header[Header::CONTENT_TYPE]`)
* includes the field in the appropriate *_fields groups (e.g. `Header.non_modifiable_fields`)
* provides improved multiple value parsing
Create a new header field descriptor.
273 274 275 276 277 278 279 280 281 282 |
# File 'lib/resourceful/header.rb', line 273 def self.header_field(name, = {}) hfd = FieldDesc.new(name, ) @@known_fields << hfd hfd.lookup_keys do |a_key| @@known_fields_lookup[a_key] = hfd end include(hfd.accessor_module) end |
.hop_by_hop_fields ⇒ Object
284 285 286 |
# File 'lib/resourceful/header.rb', line 284 def self.hop_by_hop_fields @@known_fields.select{|hfd| hfd.hop_by_hop?} end |
.non_modifiable_fields ⇒ Object
288 289 290 |
# File 'lib/resourceful/header.rb', line 288 def self.non_modifiable_fields @@known_fields.reject{|hfd| hfd.modifiable?} end |
Instance Method Details
#[](k) ⇒ Object
45 46 47 |
# File 'lib/resourceful/header.rb', line 45 def [](k) field_def(k).get_from(@raw_fields) end |
#[]=(k, v) ⇒ Object
49 50 51 |
# File 'lib/resourceful/header.rb', line 49 def []=(k, v) field_def(k).set_to(v, @raw_fields) end |
#delete(k) ⇒ Object
53 54 55 |
# File 'lib/resourceful/header.rb', line 53 def delete(k) field_def(k).delete(@raw_fields) end |
#dup ⇒ Object
99 100 101 |
# File 'lib/resourceful/header.rb', line 99 def dup self.class.new(@raw_fields.dup) end |
#each(&blk) ⇒ Object
62 63 64 |
# File 'lib/resourceful/header.rb', line 62 def each(&blk) @raw_fields.each(&blk) end |
#each_field(&blk) ⇒ Object
Iterates through the fields with values provided as message ready strings.
68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/resourceful/header.rb', line 68 def each_field(&blk) each do |k,v| str_v = if field_def(k).multivalued? v.join(', ') else v.to_s end yield k, str_v end end |
#has_key?(k) ⇒ Boolean Also known as: has_field?
57 58 59 |
# File 'lib/resourceful/header.rb', line 57 def has_key?(k) field_def(k).exists_in?(@raw_fields) end |
#merge(another) ⇒ Object
91 92 93 |
# File 'lib/resourceful/header.rb', line 91 def merge(another) self.class.new(self).merge!(another) end |
#merge!(another) ⇒ Object
80 81 82 83 84 85 |
# File 'lib/resourceful/header.rb', line 80 def merge!(another) another.each do |k,v| self[k] = v end self end |
#reverse_merge(another) ⇒ Object
95 96 97 |
# File 'lib/resourceful/header.rb', line 95 def reverse_merge(another) self.class.new(another).merge!(self) end |
#to_hash ⇒ Object
41 42 43 |
# File 'lib/resourceful/header.rb', line 41 def to_hash @raw_fields.dup end |