lib/tailor/file_line.rb in tailor-0.1.3 vs lib/tailor/file_line.rb in tailor-0.1.4
- old
+ new
@@ -5,34 +5,34 @@
require 'term/ansicolor'
module Tailor
# Calling modules will get the Ruby file to check, then read by line. This
- # class allows for checking of line-specific style by Represents a single
- # line of a file of Ruby code. Inherits from String so "self" can be used.
+ # class allows for checking of line-specific style by Represents a single
+ # line of a file of Ruby code. Inherits from String so "self" can be used.
#
# Methods are named such that they check for bad style conditions, and return
- # true and print the associated error message when the bad style condition
- # is discovered in the file line.
+ # true and print the associated error message when the bad style condition
+ # is discovered in the file line.
class FileLine < String
include Tailor::Spacing
include Tailor::Indentation
include Term::ANSIColor
LINE_LENGTH_MAX = 80
# This passes the line of code to String (the parent) so that it can act
- # like a standard string.
+ # like a standard string.
#
# @param [String] line_of_code Line from a Ruby file that will be checked
# for styling.
# @param [Pathname] file_path Path to the file the line is in.
# @param [Number] line_number Line number in the file that contains the
# line.
# @return [String] Returns a String that includes all of the methods
# defined here.
- def initialize line_of_code, file_path, line_number
+ def initialize(line_of_code, file_path, line_number)
super line_of_code
@file_path = file_path
@line_number = line_number
@line_problem_count = 0
@logger = ::Logger.new(STDOUT)
@@ -57,11 +57,11 @@
@line_problem_count += 1
print_problem "Method name uses camel case"
return true
end
- return false
+ false
end
# Checks to see if the class name is using snake case.
#
# @return [Boolean] Returns true if the class name is snake case.
@@ -79,11 +79,11 @@
@line_problem_count += 1
print_problem "Class name does NOT use camel case"
return true
end
- return false
+ false
end
# Checks to see if the line is the start of a method's definition.
#
# @return [Boolean] Returns true if the line starts with 'def'.
@@ -91,16 +91,15 @@
words = self.strip.split(/ /)
if words[0].eql? "def"
return true
end
- return false
+ false
end
- ##
# Returns the name of the method if the line is one that contains a method
- # definition.
+ # definition.
#
# @return [String] The method name.
def method_name
unless self.method_line?
return nil
@@ -118,54 +117,51 @@
words = self.split(/ /)
if words[0].eql? "class" and starts_with_uppercase?(words[1])
return true
end
- return false
+ false
end
- ##
# Checks to see if the line is a regular statement (not a class, method, or
- # comment).
+ # comment).
#
# @return [Boolean] Returns true if the line is not a class, method or
# comment.
def statement_line?
if self.method_line? or self.class_line? or self.comment_line?
return false
end
- return true
+ true
end
# Checks to see if the whole line is a basic comment line. This doesn't
- # check for trailing-line comments (@see #trailing_comment?).
+ # check for trailing-line comments (@see #trailing_comment?).
#
# @return [Boolean] Returns true if the line begins with a pound symbol.
def comment_line?
unless self.scan(/^\s*#/).empty?
return true
end
- return false
+ false
end
- ##
# Checks to see if the whole line is only space characters.
#
# @return [Boolean] Returns true if the line is only space characters.
def empty_line?
if self.scan(/^\s*$/).empty?
return false
end
- return true
+ true
end
- ##
# Checks to see if the line is greater than the defined max (80 chars is
- # default).
+ # default).
#
# @return [Boolean] Returns true if the line length exceeds the allowed
# length.
def too_long?
length = self.length
@@ -173,20 +169,19 @@
@line_problem_count += 1
print_problem "Line is >#{LINE_LENGTH_MAX} characters (#{length})"
return true
end
- return false
+ false
end
#-----------------------------------------------------------------
# Private methods
#-----------------------------------------------------------------
private
- ##
- # Prints the file name and line number that the problem occured on.
+ # Prints the file name and line number that the problem occurred on.
#
# @param [String] Error message to print.
def print_problem message
if @line_problem_count == 1
line_info = "Problems in"
@@ -194,10 +189,11 @@
line_info += " [#{@line_number}]:"
puts ""
puts line_info
end
+
puts red("\t"+ message)
end
# Checks to see if a word begins with a lowercase letter.
#
@@ -205,20 +201,20 @@
def starts_with_lowercase? word
if word =~ /^[a-z]/
return true
end
- return false
+ false
end
# Checks to see if a word begins with an uppercase letter.
#
# @param [String] word The word to check case on.
def starts_with_uppercase? word
if word =~ /^[A-Z]/
return true
end
- return false
+ false
end
end
-end
\ No newline at end of file
+end