class VCLog::Heuristics

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

Attributes

types[R]

Access to defined types.

@example

commit.type = :major

Public Class Methods

eval(script, file='(eval)', line=0) click to toggle source

Load heuristics given a script.

@param [String] script

Configuration script.
# File lib/vclog/heuristics.rb, line 28
def self.eval(script, file='(eval)', line=0)
  new{ instance_eval(text, file, line) }
end
load(file) click to toggle source

Load heuristics from a designated file.

@param [String] file

Configuration file.
# 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
new(&block) click to toggle source

Initialize new heurtistics set.

# 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

Public Instance Methods

apply(commit) click to toggle source

Apply heuristics to a commit.

@param [Change] commit

Instance of Change encapsulates an SCM commit.
# 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) click to toggle source

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

@example

colors :red, :yellow, :green, :cyan, :blue
# File lib/vclog/heuristics.rb, line 108
def colors(*list)
  @colors = list
end
default() click to toggle source

Default settings.

# 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
default2() click to toggle source

Work on next-gen default heuristics.

# 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) click to toggle source

Set default level.

# File lib/vclog/heuristics.rb, line 115
def level(integer=nil)
  @level = integer.to_i is integer
  @level
end
on(pattern=nil, &block) click to toggle source

Define a new rule.

# File lib/vclog/heuristics.rb, line 77
def on(pattern=nil, &block)
  @rules << Rule.new(pattern, &block)
end
set(type, level, label)

@deprecated

Alias for: type
type(type, level, label) click to toggle source

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

# File lib/vclog/heuristics.rb, line 85
def type(type, level, label)
  @types[type.to_sym] = Type.new(type, level, label)
end
Also aliased as: set