Class: Hermeneutics::Message::Headers

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/hermeneutics/message.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(list) ⇒ Headers

Returns a new instance of Headers.



291
292
293
# File 'lib/hermeneutics/message.rb', line 291

def initialize list
  @list = list
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, type = nil, *args) ⇒ Object (private)



342
343
344
345
346
347
348
# File 'lib/hermeneutics/message.rb', line 342

def method_missing sym, type = nil, *args
  if args.empty? and not sym =~ /[!?=]\z/ then
    field sym, type
  else
    super
  end
end

Class Method Details

.createObject



286
287
288
# File 'lib/hermeneutics/message.rb', line 286

def create
  new []
end

.find_type(entry) ⇒ Object



271
272
273
274
275
276
# File 'lib/hermeneutics/message.rb', line 271

def find_type entry
  @types.each { |k,v|
    return v if entry.name_is? k
  }
  nil
end

.parse(*list) ⇒ Object



281
282
283
284
285
# File 'lib/hermeneutics/message.rb', line 281

def parse *list
  list.flatten!
  list.map! { |h| Header.parse h }
  new list
end

.set_field_type(name, type) ⇒ Object



263
264
265
266
267
268
269
270
# File 'lib/hermeneutics/message.rb', line 263

def set_field_type name, type
  e = Header.create name
  if type then
    @types[ e.name] = type
  else
    @types.delete e.name
  end
end

Instance Method Details

#[](name, type = nil) ⇒ Object



333
334
335
336
337
338
# File 'lib/hermeneutics/message.rb', line 333

def [] name, type = nil
  case name
    when Integer then raise "Not a field name: #{name}"
    else              field name, type
  end
end

#add(name, *contents) ⇒ Object



358
359
360
361
362
# File 'lib/hermeneutics/message.rb', line 358

def add name, *contents
  e = build_entry name, *contents
  @list.push e
  self
end

#compact(name, type = nil) ⇒ Object Also known as: remove_empty



374
375
376
# File 'lib/hermeneutics/message.rb', line 374

def compact name, type = nil
  remove name, type do |c| c.empty? end
end

#eachObject



313
314
315
316
# File 'lib/hermeneutics/message.rb', line 313

def each
  @list.each { |e| yield e.name, (header_contents e) }
  self
end

#field(name, type = nil) ⇒ Object



329
330
331
332
# File 'lib/hermeneutics/message.rb', line 329

def field name, type = nil
  e = find_entry name
  header_contents e, type if e
end

#has?(name) ⇒ Boolean

Returns:

  • (Boolean)


319
320
321
322
# File 'lib/hermeneutics/message.rb', line 319

def has? name
  e = find_entry name
  not e.nil?
end

#insert(name, *contents) ⇒ Object



352
353
354
355
356
# File 'lib/hermeneutics/message.rb', line 352

def insert name, *contents
  e = build_entry name, *contents
  @list.unshift e
  self
end

#inspectObject



434
435
436
437
438
439
440
# File 'lib/hermeneutics/message.rb', line 434

def inspect
  r = ""
  r << "#<#{self.class}:"
  r << "0x%x" % (object_id<<1)
  r << " (#{length})"
  r << ">"
end

#lengthObject Also known as: size



295
296
297
# File 'lib/hermeneutics/message.rb', line 295

def length
  @list.length
end

#raw(name) ⇒ Object



324
325
326
327
# File 'lib/hermeneutics/message.rb', line 324

def raw name
  e = find_entry name
  e.data if e
end

#recode(name, type = nil) ⇒ Object



424
425
426
427
428
429
430
431
432
# File 'lib/hermeneutics/message.rb', line 424

def recode name, type = nil
  n = Header.build_name name
  @list.each { |e|
    next unless e.name_is? n
    type ||= Headers.find_type e
    e.reset type
  }
  self
end

#remove(name, type = nil) ⇒ Object Also known as: delete



364
365
366
367
368
369
370
371
# File 'lib/hermeneutics/message.rb', line 364

def remove name, type = nil
  block_given? or return remove name, type do |_| true end
  pat = Header.create name
  @list.reject! { |e|
    e.name_is? pat.name and yield (header_contents e, type)
  }
  self
end

#replace(name, *contents) ⇒ Object



379
380
381
382
383
384
385
386
387
388
389
390
# File 'lib/hermeneutics/message.rb', line 379

def replace name, *contents
  block_given? or return replace name, *contents do true end
  entry = build_entry name, *contents
  @list.map! { |e|
    if e.name_is? entry.name and yield (header_contents e) then
      entry
    else
      e
    end
  }
  self
end

#replace_add(name, *contents) ⇒ Object



411
412
413
414
415
416
417
418
419
420
421
422
# File 'lib/hermeneutics/message.rb', line 411

def replace_add name, *contents
  entry = build_entry name, *contents
  replace_all name do |c|
    if entry then
      c.push entry.contents
      entry = nil
    end
    c
  end
  @list.push entry if entry
  self
end

#replace_all(name, type = nil) ⇒ Object



392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
# File 'lib/hermeneutics/message.rb', line 392

def replace_all name, type = nil
  pat = Header.create name
  type ||= Headers.find_type pat
  @list.map! { |e|
    if e.name_is? pat.name then
      c = e.contents type
      y = yield c
      if y.equal? c then
        e.reset type
      else
        y = [ type, *y] if type
        next build_entry name, *y
      end
    end
    e
  }
  self
end

#to_sObject



300
301
302
# File 'lib/hermeneutics/message.rb', line 300

def to_s
  @list.map { |e| "#{e}\n" }.join
end