class RubbyCop::Cop::Style::ClassAndModuleChildren
This cop checks the style of children definitions at classes and modules. Basically there are two different styles:
nested - have each child on its own line
class Foo class Bar end end
compact - combine definitions as much as possible
class Foo::Bar end
The compact style is only forced for classes/modules with one child.
Constants
- COMPACT_MSG
- NESTED_MSG
Public Instance Methods
on_class(node)
click to toggle source
# File lib/rubbycop/cop/style/class_and_module_children.rb, line 28 def on_class(node) _name, superclass, body = *node return if superclass && style != :nested check_style(node, body) end
on_module(node)
click to toggle source
# File lib/rubbycop/cop/style/class_and_module_children.rb, line 34 def on_module(node) _name, body = *node check_style(node, body) end
Private Instance Methods
check_compact_style(node, body)
click to toggle source
# File lib/rubbycop/cop/style/class_and_module_children.rb, line 54 def check_compact_style(node, body) return unless one_child?(body) && !compact_node_name?(node) add_offense(node, :name, COMPACT_MSG) end
check_nested_style(node)
click to toggle source
# File lib/rubbycop/cop/style/class_and_module_children.rb, line 49 def check_nested_style(node) return unless compact_node_name?(node) add_offense(node, :name, NESTED_MSG) end
check_style(node, body)
click to toggle source
# File lib/rubbycop/cop/style/class_and_module_children.rb, line 41 def check_style(node, body) if style == :nested check_nested_style(node) else check_compact_style(node, body) end end
compact_node_name?(node)
click to toggle source
# File lib/rubbycop/cop/style/class_and_module_children.rb, line 63 def compact_node_name?(node) node.loc.name.source =~ /::/ end
one_child?(body)
click to toggle source
# File lib/rubbycop/cop/style/class_and_module_children.rb, line 59 def one_child?(body) body && %i[module class].include?(body.type) end