Class: FashionPolice
- Inherits:
-
Object
- Object
- FashionPolice
- Defined in:
- lib/fashion-police.rb
Defined Under Namespace
Classes: BadCode, ColumnWidth, FourSpaces, SpacesAroundArgumentsInAngleBrackets, SpacesAroundArgumentsInForLoops, SpacesAroundArgumentsInFunctionDeclarations, SpacesAroundArgumentsInParens, SpacesAroundArgumentsInSquareBrackets, SpacesAroundElses, SpacesAroundEqualsSigns, SpacesBeforeAngleBrackets, SpacesInFunctionDeclarations, SpacesNotTabs
Instance Attribute Summary collapse
-
#errors ⇒ Object
Returns the value of attribute errors.
-
#rules ⇒ Object
Returns the value of attribute rules.
Instance Method Summary collapse
- #comment?(line) ⇒ Boolean
-
#initialize ⇒ FashionPolice
constructor
A new instance of FashionPolice.
- #investigate(code) ⇒ Object
- #permit?(line_number, string) ⇒ Boolean
Constructor Details
#initialize ⇒ FashionPolice
Returns a new instance of FashionPolice.
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/fashion-police.rb', line 158 def initialize @errors = [] @rules = [ SpacesNotTabs.new, FourSpaces.new, SpacesInFunctionDeclarations.new, SpacesAroundElses.new, SpacesAroundEqualsSigns.new, SpacesAroundArgumentsInForLoops.new, SpacesAroundArgumentsInParens.new, SpacesBeforeAngleBrackets.new, SpacesAroundArgumentsInAngleBrackets.new, SpacesAroundArgumentsInSquareBrackets.new, SpacesAroundArgumentsInFunctionDeclarations.new, ColumnWidth.new ] end |
Instance Attribute Details
#errors ⇒ Object
Returns the value of attribute errors.
2 3 4 |
# File 'lib/fashion-police.rb', line 2 def errors @errors end |
#rules ⇒ Object
Returns the value of attribute rules.
2 3 4 |
# File 'lib/fashion-police.rb', line 2 def rules @rules end |
Instance Method Details
#comment?(line) ⇒ Boolean
182 183 184 |
# File 'lib/fashion-police.rb', line 182 def comment?(line) line.match(/^ *\/\//) end |
#investigate(code) ⇒ Object
174 175 176 177 178 179 180 |
# File 'lib/fashion-police.rb', line 174 def investigate(code) code.split("\n").each_with_index do |line, index| next if comment?(line) line_number = index + 1 raise(BadCode) unless permit?(line_number, line) end end |
#permit?(line_number, string) ⇒ Boolean
186 187 188 189 190 191 192 193 194 |
# File 'lib/fashion-police.rb', line 186 def permit?(line_number, string) @rules.inject(true) do |memo, rule| unless rule.test(string) memo = false @errors << {line_number => rule.} end memo end end |