Class: Hermeneutics::Message::Headers

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

Defined Under Namespace

Classes: Entry

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(list) ⇒ Headers

Returns a new instance of Headers.



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

def initialize list
  @list = list
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object



416
417
418
419
420
421
422
# File 'lib/hermeneutics/message.rb', line 416

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

Class Method Details

.createObject



370
371
372
# File 'lib/hermeneutics/message.rb', line 370

def create
  new []
end

.find_type(entry) ⇒ Object



355
356
357
358
359
360
# File 'lib/hermeneutics/message.rb', line 355

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

.parse(*list) ⇒ Object



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

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

.set_field_type(name, type) ⇒ Object



347
348
349
350
351
352
353
354
# File 'lib/hermeneutics/message.rb', line 347

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

Instance Method Details

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



409
410
411
412
413
414
# File 'lib/hermeneutics/message.rb', line 409

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



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

def add name, *contents
  e = build_entry name, *contents
  add_entry e
  self
end

#eachObject



388
389
390
391
392
393
394
395
# File 'lib/hermeneutics/message.rb', line 388

def each
  @list.each { |e|
    type = Headers.find_type e
    c = e.contents type
    yield e.name, c
  }
  self
end

#field(name, type = nil) ⇒ Object



402
403
404
405
406
407
408
# File 'lib/hermeneutics/message.rb', line 402

def field name, type = nil
  e = find_entry name
  if e then
    type ||= Headers.find_type e
    e.contents type
  end
end

#inspectObject



454
455
456
457
458
459
460
# File 'lib/hermeneutics/message.rb', line 454

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

#lengthObject Also known as: size



379
380
381
# File 'lib/hermeneutics/message.rb', line 379

def length
  @list.length
end

#raw(name) ⇒ Object



397
398
399
400
# File 'lib/hermeneutics/message.rb', line 397

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

#recode(name, type = nil) ⇒ Object



444
445
446
447
448
449
450
451
452
# File 'lib/hermeneutics/message.rb', line 444

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

#remove(name) ⇒ Object Also known as: delete



437
438
439
440
441
# File 'lib/hermeneutics/message.rb', line 437

def remove name
  e = Entry.create name
  remove_entries e
  self
end

#replace(name, *contents) ⇒ Object



430
431
432
433
434
435
# File 'lib/hermeneutics/message.rb', line 430

def replace name, *contents
  e = build_entry name, *contents
  remove_entries e
  add_entry e
  self
end

#to_sObject



384
385
386
# File 'lib/hermeneutics/message.rb', line 384

def to_s
  @list.map { |e| "#{e}#$/" }.join
end