Class: Tailor::Ruler
- Inherits:
-
Object
- Object
- Tailor::Ruler
- 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
Direct Known Subclasses
Tailor::Rulers::AllowCamelCaseMethodsRuler, Tailor::Rulers::AllowConditionalParenthesesRuler, Tailor::Rulers::AllowHardTabsRuler, Tailor::Rulers::AllowInvalidRubyRuler, Tailor::Rulers::AllowScreamingSnakeCaseClassesRuler, Tailor::Rulers::AllowTrailingLineSpacesRuler, Tailor::Rulers::AllowUnnecessaryDoubleQuotesRuler, Tailor::Rulers::AllowUnnecessaryInterpolationRuler, Tailor::Rulers::IndentationSpacesRuler, Tailor::Rulers::MaxCodeLinesInClassRuler, Tailor::Rulers::MaxCodeLinesInMethodRuler, Tailor::Rulers::MaxLineLengthRuler, Tailor::Rulers::SpacesAfterCommaRuler, Tailor::Rulers::SpacesAfterConditionalRuler, Tailor::Rulers::SpacesAfterLbraceRuler, Tailor::Rulers::SpacesAfterLbracketRuler, Tailor::Rulers::SpacesAfterLparenRuler, Tailor::Rulers::SpacesBeforeCommaRuler, Tailor::Rulers::SpacesBeforeLbraceRuler, Tailor::Rulers::SpacesBeforeRbraceRuler, Tailor::Rulers::SpacesBeforeRbracketRuler, Tailor::Rulers::SpacesBeforeRparenRuler, Tailor::Rulers::SpacesInEmptyBracesRuler, Tailor::Rulers::TrailingNewlinesRuler
Instance Attribute Summary collapse
-
#lexer_observers ⇒ Object
readonly
Returns the value of attribute lexer_observers.
Instance Method Summary collapse
-
#add_child_ruler(ruler) ⇒ Object
Adds the Ruler object to the list of child rulers.
-
#initialize(config = nil, options = { level: :error }) ⇒ Ruler
constructor
A new instance of Ruler.
-
#measure(*args) ⇒ Object
Each ruler should redefine this for its needs.
-
#problem_type ⇒ String
Converts the Ruler name to snake case.
-
#problems ⇒ Array
Gets all of the problems from all child rulers.
Methods included from Logger::Mixin
Constructor Details
#initialize(config = nil, options = { level: :error }) ⇒ Ruler
Returns a new instance of Ruler.
46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/tailor/ruler.rb', line 46 def initialize(config=nil, ={ level: :error }) @config = config @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_observers ⇒ Object (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.
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.
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_type ⇒ String
Converts the Tailor::Ruler name to snake case.
87 88 89 90 91 |
# File 'lib/tailor/ruler.rb', line 87 def problem_type self.class.to_s =~ /^.+::(\S+)Ruler$/ $1.underscore end |
#problems ⇒ Array
Gets all of the problems from all child rulers.
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 |