Class: FlogTask

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

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


33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/flog_task.rb', line 33

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.


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

def dirs
  @dirs
end

#methodObject

Method to use to score. Defaults to :total_score


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

def method
  @method
end

#nameObject

The name of the task. Defaults to :flog


7
8
9
# File 'lib/flog_task.rb', line 7

def name
  @name
end

#thresholdObject

Threshold to fail the task at. Default 200.


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

def threshold
  @threshold
end

#verboseObject

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


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

def verbose
  @verbose
end

Instance Method Details

#defineObject

Defines the flog task.


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/flog_task.rb', line 51

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