Class: VCLog::ChangeLog

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/vclog/changelog.rb

Overview

A ChangeLog encapsulates a list of Change objects.

Constant Summary collapse

DAY =

Seconds in a day.

24*60*60

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(changes = nil) ⇒ ChangeLog

Setup new ChangeLog instance.

Parameters:

  • changes (Array<Change>) (defaults to: nil)

    An array of Change objects.



30
31
32
33
# File 'lib/vclog/changelog.rb', line 30

def initialize(changes=nil)
  @changes = []
  @changes = changes if changes
end

Instance Attribute Details

#changesObject (readonly)

Array of Change or ChangePoint instances.



22
23
24
# File 'lib/vclog/changelog.rb', line 22

def changes
  @changes
end

Instance Method Details

#<<(entry) ⇒ Object

And a change to the changelog.



83
84
85
86
87
88
89
90
# File 'lib/vclog/changelog.rb', line 83

def <<(entry)
  case entry
  when Change, ChangePoint
    @changes << entry
  else
    #raise "Not a Change ro ChangePoint instance - #{entry.inspect}"
  end
end

#above(level) ⇒ Object

Return a new changelog with entries having a level higer or equal to the given level.



96
97
98
99
# File 'lib/vclog/changelog.rb', line 96

def above(level)
  above = changes.select{ |entry| entry.level >= level }
  self.class.new(above)
end

#after(date_limit) ⇒ Object

Return a new changelog with entries occuring after the given date limit.



105
106
107
108
# File 'lib/vclog/changelog.rb', line 105

def after(date_limit)
  after = changes.select{ |entry| entry.date > date_limit + DAY }
  self.class.new(after)
end

#before(date_limit) ⇒ Object

Return a new changelog with entries occuring before the given date limit.



114
115
116
117
# File 'lib/vclog/changelog.rb', line 114

def before(date_limit)
  before = changes.select{ |entry| entry.date <= date_limit + DAY }
  self.class.new(before)
end

#by_authorObject



136
137
138
139
140
141
142
143
# File 'lib/vclog/changelog.rb', line 136

def by_author
  mapped = {}
  changes.each do |entry|
    mapped[entry.author] ||= self.class.new
    mapped[entry.author] << entry
  end
  mapped
end

#by_dateObject



146
147
148
149
150
151
152
153
154
# File 'lib/vclog/changelog.rb', line 146

def by_date
  mapped = {}
  changes.each do |entry|
    mapped[entry.date.strftime('%Y-%m-%d')] ||= self.class.new
    mapped[entry.date.strftime('%Y-%m-%d')] << entry
  end
  mapped = mapped.to_a.sort{ |a,b| b[0] <=> a[0] }
  mapped
end

#by_typeObject



126
127
128
129
130
131
132
133
# File 'lib/vclog/changelog.rb', line 126

def by_type
  mapped = {}
  changes.each do |entry|
    mapped[entry.type] ||= self.class.new
    mapped[entry.type] << entry
  end
  mapped
end

#change(data = {}) ⇒ Object

Add a change entry to the log.



48
49
50
# File 'lib/vclog/changelog.rb', line 48

def change(data={})
  @changes << Change.new(data)
end

#each(&block) ⇒ Object

Iterate over each change.



62
63
64
# File 'lib/vclog/changelog.rb', line 62

def each(&block)
  changes.each(&block)
end

#empty?Boolean

Is the changelog void of any changes?

Returns:

  • (Boolean)


69
70
71
# File 'lib/vclog/changelog.rb', line 69

def empty?
  changes.empty?
end

#sizeObject

Return the number of changes in the changelog.



76
77
78
# File 'lib/vclog/changelog.rb', line 76

def size
  changes.size
end

#sort!(&block) ⇒ Object

Sort changes in place.



55
56
57
# File 'lib/vclog/changelog.rb', line 55

def sort!(&block)
  changes.sort!(&block)
end

#to_hArray<Hash>

TODO:

Not a Hash! Need to rename method.

Convert to list of hash.

Returns:

  • (Array<Hash>)


173
174
175
# File 'lib/vclog/changelog.rb', line 173

def to_h
  map{ |change| change.to_h }
end