class Hermes::Message::Headers::Entry

Constants

INDENT
LINE_LENGTH

Attributes

data[R]
name[R]
sep[R]

Public Class Methods

new(name, sep, data) click to toggle source
# File lib/hermes/message.rb, line 240
def initialize name, sep, data
  @name, @sep, @data, @contents = name, sep, data
end

Public Instance Methods

build_name(name) click to toggle source
# File lib/hermes/message.rb, line 230
def build_name name
  n = name.to_s
  unless n.equal? name then
    n.gsub! /_/, "-"
    n.gsub! /\b[a-z]/ do |c| c.upcase end
  end
  n
end
contents(type) click to toggle source
# File lib/hermes/message.rb, line 246
def contents type
  if type then
    unless @contents and @contents.is_a? type then
      @contents = type.parse @data
    end
    @contents
  else
    @data
  end
end
create(name, *contents) click to toggle source
# File lib/hermes/message.rb, line 225
def create name, *contents
  name = build_name name
  i = new name.to_s, ": ", nil
  i.set *contents
end
name_is?(name) click to toggle source
# File lib/hermes/message.rb, line 256
def name_is? name
  (@name.casecmp name).zero?
end
parse(str) click to toggle source
# File lib/hermes/message.rb, line 219
def parse str
  str =~ /:\s*/ or
    raise ParseError, "Header line without a colon: #{str}"
  data = $'
  new $`, $&, data
end
reset(type) click to toggle source
# File lib/hermes/message.rb, line 279
def reset type
  if type then
    c = contents type
    @data = mk_lines c.encode if c
  end
  self
end
set(*contents) click to toggle source
# File lib/hermes/message.rb, line 259
def set *contents
  type, *args = *contents
  d = case type
    when Class then
      @contents = type.new *args
      case (e = @contents.encode)
        when Array then e
        when nil   then []
        else            [ e]
      end
    when nil then
      @contents = nil
      split_args args
    else
      @contents = nil
      split_args contents
  end
  @data = mk_lines d
  self
end
to_s() click to toggle source
# File lib/hermes/message.rb, line 243
def to_s
  "#@name#@sep#@data"
end

Private Instance Methods

mk_lines(strs) click to toggle source
# File lib/hermes/message.rb, line 287
def mk_lines strs
  m = LINE_LENGTH - @name.length - @sep.length
  data = ""
  strs.each { |e|
    unless data.empty? then
      if 1 + e.length <= m then
        data << " "
        m -= 1
      else
        data << $/ << INDENT
        m = LINE_LENGTH - INDENT.length
      end
    end
    data << e
    m -= e.length
  }
  data
end
split_args(ary) click to toggle source
# File lib/hermes/message.rb, line 305
def split_args ary
  r = []
  ary.each { |a|
    r.concat case a
      when Array then split_args a
      else            a.to_s.split
    end
  }
  r
end