Class: Resourceful::Header::HeaderFieldDef
- Includes:
- Comparable, OptionsInterpretation
- 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
- #exists_in?(raw_fields_hash) ⇒ Boolean
- #gen_canonical_name_const(klass) ⇒ Object
- #gen_getter(klass) ⇒ Object
- #gen_setter(klass) ⇒ Object
- #get_from(raw_fields_hash) ⇒ Object
- #hop_by_hop? ⇒ Boolean
-
#initialize(name, options = {}) ⇒ HeaderFieldDef
constructor
A new instance of HeaderFieldDef.
- #methodized_name ⇒ Object
- #modifiable? ⇒ Boolean
- #name_pattern ⇒ Object
- #repeatable? ⇒ Boolean
- #set_to(value, raw_fields_hash) ⇒ Object
Methods included from OptionsInterpretation
Constructor Details
#initialize(name, options = {}) ⇒ HeaderFieldDef
Returns a new instance of HeaderFieldDef.
90 91 92 93 94 95 96 97 |
# File 'lib/resourceful/header.rb', line 90 def initialize(name, = {}) @name = name extract_opts() do |opts| @repeatable = opts.extract(:repeatable, :default => false) @hop_by_hop = opts.extract(:hop_by_hop, :default => false) @modifiable = opts.extract(:modifiable, :default => true) end end |
Instance Attribute Details
#name ⇒ Object (readonly) Also known as: to_s
Returns the value of attribute name.
88 89 90 |
# File 'lib/resourceful/header.rb', line 88 def name @name end |
Instance Method Details
#<=>(another) ⇒ Object
130 131 132 |
# File 'lib/resourceful/header.rb', line 130 def <=>(another) name <=> another.name end |
#==(another) ⇒ Object Also known as: eql?
134 135 136 |
# File 'lib/resourceful/header.rb', line 134 def ==(another) name_pattern === another.name end |
#===(another) ⇒ Object
139 140 141 142 143 144 145 |
# File 'lib/resourceful/header.rb', line 139 def ===(another) if another.kind_of?(HeaderFieldDef) self == another else name_pattern === another end end |
#exists_in?(raw_fields_hash) ⇒ Boolean
126 127 128 |
# File 'lib/resourceful/header.rb', line 126 def exists_in?(raw_fields_hash) raw_fields_hash.has_key?(name) end |
#gen_canonical_name_const(klass) ⇒ Object
173 174 175 176 177 |
# File 'lib/resourceful/header.rb', line 173 def gen_canonical_name_const(klass) const_name = name.upcase.gsub('-', '_') klass.const_set(const_name, name) end |
#gen_getter(klass) ⇒ Object
165 166 167 168 169 170 171 |
# File 'lib/resourceful/header.rb', line 165 def gen_getter(klass) klass.class_eval " def \#{methodized_name} # def accept\n self['\#{name}'] # self['Accept']\n end # end\n RUBY\nend\n" |
#gen_setter(klass) ⇒ Object
157 158 159 160 161 162 163 |
# File 'lib/resourceful/header.rb', line 157 def gen_setter(klass) klass.class_eval " def \#{methodized_name}=(val) # def accept=(val)\n self['\#{name}'] = val # self['Accept'] = val\n end # end\n RUBY\nend\n" |
#get_from(raw_fields_hash) ⇒ Object
111 112 113 |
# File 'lib/resourceful/header.rb', line 111 def get_from(raw_fields_hash) raw_fields_hash[name] end |
#hop_by_hop? ⇒ Boolean
103 104 105 |
# File 'lib/resourceful/header.rb', line 103 def hop_by_hop? @hop_by_hop end |
#methodized_name ⇒ Object
151 152 153 |
# File 'lib/resourceful/header.rb', line 151 def methodized_name name.downcase.gsub('-', '_') end |
#modifiable? ⇒ Boolean
107 108 109 |
# File 'lib/resourceful/header.rb', line 107 def modifiable? @modifiable end |
#name_pattern ⇒ Object
147 148 149 |
# File 'lib/resourceful/header.rb', line 147 def name_pattern Regexp.new('^' + name.gsub('-', '[_-]') + '$', Regexp::IGNORECASE) end |
#repeatable? ⇒ Boolean
99 100 101 |
# File 'lib/resourceful/header.rb', line 99 def repeatable? @repeatable end |
#set_to(value, raw_fields_hash) ⇒ Object
115 116 117 118 119 120 121 122 123 124 |
# File 'lib/resourceful/header.rb', line 115 def set_to(value, raw_fields_hash) raw_fields_hash[name] = if repeatable? Array(value) elsif value.kind_of?(Array) raise ArgumentError, "#{name} field may only have one value" if value.size > 1 value.first else value end end |