Class: Resourceful::Header::FieldDesc
- Includes:
- Comparable
- Defined in:
- lib/resourceful/header.rb
Overview
Class to handle the details of each type of field.
Instance Attribute Summary collapse
-
#name ⇒ Object
(also: #to_s)
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #<=>(another) ⇒ Object
- #==(another) ⇒ Object (also: #eql?)
- #===(another) ⇒ Object
- #accessor_module ⇒ Object
- #constantized_name ⇒ Object
- #delete(raw_fields_hash) ⇒ Object
- #exists_in?(raw_fields_hash) ⇒ Boolean
- #get_from(raw_fields_hash) ⇒ Object
- #hash ⇒ Object
- #hop_by_hop? ⇒ Boolean
-
#initialize(name, options = {}) ⇒ FieldDesc
constructor
Create a new header field descriptor.
-
#lookup_keys {|name| ... } ⇒ Object
Yields each commonly used lookup key for this header field.
- #methodized_name ⇒ Object
- #modifiable? ⇒ Boolean
- #name_pattern ⇒ Object
- #repeatable? ⇒ Boolean (also: #multivalued?)
- #set_to(value, raw_fields_hash) ⇒ Object
Constructor Details
#initialize(name, options = {}) ⇒ FieldDesc
Create a new header field descriptor.
129 130 131 132 133 134 135 136 |
# File 'lib/resourceful/header.rb', line 129 def initialize(name, = {}) @name = name = Options.for().validate(:repeatable, :hop_by_hop, :modifiable) @repeatable = .getopt([:repeatable, :multivalue, :multivalued]) || false @hop_by_hop = .getopt(:hop_by_hop) || false @modifiable = .getopt(:modifiable, true) end |
Instance Attribute Details
#name ⇒ Object (readonly) Also known as: to_s
Returns the value of attribute name.
108 109 110 |
# File 'lib/resourceful/header.rb', line 108 def name @name end |
Instance Method Details
#<=>(another) ⇒ Object
174 175 176 |
# File 'lib/resourceful/header.rb', line 174 def <=>(another) name <=> another.name end |
#==(another) ⇒ Object Also known as: eql?
178 179 180 |
# File 'lib/resourceful/header.rb', line 178 def ==(another) name_pattern === another.to_s end |
#===(another) ⇒ Object
183 184 185 186 187 188 189 |
# File 'lib/resourceful/header.rb', line 183 def ===(another) if another.kind_of?(FieldDesc) self == another else name_pattern === another end end |
#accessor_module ⇒ Object
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/resourceful/header.rb', line 205 def accessor_module @accessor_module ||= begin Module.new.tap{|m| m.module_eval(<<-RUBY)} #{constantized_name} = '#{name}' def #{methodized_name} # def accept self[#{constantized_name}] # self[ACCEPT] end # end def #{methodized_name}=(val) # def accept=(val) self[#{constantized_name}] = val # self[ACCEPT] = val end # end RUBY end end |
#constantized_name ⇒ Object
199 200 201 |
# File 'lib/resourceful/header.rb', line 199 def constantized_name @constantized_name ||= name.upcase.gsub('-', '_') end |
#delete(raw_fields_hash) ⇒ Object
166 167 168 |
# File 'lib/resourceful/header.rb', line 166 def delete(raw_fields_hash) raw_fields_hash.delete(name) end |
#exists_in?(raw_fields_hash) ⇒ Boolean
170 171 172 |
# File 'lib/resourceful/header.rb', line 170 def exists_in?(raw_fields_hash) raw_fields_hash.has_key?(name) end |
#get_from(raw_fields_hash) ⇒ Object
151 152 153 |
# File 'lib/resourceful/header.rb', line 151 def get_from(raw_fields_hash) raw_fields_hash[name] end |
#hash ⇒ Object
221 222 223 |
# File 'lib/resourceful/header.rb', line 221 def hash @name.hash end |
#hop_by_hop? ⇒ Boolean
143 144 145 |
# File 'lib/resourceful/header.rb', line 143 def hop_by_hop? @hop_by_hop end |
#lookup_keys {|name| ... } ⇒ Object
Yields each commonly used lookup key for this header field.
226 227 228 229 230 231 232 233 234 |
# File 'lib/resourceful/header.rb', line 226 def lookup_keys(&blk) yield name yield name.upcase yield name.downcase yield methodized_name yield methodized_name.to_sym yield constantized_name yield constantized_name.to_sym end |
#methodized_name ⇒ Object
195 196 197 |
# File 'lib/resourceful/header.rb', line 195 def methodized_name @methodized_name ||= name.downcase.gsub('-', '_') end |
#modifiable? ⇒ Boolean
147 148 149 |
# File 'lib/resourceful/header.rb', line 147 def modifiable? @modifiable end |
#name_pattern ⇒ Object
191 192 193 |
# File 'lib/resourceful/header.rb', line 191 def name_pattern @name_pattern ||= Regexp.new('^' + name.gsub('-', '[_-]') + '$', Regexp::IGNORECASE) end |
#repeatable? ⇒ Boolean Also known as: multivalued?
138 139 140 |
# File 'lib/resourceful/header.rb', line 138 def repeatable? @repeatable end |
#set_to(value, raw_fields_hash) ⇒ Object
155 156 157 158 159 160 161 162 163 164 |
# File 'lib/resourceful/header.rb', line 155 def set_to(value, raw_fields_hash) raw_fields_hash[name] = if multivalued? Array(value).map{|v| v.split(/,\s*/)}.flatten elsif value.kind_of?(Array) raise ArgumentError, "#{name} field may only have one value" if value.size > 1 value.first else value end end |