class TaskJuggler::JournalEntryList

The JournalEntryList is an Array with a twist. Before any data retrieval function is called, the list of JournalEntry objects will be sorted by date. This is a utility class only. Use Journal to store a journal.

Constants

SortingAttributes

Attributes

entries[R]

Public Class Methods

new() click to toggle source
# File lib/taskjuggler/Journal.rb, line 199
def initialize
  @entries = []
  @sorted = false
  @sortBy = [ [ :date, 1 ], [ :alert, 1 ], [ :seqno, 1 ] ]
end

Public Instance Methods

+(list) click to toggle source

Add a list of JournalEntry objects to the existing list. The list will be marked unsorted.

# File lib/taskjuggler/Journal.rb, line 231
def +(list)
  @entries += list.entries
  @sorted = false
  self
end
<<(entry) click to toggle source

Add a new JournalEntry to the list. The list will be marked as unsorted.

# File lib/taskjuggler/Journal.rb, line 224
def <<(entry)
  @entries << entry
  @sorted = false
end
[](index) click to toggle source

Return the index-th entry.

# File lib/taskjuggler/Journal.rb, line 238
def[](index)
  sort!
  @entries[index]
end
count() click to toggle source

Return the number of entries.

# File lib/taskjuggler/Journal.rb, line 219
def count
  @entries.length
end
delete(e) click to toggle source

Like Array::delete

# File lib/taskjuggler/Journal.rb, line 252
def delete(e)
  @entries.delete(e)
end
delete_if() { |e| ... } click to toggle source

Like Array::delete_if

# File lib/taskjuggler/Journal.rb, line 257
def delete_if
  @entries.delete_if { |e| yield(e) }
end
each() { |entry| ... } click to toggle source

The well known iterator. The list will be sorted first.

# File lib/taskjuggler/Journal.rb, line 244
def each
  sort!
  @entries.each do |entry|
    yield entry
  end
end
empty?() click to toggle source

Like Array::empty?

# File lib/taskjuggler/Journal.rb, line 262
def empty?
  @entries.empty?
end
first() click to toggle source

Like Array::first but list is first sorted.

# File lib/taskjuggler/Journal.rb, line 277
def first
  sort!
  @entries.first
end
include?(entry) click to toggle source

Like Array::include?

# File lib/taskjuggler/Journal.rb, line 272
def include?(entry)
  @entries.include?(entry)
end
last(date = nil) click to toggle source

Returns the last elements (by date) if date is nil or the last elements right before the given date. If there are multiple entries with exactly the same date, all are returned. Otherwise the result Array will only contain one element. In case no matching entry is found, the Array will be empty.

# File lib/taskjuggler/Journal.rb, line 287
def last(date = nil)
  result = JournalEntryList.new
  sort!

  @entries.reverse_each do |e|
    if result.empty?
      # We haven't found any yet. So add the first one we find before the
      # cut-off date.
      result << e if e.date <= date
    elsif result.first.date == e.date
      # Now we only accept other entries with the exact same date.
      result << e
    else
      # We've found all entries we are looking for.
      break
    end
  end
  result.sort!
end
length() click to toggle source

Like Array:length

# File lib/taskjuggler/Journal.rb, line 267
def length
  @entries.length
end
setSorting(by) click to toggle source
# File lib/taskjuggler/Journal.rb, line 205
def setSorting(by)
  by.each do |attr, direction|
    unless SortingAttributes.include?(attr)
      raise ArgumentError, "Unknown attribute #{attr}"
    end
    if (direction != 1) && (direction != -1)
      raise ArgumentError, "Unknown direction #{direction}"
    end
  end
  @sortBy = by
end
sort!() { |a, b| ... } click to toggle source

Sort the list of entries. First by ascending by date, than by alertLevel and finally by PropertyTreeNode sequence number.

# File lib/taskjuggler/Journal.rb, line 309
def sort!
  if block_given?
    @entries.sort! { |a, b| yield(a, b) }
  else
    return self if @sorted

    @entries.sort! do |a, b|
      res = 0
      @sortBy.each do |attr, direction|
        res = case attr
              when :date
                a.date <=> b.date
              when :alert
                a.alertLevel <=> b.alertLevel
              when :seqno
                a.property.sequenceNo <=> b.property.sequenceNo
              end * direction
        break if res != 0
      end
      res
    end
  end
  @sorted = true
  self
end
uniq!() click to toggle source

Eliminate duplicate entries.

# File lib/taskjuggler/Journal.rb, line 336
def uniq!
  @entries.uniq!
end