Class: TaskJuggler::JournalEntryList
- 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
-
#entries ⇒ Object
readonly
Returns the value of attribute entries.
Instance Method Summary collapse
-
#+(list) ⇒ Object
Add a list of JournalEntry objects to the existing list.
-
#<<(entry) ⇒ Object
Add a new JournalEntry to the list.
-
#[](index) ⇒ Object
Return the index-th entry.
-
#count ⇒ Object
Return the number of entries.
-
#delete(e) ⇒ Object
Like Array::delete.
-
#delete_if ⇒ Object
Like Array::delete_if.
-
#each ⇒ Object
The well known iterator.
-
#empty? ⇒ Boolean
Like Array::empty?.
-
#first ⇒ Object
Like Array::first but list is first sorted.
-
#include?(entry) ⇒ Boolean
Like Array::include?.
-
#initialize ⇒ JournalEntryList
constructor
A new instance of JournalEntryList.
-
#last(date = nil) ⇒ Object
Returns the last elements (by date) if date is nil or the last elements right before the given date.
-
#length ⇒ Object
Like Array:length.
- #setSorting(by) ⇒ Object
-
#sort! ⇒ Object
Sort the list of entries.
-
#uniq! ⇒ Object
Eliminate duplicate entries.
Constructor Details
#initialize ⇒ JournalEntryList
Returns a new instance of JournalEntryList.
199 200 201 202 203 |
# File 'lib/taskjuggler/Journal.rb', line 199 def initialize @entries = [] @sorted = false @sortBy = [ [ :date, 1 ], [ :alert, 1 ], [ :seqno, 1 ] ] end |
Instance Attribute Details
#entries ⇒ Object (readonly)
Returns the value of attribute entries.
195 196 197 |
# File 'lib/taskjuggler/Journal.rb', line 195 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.
231 232 233 234 235 |
# File 'lib/taskjuggler/Journal.rb', line 231 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.
224 225 226 227 |
# File 'lib/taskjuggler/Journal.rb', line 224 def <<(entry) @entries << entry @sorted = false end |
#[](index) ⇒ Object
Return the index-th entry.
238 239 240 241 |
# File 'lib/taskjuggler/Journal.rb', line 238 def[](index) sort! @entries[index] end |
#count ⇒ Object
Return the number of entries.
219 220 221 |
# File 'lib/taskjuggler/Journal.rb', line 219 def count @entries.length end |
#delete(e) ⇒ Object
Like Array::delete
252 253 254 |
# File 'lib/taskjuggler/Journal.rb', line 252 def delete(e) @entries.delete(e) end |
#delete_if ⇒ Object
Like Array::delete_if
257 258 259 |
# File 'lib/taskjuggler/Journal.rb', line 257 def delete_if @entries.delete_if { |e| yield(e) } end |
#each ⇒ Object
The well known iterator. The list will be sorted first.
244 245 246 247 248 249 |
# File 'lib/taskjuggler/Journal.rb', line 244 def each sort! @entries.each do |entry| yield entry end end |
#empty? ⇒ Boolean
Like Array::empty?
262 263 264 |
# File 'lib/taskjuggler/Journal.rb', line 262 def empty? @entries.empty? end |
#first ⇒ Object
Like Array::first but list is first sorted.
277 278 279 280 |
# File 'lib/taskjuggler/Journal.rb', line 277 def first sort! @entries.first end |
#include?(entry) ⇒ Boolean
Like Array::include?
272 273 274 |
# File 'lib/taskjuggler/Journal.rb', line 272 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.
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
# 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 ⇒ Object
Like Array:length
267 268 269 |
# File 'lib/taskjuggler/Journal.rb', line 267 def length @entries.length end |
#setSorting(by) ⇒ Object
205 206 207 208 209 210 211 212 213 214 215 |
# 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! ⇒ Object
Sort the list of entries. First by ascending by date, than by alertLevel and finally by PropertyTreeNode sequence number.
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 |
# 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! ⇒ Object
Eliminate duplicate entries.
336 337 338 |
# File 'lib/taskjuggler/Journal.rb', line 336 def uniq! @entries.uniq! end |