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::ALL_FILES, Core::Check::CONTROLLER_FILES, Core::Check::DEPLOY_FILES, Core::Check::HELPER_FILES, Core::Check::MAILER_FILES, Core::Check::MIGRATION_FILES, Core::Check::MODEL_FILES, Core::Check::PARTIAL_VIEW_FILES, Core::Check::ROUTE_FILES, Core::Check::SCHEMA_FILE, Core::Check::VIEW_FILES

Instance Method Summary collapse

Methods inherited from Core::Check

add_callback, #add_error, #after_prepare, #after_review, callbacks, #errors, #increment_total_files_checked!, #initialize, interesting_files, #interesting_files, #interesting_nodes, interesting_nodes, #method_missing, #node_end, #node_start, #parse_file?, #result, #total_files_checked

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

#model_associationsHash

get the model associations from Prepares.

Returns:

  • (Hash)


55
56
57
# File 'lib/rails_best_practices/reviews/review.rb', line 55

def model_associations
  @model_associations ||= Prepares.model_associations
end

#model_attributesHash

get the model attributes from Prepares.

Returns:

  • (Hash)


62
63
64
# File 'lib/rails_best_practices/reviews/review.rb', line 62

def model_attributes
  @model_attributes ||= Prepares.model_attributes
end

#modelsArray

get the models from Prepares.

Returns:

  • (Array)


48
49
50
# File 'lib/rails_best_practices/reviews/review.rb', line 48

def models
  @models ||= Prepares.models
end

#remember_variable_use_count(node) ⇒ Object

remember use count for the variable in the call or assign node.

find the variable in the call or assign 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
24
# File 'lib/rails_best_practices/reviews/review.rb', line 17

def remember_variable_use_count(node)
  variable_node = variable(node)
  if variable_node && "self" != variable_node.to_s && @last_variable_node != variable_node
    @last_variable_node = variable_node
    variable_use_count[variable_node.to_s] ||= 0
    variable_use_count[variable_node.to_s] += 1
  end
end

#reset_variable_use_countObject

reset @variable_use_count hash.



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

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 variable in the call or field node.



37
38
39
40
41
42
43
# File 'lib/rails_best_practices/reviews/review.rb', line 37

def variable(node)
  while [:call, :field, :method_add_arg, :method_add_block].include?(node.subject.sexp_type)
    node = node.subject
  end
  return if [:fcall, :hash].include?(node.subject.sexp_type)
  node.subject
end

#variable_use_countObject

return @variable_use_count hash.



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

def variable_use_count
  @variable_use_count ||= {}
end