Class: FashionPolice

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initializeFashionPolice

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

#errorsObject

Returns the value of attribute errors.



2
3
4
# File 'lib/fashion-police.rb', line 2

def errors
  @errors
end

#rulesObject

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

Returns:

  • (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

Returns:

  • (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.error_message}
    end
    memo
  end
end