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
- #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.
37 38 39 40 |
# File 'lib/resourceful/header.rb', line 37 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.
262 263 264 265 266 267 268 269 270 271 |
# File 'lib/resourceful/header.rb', line 262 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
273 274 275 |
# File 'lib/resourceful/header.rb', line 273 def self.hop_by_hop_fields @@known_fields.select{|hfd| hfd.hop_by_hop?} end |
.non_modifiable_fields ⇒ Object
277 278 279 |
# File 'lib/resourceful/header.rb', line 277 def self.non_modifiable_fields @@known_fields.reject{|hfd| hfd.modifiable?} end |
Instance Method Details
#[](k) ⇒ Object
46 47 48 |
# File 'lib/resourceful/header.rb', line 46 def [](k) field_def(k).get_from(@raw_fields) end |
#[]=(k, v) ⇒ Object
50 51 52 |
# File 'lib/resourceful/header.rb', line 50 def []=(k, v) field_def(k).set_to(v, @raw_fields) end |
#dup ⇒ Object
92 93 94 |
# File 'lib/resourceful/header.rb', line 92 def dup self.class.new(@raw_fields.dup) end |
#each(&blk) ⇒ Object
59 60 61 |
# File 'lib/resourceful/header.rb', line 59 def each(&blk) @raw_fields.each(&blk) end |
#each_field(&blk) ⇒ Object
Iterates through the fields with values provided as message ready strings.
65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/resourceful/header.rb', line 65 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?
54 55 56 |
# File 'lib/resourceful/header.rb', line 54 def has_key?(k) field_def(k).exists_in?(@raw_fields) end |
#merge(another) ⇒ Object
84 85 86 |
# File 'lib/resourceful/header.rb', line 84 def merge(another) self.class.new(self).merge!(another) end |
#merge!(another) ⇒ Object
77 78 79 80 81 82 |
# File 'lib/resourceful/header.rb', line 77 def merge!(another) another.each do |k,v| self[k] = v end self end |
#reverse_merge(another) ⇒ Object
88 89 90 |
# File 'lib/resourceful/header.rb', line 88 def reverse_merge(another) self.class.new(another).merge!(self) end |
#to_hash ⇒ Object
42 43 44 |
# File 'lib/resourceful/header.rb', line 42 def to_hash @raw_fields.dup end |