Class: RailsBestPractices::Reviews::Review

Inherits:
Core::Check
  • Object
show all
Defined in:
lib/rails_best_practices/reviews/review.rb

Overview

A Review class that takes charge of reviewing one rails best practice.

Constant Summary

Constants inherited from Core::Check

Core::Check::CONTROLLER_FILES, Core::Check::HELPER_FILES, Core::Check::MAILER_FILES, Core::Check::MIGRATION_FILES, Core::Check::MODEL_FILES, Core::Check::NODE_TYPES, Core::Check::PARTIAL_VIEW_FILES, Core::Check::ROUTE_FILE, Core::Check::SCHEMA_FILE, Core::Check::VIEW_FILES

Instance Attribute Summary

Attributes inherited from Core::Check

#errors

Instance Method Summary collapse

Methods inherited from Core::Check

#add_error, #initialize, #interesting_files, #interesting_nodes, #method_missing, #node_end, #node_start

Constructor Details

This class inherits a constructor from RailsBestPractices::Core::Check

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class RailsBestPractices::Core::Check

Instance Method Details

#equal?(node, expected_node) ⇒ Boolean

compare two sexp nodes’ to_s.

equal?(":test", :test) => true
equai?("@test", :test) => true

Returns:

  • (Boolean)


88
89
90
91
92
# File 'lib/rails_best_practices/reviews/review.rb', line 88

def equal?(node, expected_node)
  actual = node.to_s.downcase
  expected = expected_node.to_s.downcase
  actual == expected || actual == ':' + expected || actual == '@' + expected
end

#model_associationsHash

get the model associations from Prepares.

Returns:

  • (Hash)


73
74
75
# File 'lib/rails_best_practices/reviews/review.rb', line 73

def model_associations
  @model_associations ||= Prepares.model_associations
end

#model_attributesHash

get the model attributes from Prepares.

Returns:

  • (Hash)


80
81
82
# File 'lib/rails_best_practices/reviews/review.rb', line 80

def model_attributes
  @model_attributes ||= Prepares.model_attributes
end

#modelsArray

get the models from Prepares.

Returns:

  • (Array)


66
67
68
# File 'lib/rails_best_practices/reviews/review.rb', line 66

def models
  @models ||= Prepares.models
end

#remember_variable_use_count(node) ⇒ Object

remember use count for the local or instance variable in the call or attrasgn node.

find the local variable or instance variable in the call or attrasgn node, then save it to as key in @variable_use_count hash, and add the call count (hash value).



17
18
19
20
21
22
23
# File 'lib/rails_best_practices/reviews/review.rb', line 17

def remember_variable_use_count(node)
  variable_node = variable(node)
  if variable_node
    variable_use_count[variable_node] ||= 0
    variable_use_count[variable_node] += 1
  end
end

#reset_variable_use_countObject

reset @variable_use_count hash.



31
32
33
# File 'lib/rails_best_practices/reviews/review.rb', line 31

def reset_variable_use_count
  @variable_use_count = nil
end

#urlObject

default url.



10
11
12
# File 'lib/rails_best_practices/reviews/review.rb', line 10

def url
  "#"
end

#variable(node) ⇒ Object

find local variable or instance variable in the most inner call node, e.g.

if the call node is

s(:call, s(:ivar, :@post), :editors, s(:arglist)),

or it is

s(:call,
  s(:call, s(:ivar, :@post), :editors, s(:arglist)),
  :include?,
  s(:arglist, s(:call, nil, :current_user, s(:arglist)))
)

then the variable both are s(:ivar, :@post).



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rails_best_practices/reviews/review.rb', line 51

def variable(node)
  while node.subject.node_type == :call
    node = node.subject
  end
  subject_node = node.subject
  if [:ivar, :lvar].include?(subject_node.node_type) and subject_node[1] != :_erbout
    subject_node
  else
    nil
  end
end

#variable_use_countObject

return @variable_use_count hash.



26
27
28
# File 'lib/rails_best_practices/reviews/review.rb', line 26

def variable_use_count
  @variable_use_count ||= {}
end