Class: Hermeneutics::Message::Header

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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, data) ⇒ Header

Returns a new instance of Header.



139
140
141
# File 'lib/hermeneutics/message.rb', line 139

def initialize name, data
  @name, @data, @contents = name, data
end

Class Attribute Details

.indentObject

Returns the value of attribute indent.



110
111
112
# File 'lib/hermeneutics/message.rb', line 110

def indent
  @indent
end

.line_maxObject

Returns the value of attribute line_max.



110
111
112
# File 'lib/hermeneutics/message.rb', line 110

def line_max
  @line_max
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



137
138
139
# File 'lib/hermeneutics/message.rb', line 137

def data
  @data
end

#nameObject (readonly)

Returns the value of attribute name.



137
138
139
# File 'lib/hermeneutics/message.rb', line 137

def name
  @name
end

Class Method Details

.build_name(name) ⇒ Object



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

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

.create(name, *contents) ⇒ Object



120
121
122
123
124
# File 'lib/hermeneutics/message.rb', line 120

def create name, *contents
  name = build_name name
  i = new name.to_s, nil
  i.set *contents
end

.parse(str) ⇒ Object



114
115
116
117
118
# File 'lib/hermeneutics/message.rb', line 114

def parse str
  str =~ /:\s*/ or
    raise ParseError, "Header line without a colon: #{str}"
  new $`, $'
end

Instance Method Details

#contents(type = nil) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/hermeneutics/message.rb', line 147

def contents type = nil
  if type then
    if @contents then
      if not @contents.is_a? type then
        @contents = type.parse @data
      end
    else
      @contents = type.parse @data
    end
  else
    unless @contents then
      @contents = @data
      @contents.strip!
      @contents.gsub! /\s\+/m, " "
    end
  end
  @contents
end

#name_is?(name) ⇒ Boolean

Returns:

  • (Boolean)


166
167
168
# File 'lib/hermeneutics/message.rb', line 166

def name_is? name
  (@name.casecmp name).zero?
end

#reset(type) ⇒ Object



191
192
193
194
195
196
197
198
199
# File 'lib/hermeneutics/message.rb', line 191

def reset type
  d = if type then
    (contents type).encode
  else
    @contents
  end
  @data = mk_lines d
  self
end

#set(*contents) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/hermeneutics/message.rb', line 170

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_sObject



143
144
145
# File 'lib/hermeneutics/message.rb', line 143

def to_s
  "#@name: #@data"
end