Class TaskJuggler::JournalEntryList
In: lib/taskjuggler/Journal.rb
Parent: Object

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.

Methods

+   <<   []   count   delete   delete_if   each   empty?   first   include?   last   length   new   setSorting   sort!   uniq!  

Constants

SortingAttributes = [ :alert, :date, :seqno ]

Attributes

entries  [R] 

Public Class methods

[Source]

# File lib/taskjuggler/Journal.rb, line 197
    def initialize
      @entries = []
      @sorted = false
      @sortBy = [ [ :date, 1 ], [ :alert, 1 ], [ :seqno, 1 ] ]
    end

Public Instance methods

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

[Source]

# File lib/taskjuggler/Journal.rb, line 229
    def +(list)
      @entries += list.entries
      @sorted = false
      self
    end

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

[Source]

# File lib/taskjuggler/Journal.rb, line 222
    def <<(entry)
      @entries << entry
      @sorted = false
    end

Return the index-th entry.

[Source]

# File lib/taskjuggler/Journal.rb, line 236
    def[](index)
      sort!
      @entries[index]
    end

Return the number of entries.

[Source]

# File lib/taskjuggler/Journal.rb, line 217
    def count
      @entries.length
    end

Like Array::delete

[Source]

# File lib/taskjuggler/Journal.rb, line 250
    def delete(e)
      @entries.delete(e)
    end

Like Array::delete_if

[Source]

# File lib/taskjuggler/Journal.rb, line 255
    def delete_if
      @entries.delete_if { |e| yield(e) }
    end

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

[Source]

# File lib/taskjuggler/Journal.rb, line 242
    def each
      sort!
      @entries.each do |entry|
        yield entry
      end
    end

Like Array::empty?

[Source]

# File lib/taskjuggler/Journal.rb, line 260
    def empty?
      @entries.empty?
    end

Like Array::first but list is first sorted.

[Source]

# File lib/taskjuggler/Journal.rb, line 275
    def first
      sort!
      @entries.first
    end

Like Array::include?

[Source]

# File lib/taskjuggler/Journal.rb, line 270
    def include?(entry)
      @entries.include?(entry)
    end

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.

[Source]

# File lib/taskjuggler/Journal.rb, line 285
    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

Like Array:length

[Source]

# File lib/taskjuggler/Journal.rb, line 265
    def length
      @entries.length
    end

[Source]

# File lib/taskjuggler/Journal.rb, line 203
    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 the list of entries. First by ascending by date, than by alertLevel and finally by PropertyTreeNode sequence number.

[Source]

# File lib/taskjuggler/Journal.rb, line 307
    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

Eliminate duplicate entries.

[Source]

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

[Validate]