Class: Protege::Gateway::Mail::References

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/protege/gateway/mail/references.rb

Overview

Value object representing an ordered chain of Message-IDs from the References and In-Reply-To headers of an email thread.

Instances are frozen and Enumerable. Construct via .parse; build reply chains with #with_parent_appended rather than mutating.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message_ids) ⇒ References

Wrap a pre-parsed array of +MessageId+s and freeze. Prefer .parse.

Parameters:

  • message_ids (Array<MessageId>)

    the ordered chain entries.



38
39
40
41
# File 'lib/protege/gateway/mail/references.rb', line 38

def initialize(message_ids)
  @items = message_ids.dup.freeze
  freeze
end

Class Method Details

.parse(items) ⇒ References

Parse a list of raw header values into a References chain.

nil entries and blank/whitespace-only strings are dropped; surviving entries are normalized via MessageId.parse. Duplicates are preserved.

Parameters:

  • items (Array<String, nil>)

    raw message-id strings from +References+/+In-Reply-To+.

Returns:



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/protege/gateway/mail/references.rb', line 22

def parse(items)
  parsed = items.filter_map do |item|
    next if item.nil?

    stripped = item.to_s.strip
    next if stripped.empty?

    MessageId.parse(stripped)
  end
  new(parsed)
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

Compare by element-wise value equality.

Parameters:

  • other (Object)

    the value to compare against.

Returns:

  • (Boolean)

    true when other is a References with equal entries.



93
94
95
# File 'lib/protege/gateway/mail/references.rb', line 93

def ==(other)
  other.is_a?(References) && other.to_a == @items
end

#each {|message_id| ... } ⇒ Enumerator, References

Yield each MessageId in order.

Yields:

  • (message_id)

    each entry in chain order.

Yield Parameters:

  • message_id (MessageId)

    the current entry.

Returns:

  • (Enumerator, References)

    an enumerator without a block, else self.



48
49
50
# File 'lib/protege/gateway/mail/references.rb', line 48

def each(&)
  @items.each(&)
end

#empty?Boolean

Report whether the chain has no entries.

Returns:

  • (Boolean)

    true when empty.



62
63
64
# File 'lib/protege/gateway/mail/references.rb', line 62

def empty?
  @items.empty?
end

#firstMessageId?

Return the first MessageId in the chain.

Returns:

  • (MessageId, nil)

    the first entry, or nil when empty.



55
56
57
# File 'lib/protege/gateway/mail/references.rb', line 55

def first
  @items.first
end

#hashInteger

Hash by the underlying array contents.

Returns:

  • (Integer)

    the hash code.



101
102
103
# File 'lib/protege/gateway/mail/references.rb', line 101

def hash
  @items.hash
end

#sizeInteger

Return the number of entries.

Returns:

  • (Integer)

    the chain length.



69
70
71
# File 'lib/protege/gateway/mail/references.rb', line 69

def size
  @items.size
end

#to_aArray<MessageId>

Return the underlying entries as a frozen array.

Returns:



76
77
78
# File 'lib/protege/gateway/mail/references.rb', line 76

def to_a
  @items
end

#with_parent_appended(parent) ⇒ References

Return a new References with parent appended (immutable update).

Parameters:

  • parent (String, MessageId)

    the parent id; Strings are parsed, +MessageId+s reused.

Returns:

  • (References)

    a new chain with parent at the end.



84
85
86
87
# File 'lib/protege/gateway/mail/references.rb', line 84

def with_parent_appended(parent)
  message_id = parent.is_a?(MessageId) ? parent : MessageId.parse(parent)
  self.class.new(@items + [message_id])
end