Class: TaskJuggler::JournalEntryList

Inherits:
Object
  • Object
show all
Defined in:
lib/taskjuggler/Journal.rb

Overview

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.

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeJournalEntryList

Returns a new instance of JournalEntryList.



200
201
202
203
204
# File 'lib/taskjuggler/Journal.rb', line 200

def initialize
  @entries = []
  @sorted = false
  @sortBy = [ [ :date, 1 ], [ :alert, 1 ], [ :seqno, 1 ] ]
end

Instance Attribute Details

#entriesObject (readonly)

Returns the value of attribute entries.



196
197
198
# File 'lib/taskjuggler/Journal.rb', line 196

def entries
  @entries
end

Instance Method Details

#+(list) ⇒ Object

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



232
233
234
235
236
# File 'lib/taskjuggler/Journal.rb', line 232

def +(list)
  @entries += list.entries
  @sorted = false
  self
end

#<<(entry) ⇒ Object

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



225
226
227
228
# File 'lib/taskjuggler/Journal.rb', line 225

def <<(entry)
  @entries << entry
  @sorted = false
end

#[](index) ⇒ Object

Return the index-th entry.



239
240
241
242
# File 'lib/taskjuggler/Journal.rb', line 239

def[](index)
  sort!
  @entries[index]
end

#countObject

Return the number of entries.



220
221
222
# File 'lib/taskjuggler/Journal.rb', line 220

def count
  @entries.length
end

#delete(e) ⇒ Object

Like Array::delete



253
254
255
# File 'lib/taskjuggler/Journal.rb', line 253

def delete(e)
  @entries.delete(e)
end

#delete_ifObject

Like Array::delete_if



258
259
260
# File 'lib/taskjuggler/Journal.rb', line 258

def delete_if
  @entries.delete_if { |e| yield(e) }
end

#eachObject

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



245
246
247
248
249
250
# File 'lib/taskjuggler/Journal.rb', line 245

def each
  sort!
  @entries.each do |entry|
    yield entry
  end
end

#empty?Boolean

Like Array::empty?

Returns:

  • (Boolean)


263
264
265
# File 'lib/taskjuggler/Journal.rb', line 263

def empty?
  @entries.empty?
end

#firstObject

Like Array::first but list is first sorted.



278
279
280
281
# File 'lib/taskjuggler/Journal.rb', line 278

def first
  sort!
  @entries.first
end

#include?(entry) ⇒ Boolean

Like Array::include?

Returns:

  • (Boolean)


273
274
275
# File 'lib/taskjuggler/Journal.rb', line 273

def include?(entry)
  @entries.include?(entry)
end

#last(date = nil) ⇒ Object

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.



288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/taskjuggler/Journal.rb', line 288

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

#lengthObject

Like Array:length



268
269
270
# File 'lib/taskjuggler/Journal.rb', line 268

def length
  @entries.length
end

#setSorting(by) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
# File 'lib/taskjuggler/Journal.rb', line 206

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!Object

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



310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/taskjuggler/Journal.rb', line 310

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!Object

Eliminate duplicate entries.



337
338
339
# File 'lib/taskjuggler/Journal.rb', line 337

def uniq!
  @entries.uniq!
end