Class: FlogTask

Inherits:
Rake::TaskLib
  • Object
show all
Defined in:
lib/flog_task.rb

Overview

A rake front-end for flog, allowing task creation with options for verbosity, reporting, and a failure threshold.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = :flog, threshold = 200, dirs = nil, method = nil, methods_only = false) {|_self| ... } ⇒ FlogTask

Creates a new FlogTask instance with given name, threshold, dirs, and method.

Yields:

  • (_self)

Yield Parameters:

  • _self (FlogTask)

    the object that the method was called on



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/flog_task.rb', line 37

def initialize name = :flog, threshold = 200, dirs = nil, method = nil, methods_only = false
  @name         = name
  @dirs         = dirs || %w(app bin lib spec test)
  @threshold    = threshold
  @method       = method || :total_score
  @verbose      = Rake.application.options.trace
  @methods_only = methods_only

  yield self if block_given?

  @dirs.reject! { |f| ! File.directory? f }

  define
end

Instance Attribute Details

#dirsObject

What directories to operate on. Sensible defaults.



16
17
18
# File 'lib/flog_task.rb', line 16

def dirs
  @dirs
end

#methodObject

Method to use to score. Defaults to :total_score



31
32
33
# File 'lib/flog_task.rb', line 31

def method
  @method
end

#nameObject

The name of the task. Defaults to :flog



11
12
13
# File 'lib/flog_task.rb', line 11

def name
  @name
end

#thresholdObject

Threshold to fail the task at. Default 200.



21
22
23
# File 'lib/flog_task.rb', line 21

def threshold
  @threshold
end

#verboseObject

Verbosity of output. Defaults to rake’s trace (-t) option.



26
27
28
# File 'lib/flog_task.rb', line 26

def verbose
  @verbose
end

Instance Method Details

#defineObject

Defines the flog task.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/flog_task.rb', line 55

def define
  desc "Analyze for code complexity in: #{dirs.join(', ')}"
  task name do
    require "flog_cli"
    flog = FlogCLI.new :continue => true, :quiet => true, :methods => @methods_only

    expander = PathExpander.new dirs, "**/*.{rb,rake}"
    files = expander.process

    flog.flog(*files)

    desc, score = flog.send method
    desc, score = "total", desc unless score # total only returns a number

    flog.report if verbose

    raise "Flog score for #{desc} is too high! #{score} > #{threshold}" if
      score > threshold
  end

  self
end