Class: Tailor::Ruler

Inherits:
Object
  • Object
show all
Includes:
Logger::Mixin
Defined in:
lib/tailor/ruler.rb

Overview

This is a composite class, geared for getting at or managing the Rulers that should be used for measuring style. To do so, create a new object of this type, then add child rulers to that object using #add_child_ruler. After using the Ruler, you’ll have access to all of the problems found by all of the child rulers via #problems.

Example:

ruler = Ruler.new
file_length_ruler = FileLengthRuler.new
ruler.add_child_ruler(file_length_ruler)
# use ruler
ruler.problems      # => [{ all of your file length problems... }]

There’s really no measuring functionality in this base class–it’s merely for aggregating child data and for providing a base class to create child Rulers from. Speaking of… if you want, you can create your own rulers. A Ruler requires a few things:

First, it needs a list of Lexer events to observer. Tailor uses its Lexer to publish events (in this case, characters or string Ruby constructs) of interest to its observers. Rulers subscribe to those events so that they can detect the problems they’re looking for. These are defined as a Set in @lexer_observers. Adding to that list means the Ruler will subscribe to those events.

Example:

class MyRuler < Tailor::Ruler
  def initialize
    add_lexer_observers = :nl_observer, :kw_observer
  end
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logger::Mixin

included

Constructor Details

#initialize(config = nil, options = { level: :error }) ⇒ Ruler

Returns a new instance of Ruler.

Parameters:

  • config (Object) (defaults to: nil)
  • options (Hash) (defaults to: { level: :error })


46
47
48
49
50
51
52
53
54
55
56
# File 'lib/tailor/ruler.rb', line 46

def initialize(config=nil, options={ level: :error })
  @config = config
  @options = options
  @do_measurement = true
  log "Ruler initialized with style setting: #{@config}"
  log "Ruler initialized with problem level setting: #{@options[:level]}"

  @child_rulers = []
  @lexer_observers = []
  @problems = []
end

Instance Attribute Details

#lexer_observersObject (readonly)

Returns the value of attribute lexer_observers.



42
43
44
# File 'lib/tailor/ruler.rb', line 42

def lexer_observers
  @lexer_observers
end

Instance Method Details

#add_child_ruler(ruler) ⇒ Object

Adds the Tailor::Ruler object to the list of child rulers.

Parameters:



61
62
63
64
# File 'lib/tailor/ruler.rb', line 61

def add_child_ruler(ruler)
  @child_rulers << ruler
  log "Added child ruler: #{ruler}"
end

#measure(*args) ⇒ Object

Each ruler should redefine this for its needs.

Raises:



78
79
80
81
# File 'lib/tailor/ruler.rb', line 78

def measure(*args)
  raise RuntimeError,
    'Ruler#measure called, but should be redefined by a real ruler.'
end

#problem_typeString

Converts the Tailor::Ruler name to snake case.

Returns:

  • (String)

    The ruler name as snake-case that represents the problem that was found.



87
88
89
90
91
# File 'lib/tailor/ruler.rb', line 87

def problem_type
  self.class.to_s =~ /^.+::(\S+)Ruler$/

  $1.underscore
end

#problemsArray

Gets all of the problems from all child rulers.

Returns:

  • (Array)

    The list of problems.



69
70
71
72
73
74
75
# File 'lib/tailor/ruler.rb', line 69

def problems
  @problems = @child_rulers.inject(@problems) do |problems, ruler|
    problems + ruler.problems
  end

  @problems.sort_by! { |problem| problem[:line].to_i }
end