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
Public Class Methods
# File lib/taskjuggler/Journal.rb, line 199 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.
# File lib/taskjuggler/Journal.rb, line 231 def +(list) @entries += list.entries @sorted = false self end
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
Return the index-th entry.
# File lib/taskjuggler/Journal.rb, line 238 def[](index) sort! @entries[index] end
Return the number of entries.
# File lib/taskjuggler/Journal.rb, line 219 def count @entries.length end
Like Array::delete
# File lib/taskjuggler/Journal.rb, line 252 def delete(e) @entries.delete(e) end
Like Array::delete_if
# File lib/taskjuggler/Journal.rb, line 257 def delete_if @entries.delete_if { |e| yield(e) } end
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
Like Array::empty?
# File lib/taskjuggler/Journal.rb, line 262 def empty? @entries.empty? end
Like Array::first but list is first sorted.
# File lib/taskjuggler/Journal.rb, line 277 def first sort! @entries.first end
Like Array::include?
# File lib/taskjuggler/Journal.rb, line 272 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.
# 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
Like Array:length
# File lib/taskjuggler/Journal.rb, line 267 def length @entries.length end
# 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 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
Eliminate duplicate entries.
# File lib/taskjuggler/Journal.rb, line 336 def uniq! @entries.uniq! end