Class: RuboCop::Cop::DarkFinger::ModelStructure

Inherits:
Cop
  • Object
show all
Defined in:
lib/rubocop/cop/dark_finger/model_structure.rb

Defined Under Namespace

Classes: InvalidConfigError

Constant Summary collapse

ASSOCIATION =
:association
ATTRIBUTES =
:attribute
CALLBACK =
:callback
CLASS_METHOD =
:class_method
CONSTANT =
:constant
CONSTRUCTOR =
:constructor
ENUM =
:enum
INCLUDE =
:include
INSTANCE_METHOD =
:instance_method
MISC =
:misc
MODULE =
:module
SCOPE =
:scope
VALIDATION =
:validation
KNOWN_ELEMENTS =
[
  ASSOCIATION,
  ATTRIBUTES,
  CALLBACK,
  CLASS_METHOD,
  CONSTANT,
  CONSTRUCTOR,
  ENUM,
  INCLUDE,
  INSTANCE_METHOD,
  MISC,
  MODULE,
  SCOPE,
  VALIDATION,
]
DEFAULT_REQUIRED_ORDER =
[
  MODULE,
  INCLUDE,
  ENUM,
  CONSTANT,
  ASSOCIATION,
  VALIDATION,
  SCOPE,
  ATTRIBUTES,
  CALLBACK,
  MISC,
  CONSTRUCTOR,
  CLASS_METHOD,
  INSTANCE_METHOD,
]
DEFAULT_REQUIRED_COMMENTS =
{
  ASSOCIATION => '# Relationships',
  ATTRIBUTES => '# Attributes',
  CALLBACK => '# Callbacks',
  CONSTANT => '# Constants',
  ENUM => '# Enums',
  INCLUDE => '# Includes',
  MODULE => '# Modules',
  SCOPE => '# Scopes',
  VALIDATION => '# Validations'
}
DEFAULT_MISC_METHOD_NAMES =
[]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, options) ⇒ ModelStructure

Returns a new instance of ModelStructure.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/rubocop/cop/dark_finger/model_structure.rb', line 71

def initialize(*args, options)
  super
  @class_elements_seen = []
  @required_order = options[:required_order] || cop_config['required_order'] || DEFAULT_REQUIRED_ORDER
  @required_comments = options[:required_comments] || cop_config['required_comments'] || DEFAULT_REQUIRED_COMMENTS
  @misc_method_names = options[:misc_method_names] || cop_config['misc_method_names'] || DEFAULT_MISC_METHOD_NAMES

  # symbolize configs
  @required_order.map!(&:to_sym)
  @required_comments = Hash[ @required_comments.map {|k,v| [k.to_sym, v]} ]
  @misc_method_names.map!(&:to_sym)

  validate_config!
end

Instance Attribute Details

#misc_method_namesObject (readonly)

Returns the value of attribute misc_method_names.



69
70
71
# File 'lib/rubocop/cop/dark_finger/model_structure.rb', line 69

def misc_method_names
  @misc_method_names
end

#required_commentsObject (readonly)

Returns the value of attribute required_comments.



69
70
71
# File 'lib/rubocop/cop/dark_finger/model_structure.rb', line 69

def required_comments
  @required_comments
end

#required_orderObject (readonly)

Returns the value of attribute required_order.



69
70
71
# File 'lib/rubocop/cop/dark_finger/model_structure.rb', line 69

def required_order
  @required_order
end

Instance Method Details

#on_casgn(node) ⇒ Object



90
91
92
# File 'lib/rubocop/cop/dark_finger/model_structure.rb', line 90

def on_casgn(node)
  process_node(node, seen_element: CONSTANT)
end

#on_def(node) ⇒ Object



98
99
100
101
102
103
104
105
# File 'lib/rubocop/cop/dark_finger/model_structure.rb', line 98

def on_def(node)
  seen_element = if node.method_name == :initialize
                   CONSTRUCTOR
                 else
                   INSTANCE_METHOD
                 end
  process_node(node, seen_element: seen_element)
end

#on_defs(node) ⇒ Object



107
108
109
# File 'lib/rubocop/cop/dark_finger/model_structure.rb', line 107

def on_defs(node)
  process_node(node, seen_element: CLASS_METHOD)
end

#on_module(node) ⇒ Object



94
95
96
# File 'lib/rubocop/cop/dark_finger/model_structure.rb', line 94

def on_module(node)
  process_node(node, seen_element: MODULE)
end

#on_send(node) ⇒ Object



86
87
88
# File 'lib/rubocop/cop/dark_finger/model_structure.rb', line 86

def on_send(node)
  process_node(node)
end