Class: Resourceful::Header
- Includes:
- Enumerable
- Defined in:
- lib/resourceful/header.rb
Defined Under Namespace
Classes: HeaderFieldDef
Constant Summary collapse
- @@header_field_defs =
Set.new
Class Method Summary collapse
- .header_field(name, options = {}) ⇒ Object
- .hop_by_hop_headers ⇒ Object
- .non_modifiable_headers ⇒ Object
Instance Method Summary collapse
- #[](k) ⇒ Object
- #[]=(k, v) ⇒ Object
- #dup ⇒ Object
- #each(&blk) ⇒ Object (also: #each_field)
- #field_def(name) ⇒ Object
- #has_key?(k) ⇒ Boolean
-
#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
182 183 184 185 186 187 188 189 190 |
# File 'lib/resourceful/header.rb', line 182 def self.header_field(name, = {}) hfd = HeaderFieldDef.new(name, ) @@header_field_defs << hfd hfd.gen_getter(self) hfd.gen_setter(self) hfd.gen_canonical_name_const(self) end |
.hop_by_hop_headers ⇒ Object
192 193 194 |
# File 'lib/resourceful/header.rb', line 192 def self.hop_by_hop_headers @@header_field_defs.select{|hfd| hfd.hop_by_hop?} end |
.non_modifiable_headers ⇒ Object
196 197 198 |
# File 'lib/resourceful/header.rb', line 196 def self.non_modifiable_headers @@header_field_defs.reject{|hfd| hfd.repeatable?} 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 |
#dup ⇒ Object
77 78 79 |
# File 'lib/resourceful/header.rb', line 77 def dup self.class.new(@raw_fields.dup) end |
#each(&blk) ⇒ Object Also known as: each_field
57 58 59 |
# File 'lib/resourceful/header.rb', line 57 def each(&blk) @raw_fields.each(&blk) end |
#field_def(name) ⇒ Object
200 201 202 203 |
# File 'lib/resourceful/header.rb', line 200 def field_def(name) @@header_field_defs.find{|hfd| hfd === name} || HeaderFieldDef.new(name.to_s.downcase.gsub(/^.|[-_\s]./) { |x| x.upcase }.gsub('_', '-'), :repeatable => true) end |
#has_key?(k) ⇒ Boolean
53 54 55 |
# File 'lib/resourceful/header.rb', line 53 def has_key?(k) field_def(k).exists_in?(@raw_fields) end |
#merge(another) ⇒ Object
69 70 71 |
# File 'lib/resourceful/header.rb', line 69 def merge(another) self.class.new(self).merge!(another) end |
#merge!(another) ⇒ Object
62 63 64 65 66 67 |
# File 'lib/resourceful/header.rb', line 62 def merge!(another) another.each do |k,v| self[k] = v end self end |
#reverse_merge(another) ⇒ Object
73 74 75 |
# File 'lib/resourceful/header.rb', line 73 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 |