Class: VCLog::Heuristics

Inherits:
Object
  • Object
show all
Defined in:
lib/vclog/heuristics.rb,
lib/vclog/heuristics/rule.rb,
lib/vclog/heuristics/type.rb

Overview

Heuristics stores a set of rules to be applied to commmits in order to assign them priority levels and report labels.

Defined Under Namespace

Classes: Rule, Type

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Heuristics

Initialize new heurtistics set.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/vclog/heuristics.rb', line 35

def initialize(&block)
  @rules = []

  @types = Hash.new{ |h,k| h[k] = h[:default] }
  @types[:default] = Type.new(:default, -1, "Nominal Changes")

  @colors = [:blue, :blue, :cyan, :green, :yellow, :red, :red]

  if block
    instance_eval(&block)
  else
    default
  end
end

Instance Attribute Details

#typesObject (readonly)

Access to defined types.

Examples:

commit.type = :major


98
99
100
# File 'lib/vclog/heuristics.rb', line 98

def types
  @types
end

Class Method Details

.eval(script, file = '(eval)', line = 0) ⇒ Object

Load heuristics given a script.

Parameters:

  • script (String)

    Configuration script.



28
29
30
# File 'lib/vclog/heuristics.rb', line 28

def self.eval(script, file='(eval)', line=0)
  new{ instance_eval(text, file, line) }
end

.load(file) ⇒ Object

Load heuristics from a designated file.

Parameters:

  • file (String)

    Configuration file.

Raises:

  • (LoadError)


17
18
19
20
# File 'lib/vclog/heuristics.rb', line 17

def self.load(file)
  raise LoadError unless File.exist?(file)
  new{ instance_eval(File.read(file), file) }
end

Instance Method Details

#apply(commit) ⇒ Object

Apply heuristics to a commit.

Parameters:

  • commit (Change)

    Instance of Change encapsulates an SCM commit.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/vclog/heuristics.rb', line 56

def apply(commit)
  # apply rules, breaking on first rule found that fits.
  @rules.find{ |rule| rule.call(commit) }

  unless commit.level
    commit.level = types[commit.type].level
  end

  unless commit.label
    commit.label = types[commit.type].label
  end

  # apply color for commit level
  color = @colors[commit.level + (@colors.size / 2)]
  color ||= (commit.level > 0 ? @colors.first : @colors.last)
  commit.color = color
end

#colors(*list) ⇒ Object

Set color list. The center element cooresponds to ‘level=0`. Elements before the center are incrementally higher levels and those after are lower.

Examples:

colors :red, :yellow, :green, :cyan, :blue


108
109
110
# File 'lib/vclog/heuristics.rb', line 108

def colors(*list)
  @colors = list
end

#defaultObject

Default settings.



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/vclog/heuristics.rb', line 123

def default
  type :major,    3, "Major Enhancements"
  type :minor,    2, "Minor Enhancements"
  type :bug,      1, "Bug Fixes"
  type :default,  0, "Nominal Changes"
  type :doc,     -1, "Documentation Changes"
  type :test,    -2, "Test/Spec Adjustments"
  type :admin,   -3, "Administrative Changes"

  on /\A(\w+):/ do |commit, md|
    type = md[1].to_sym
    commit.type    = type
    commit.message = commit.message.sub(md[0],'').strip
    true
  end

  on /\[(\w+)\]\s*$/ do |commit, md|
    type = md[1].to_sym
    commit.type    = type
    commit.message = commit.message.sub(md[0],'').strip
    true
  end

  on /updated? (README|PROFILE|PACKAGE|VERSION|MANIFEST)/ do |commit|
    commit.type = :admin
  end

  on /(bump|bumped|prepare) version/ do |commit|
    commit.type = :admin
  end
end

#default2Object

Work on next-gen default heuristics.



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/vclog/heuristics.rb', line 158

def default2
  type :major,    3, "Major Enhancements"
  type :minor,    2, "Minor Enhancements"
  type :bug,      1, "Bug Fixes"
  type :default,  0, "Nominal Changes"
  type :doc,     -1, "Documentation Changes"
  type :test,    -2, "Test/Spec Adjustments"
  type :admin,   -3, "Administrative Changes"

  # test/spec file only changes
  on do |commit|
    if commit.files.all?{ |f| f.start_with?('test') || f.start_with?('spec') }
      commit.type = :test
    end
  end
end

#level(integer = nil) ⇒ Object

Set default level.



115
116
117
118
# File 'lib/vclog/heuristics.rb', line 115

def level(integer=nil)
  @level = integer.to_i is integer
  @level
end

#on(pattern = nil, &block) ⇒ Object

Define a new rule.



77
78
79
# File 'lib/vclog/heuristics.rb', line 77

def on(pattern=nil, &block)
  @rules << Rule.new(pattern, &block)
end

#type(type, level, label) ⇒ Object Also known as: set

Convenience method for setting-up commit types, which can be easily assigned, setting both label and level in one go.



85
86
87
# File 'lib/vclog/heuristics.rb', line 85

def type(type, level, label)
  @types[type.to_sym] = Type.new(type, level, label)
end