Class: Redwood::Container

Inherits:
Object show all
Defined in:
lib/sup/thread.rb

Overview

recursive structure used internally to represent message trees as described by reply-to: and references: headers.

the ‘id’ field is the same as the message id. but the message might be empty, in the case that we represent a message that was referenced by another message (as an ancestor) but never received.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id) ⇒ Container

Returns a new instance of Container.



166
167
168
169
170
171
# File 'lib/sup/thread.rb', line 166

def initialize id
  raise "non-String #{id.inspect}" unless id.is_a? String
  @id = id
  @message, @parent, @thread = nil, nil, nil
  @children = []
end

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



164
165
166
# File 'lib/sup/thread.rb', line 164

def children
  @children
end

#idObject

Returns the value of attribute id.



164
165
166
# File 'lib/sup/thread.rb', line 164

def id
  @id
end

#messageObject

Returns the value of attribute message.



164
165
166
# File 'lib/sup/thread.rb', line 164

def message
  @message
end

#parentObject

Returns the value of attribute parent.



164
165
166
# File 'lib/sup/thread.rb', line 164

def parent
  @parent
end

#threadObject

Returns the value of attribute thread.



164
165
166
# File 'lib/sup/thread.rb', line 164

def thread
  @thread
end

Instance Method Details

#==(o) ⇒ Object



188
# File 'lib/sup/thread.rb', line 188

def == o; Container === o && id == o.id; end

#dateObject



214
# File 'lib/sup/thread.rb', line 214

def date; find_attr :date; end

#descendant_of?(o) ⇒ Boolean

Returns:

  • (Boolean)


180
181
182
183
184
185
186
# File 'lib/sup/thread.rb', line 180

def descendant_of? o
  if o == self
    true
  else
    @parent && @parent.descendant_of?(o)
  end
end

#dump_recursive(f = $stdout, indent = 0, root = true, parent = nil) ⇒ Object



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/sup/thread.rb', line 225

def dump_recursive f=$stdout, indent=0, root=true, parent=nil
  raise "inconsistency" unless parent.nil? || parent.children.include?(self)
  unless root
    f.print " " * indent
    f.print "+->"
  end
  line = "[#{thread.nil? ? ' ' : '*'}] " + #"[#{useful? ? 'U' : ' '}] " +
    if @message
      message.subj ##{@message.refs.inspect} / #{@message.replytos.inspect}"
    else
      "<no message>"
    end

  f.puts "#{id} #{line}"#[0 .. (105 - indent)]
  indent += 3
  @children.each { |c| c.dump_recursive f, indent, false, self }
end

#each_with_stuff(parent = nil) {|_self, 0, parent| ... } ⇒ Object

Yields:

Yield Parameters:



173
174
175
176
177
178
# File 'lib/sup/thread.rb', line 173

def each_with_stuff parent=nil
  yield self, 0, parent
  @children.each do |c|
    c.each_with_stuff(self) { |cc, d, par| yield cc, d + 1, par }
  end
end

#empty?Boolean

Returns:

  • (Boolean)


190
# File 'lib/sup/thread.rb', line 190

def empty?; @message.nil?; end

#find_attr(attr) ⇒ Object



206
207
208
209
210
211
212
# File 'lib/sup/thread.rb', line 206

def find_attr attr
  if empty?
    @children.argfind { |c| c.find_attr attr }
  else
    @message.send attr
  end
end

#first_useful_descendantObject

skip over any containers which are empty and have only one child. we use this make the threaded display a little nicer, and only stick in the “missing message” line when it’s graphically necessary, i.e. when the missing message has more than one descendent.



198
199
200
201
202
203
204
# File 'lib/sup/thread.rb', line 198

def first_useful_descendant
  if empty? && @children.size == 1
    @children.first.first_useful_descendant
  else
    self
  end
end

#is_reply?Boolean

Returns:

  • (Boolean)


216
# File 'lib/sup/thread.rb', line 216

def is_reply?; subj && Message.subj_is_reply?(subj); end

#rootObject



192
# File 'lib/sup/thread.rb', line 192

def root; root? ? self : @parent.root; end

#root?Boolean

Returns:

  • (Boolean)


191
# File 'lib/sup/thread.rb', line 191

def root?; @parent.nil?; end

#subjObject



213
# File 'lib/sup/thread.rb', line 213

def subj; find_attr :subj; end

#to_sObject



218
219
220
221
222
223
# File 'lib/sup/thread.rb', line 218

def to_s
  [ "<#{id}",
    (@parent.nil? ? nil : "parent=#{@parent.id}"),
    (@children.empty? ? nil : "children=#{@children.map { |c| c.id }.inspect}"),
  ].compact.join(" ") + ">"
end