Class: Resourceful::Header::FieldDesc

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/resourceful/header.rb

Overview

Class to handle the details of each type of field.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ FieldDesc

Create a new header field descriptor.

Parameters:

  • name (String)

    The canonical name of this field.

  • options (Hash) (defaults to: {})

    hash containing extra information about this header fields. Valid keys are:

    `:multivalued`
    `:multivalue`
    `:repeatable`
    :   Values of this field are comma separated list of values.  
        (n#VALUE per HTTP spec.) Default: false
    
    `:hop_by_hop`
    :   True if the header is a hop-by-hop header. Default: false
    
    `:modifiable`
    :   False if the header should not be modified by intermediates or caches. Default: true
    


126
127
128
129
130
131
132
133
# File 'lib/resourceful/header.rb', line 126

def initialize(name, options = {})
  @name = name
  options = Options.for(options).validate(:repeatable, :hop_by_hop, :modifiable)
  
  @repeatable = options.getopt([:repeatable, :multivalue, :multivalued]) || false
  @hop_by_hop = options.getopt(:hop_by_hop) || false
  @modifiable = options.getopt(:modifiable, true)
end

Instance Attribute Details

#nameObject (readonly) Also known as: to_s

Returns the value of attribute name.



105
106
107
# File 'lib/resourceful/header.rb', line 105

def name
  @name
end

Instance Method Details

#<=>(another) ⇒ Object



167
168
169
# File 'lib/resourceful/header.rb', line 167

def <=>(another)
  name <=> another.name
end

#==(another) ⇒ Object Also known as: eql?



171
172
173
# File 'lib/resourceful/header.rb', line 171

def ==(another)
  name_pattern === another.to_s
end

#===(another) ⇒ Object



176
177
178
179
180
181
182
# File 'lib/resourceful/header.rb', line 176

def ===(another)
  if another.kind_of?(FieldDesc)
    self == another
  else
    name_pattern === another
  end
end

#accessor_moduleObject



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/resourceful/header.rb', line 198

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_nameObject



192
193
194
# File 'lib/resourceful/header.rb', line 192

def constantized_name
  @constantized_name ||= name.upcase.gsub('-', '_')
end

#exists_in?(raw_fields_hash) ⇒ Boolean

Returns:

  • (Boolean)


163
164
165
# File 'lib/resourceful/header.rb', line 163

def exists_in?(raw_fields_hash)
  raw_fields_hash.has_key?(name)
end

#get_from(raw_fields_hash) ⇒ Object



148
149
150
# File 'lib/resourceful/header.rb', line 148

def get_from(raw_fields_hash)
  raw_fields_hash[name]
end

#hashObject



214
215
216
# File 'lib/resourceful/header.rb', line 214

def hash
  @name.hash
end

#hop_by_hop?Boolean

Returns:

  • (Boolean)


140
141
142
# File 'lib/resourceful/header.rb', line 140

def hop_by_hop?
  @hop_by_hop
end

#lookup_keys {|name| ... } ⇒ Object

Yields each commonly used lookup key for this header field.

Yields:



219
220
221
222
223
224
225
226
227
# File 'lib/resourceful/header.rb', line 219

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_nameObject



188
189
190
# File 'lib/resourceful/header.rb', line 188

def methodized_name
  @methodized_name ||= name.downcase.gsub('-', '_')
end

#modifiable?Boolean

Returns:

  • (Boolean)


144
145
146
# File 'lib/resourceful/header.rb', line 144

def modifiable?
  @modifiable
end

#name_patternObject



184
185
186
# File 'lib/resourceful/header.rb', line 184

def name_pattern
  @name_pattern || @name_pattern = Regexp.new('^' + name.gsub('-', '[_-]') + '$', Regexp::IGNORECASE)
end

#repeatable?Boolean Also known as: multivalued?

Returns:

  • (Boolean)


135
136
137
# File 'lib/resourceful/header.rb', line 135

def repeatable?
  @repeatable
end

#set_to(value, raw_fields_hash) ⇒ Object



152
153
154
155
156
157
158
159
160
161
# File 'lib/resourceful/header.rb', line 152

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