Skip to content

Commit

Permalink
factored out some duplication in naming checks.
Browse files Browse the repository at this point in the history
  • Loading branch information
martinjandrews committed Sep 18, 2008
1 parent e606318 commit 32ed522
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 35 deletions.
17 changes: 6 additions & 11 deletions lib/roodi/checks/class_name_check.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
require 'roodi/checks/check'
require 'roodi/checks/name_check'

module Roodi
module Checks
# Checks a class name to make sure it matches the specified pattern.
#
# Keeping to a consistent nameing convention makes your code easier to read.
class ClassNameCheck < Check
class ClassNameCheck < NameCheck
DEFAULT_PATTERN = /^[A-Z][a-zA-Z0-9]*$/

def initialize(options = {})
super()
@pattern = options['pattern'] || DEFAULT_PATTERN
pattern = options['pattern'] || DEFAULT_PATTERN
super([:class], pattern, 'Class')
end

def interesting_nodes
[:class]
end

def evaluate(node)
class_name = node[1].class == Symbol ? node[1] : node[1].last
add_error "Class name \"#{class_name}\" should match pattern #{@pattern.inspect}" unless class_name.to_s =~ @pattern
def find_name(node)
node[1].class == Symbol ? node[1] : node[1].last
end
end
end
Expand Down
19 changes: 7 additions & 12 deletions lib/roodi/checks/method_name_check.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
require 'roodi/checks/check'
require 'roodi/checks/name_check'

module Roodi
module Checks
# Checks a method name to make sure it matches the specified pattern.
#
# Keeping to a consistent nameing convention makes your code easier to read.
class MethodNameCheck < Check
class MethodNameCheck < NameCheck
DEFAULT_PATTERN = /^[_a-z<>=\[\]|+-\/\*`]+[_a-z0-9_<>=~@\[\]]*[=!\?]?$/

def initialize(options = {})
super()
@pattern = options['pattern'] || DEFAULT_PATTERN
pattern = options['pattern'] || DEFAULT_PATTERN
super([:defn], pattern, 'Method')
end

def interesting_nodes
[:defn]
end

def evaluate(node)
method_name = node[1]
add_error "Method name \"#{method_name}\" should match pattern #{@pattern.inspect}" unless method_name.to_s =~ @pattern

def find_name(node)
node[1]
end
end
end
Expand Down
19 changes: 7 additions & 12 deletions lib/roodi/checks/module_name_check.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
require 'roodi/checks/check'
require 'roodi/checks/name_check'

module Roodi
module Checks
# Checks a module name to make sure it matches the specified pattern.
#
# Keeping to a consistent nameing convention makes your code easier to read.
class ModuleNameCheck < Check
class ModuleNameCheck < NameCheck
DEFAULT_PATTERN = /^[A-Z][a-zA-Z0-9]*$/

def initialize(options = {})
super()
@pattern = options['pattern'] || DEFAULT_PATTERN
pattern = options['pattern'] || DEFAULT_PATTERN
super([:module], pattern, 'Module')
end

def interesting_nodes
[:module]
end

def evaluate(node)
module_name = node[1].class == Symbol ? node[1] : node[1].last
add_error "Module name \"#{module_name}\" should match pattern #{@pattern.inspect}" unless module_name.to_s =~ @pattern

def find_name(node)
node[1].class == Symbol ? node[1] : node[1].last
end
end
end
Expand Down
23 changes: 23 additions & 0 deletions lib/roodi/checks/name_check.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require 'roodi/checks/check'

module Roodi
module Checks
class NameCheck < Check
def initialize(interesting_nodes, pattern, message_prefix)
super()
@interesting_nodes = interesting_nodes
@pattern = pattern
@message_prefix = message_prefix
end

def interesting_nodes
@interesting_nodes
end

def evaluate(node)
name = find_name(node)
add_error "#{@message_prefix} name \"#{name}\" should match pattern #{@pattern.inspect}" unless name.to_s =~ @pattern
end
end
end
end

0 comments on commit 32ed522

Please sign in to comment.