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.



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

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)



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

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



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

def create
  new []
end

.find_type(entry) ⇒ Object



275
276
277
278
279
280
# File 'lib/hermeneutics/message.rb', line 275

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

.parse(*list) ⇒ Object



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

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

.set_field_type(name, type) ⇒ Object



267
268
269
270
271
272
273
274
# File 'lib/hermeneutics/message.rb', line 267

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



337
338
339
340
341
342
# File 'lib/hermeneutics/message.rb', line 337

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



362
363
364
365
366
# File 'lib/hermeneutics/message.rb', line 362

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

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



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

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

#eachObject



317
318
319
320
# File 'lib/hermeneutics/message.rb', line 317

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

#field(name, type = nil) ⇒ Object



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

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

#has?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#insert(name, *contents) ⇒ Object



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

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

#inspectObject



438
439
440
441
442
443
444
# File 'lib/hermeneutics/message.rb', line 438

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

#lengthObject Also known as: size



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

def length
  @list.length
end

#raw(name) ⇒ Object



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

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

#recode(name, type = nil) ⇒ Object



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

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



368
369
370
371
372
373
374
375
# File 'lib/hermeneutics/message.rb', line 368

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



383
384
385
386
387
388
389
390
391
392
393
394
# File 'lib/hermeneutics/message.rb', line 383

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



415
416
417
418
419
420
421
422
423
424
425
426
# File 'lib/hermeneutics/message.rb', line 415

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



396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# File 'lib/hermeneutics/message.rb', line 396

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



304
305
306
# File 'lib/hermeneutics/message.rb', line 304

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