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.
Add a new JournalEntry to the list. The list will be marked as unsorted.
# File lib/Journal.rb, line 79 79: def <<(entry) 80: @entries << entry 81: @sorted = false 82: end
Return the index-th entry.
# File lib/Journal.rb, line 93 93: def[](index) 94: sort! 95: @entries[index] 96: end
Return the number of entries.
# File lib/Journal.rb, line 74 74: def count 75: @entries.length 76: end
Like Array::delete_if
# File lib/Journal.rb, line 107 107: def delete_if 108: @entries.delete_if { |e| yield(e) } 109: end
The well known iterator. The list will be sorted first.
# File lib/Journal.rb, line 99 99: def each 100: sort! 101: @entries.each do |entry| 102: yield entry 103: end 104: end
Like Array::empty?
# File lib/Journal.rb, line 112 112: def empty? 113: @entries.empty? 114: end
Like Array::first but list is first sorted.
# File lib/Journal.rb, line 122 122: def first 123: sort! 124: @entries.first 125: end
Like Array::include?
# File lib/Journal.rb, line 117 117: def include?(entry) 118: @entries.include?(entry) 119: 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/Journal.rb, line 132 132: def last(date = nil) 133: result = JournalEntryList.new 134: sort! 135: 136: @entries.reverse_each do |e| 137: if result.empty? 138: # We haven't found any yet. So add the first one we find before the 139: # cut-off date. 140: result << e if e.date <= date 141: elsif result.first.date == e.date 142: # Now we only accept other entries with the exact same date. 143: result << e 144: else 145: # We've found all entries we are looking for. 146: break 147: end 148: end 149: result.sort! 150: end
Sort the list of entries. First by ascending by date, than by alertLevel and finally by PropertyTreeNode sequence number.
# File lib/Journal.rb, line 154 154: def sort! 155: if block_given? 156: @entries.sort! { |a, b| yield(a, b) } 157: else 158: return self if @sorted 159: 160: @entries.sort! { |a, b| a.date != b.date ? 161: a.date <=> b.date : 162: (a.alertLevel != b.alertLevel ? 163: a.alertLevel <=> b.alertLevel : 164: a.property.sequenceNo <=> 165: b.property.sequenceNo) } 166: end 167: @sorted = true 168: self 169: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.