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

Setup new heurtistics set.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/vclog/heuristics.rb', line 22

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


79
80
81
# File 'lib/vclog/heuristics.rb', line 79

def types
  @types
end

Class Method Details

.load(file) ⇒ Object

Load heuristics from a designated file.

Raises:

  • (LoadError)


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

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.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/vclog/heuristics.rb', line 42

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


88
89
90
# File 'lib/vclog/heuristics.rb', line 88

def colors(*list)
  @colors = list
end

#defaultObject

Default settings.



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/vclog/heuristics.rb', line 93

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.



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/vclog/heuristics.rb', line 126

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

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

Define a new rule.



61
62
63
# File 'lib/vclog/heuristics.rb', line 61

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.



67
68
69
# File 'lib/vclog/heuristics.rb', line 67

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