Class: Rubyzen::Declarations::ClassDeclaration
- Inherits:
-
Object
- Object
- Rubyzen::Declarations::ClassDeclaration
- Includes:
- Providers::AttributesProvider, Providers::BlocksProvider, Providers::ClassNameProvider, Providers::ConstantsProvider, Providers::FilePathProvider, Providers::IfStatementsProvider, Providers::LineNumberProvider, Providers::LinesOfCodeProvider, Providers::MacrosProvider, Providers::RaisesProvider, Providers::RescuesProvider
- Defined in:
- lib/rubyzen/declarations/class_declaration.rb
Overview
Represents a Ruby class definition. Provides access to methods, attributes, macros, and other class-level constructs.
Instance Attribute Summary collapse
-
#file_declaration ⇒ FileDeclaration
readonly
The file this class belongs to.
-
#node ⇒ RuboCop::AST::Node
readonly
The class AST node.
Instance Method Summary collapse
-
#called_method_names ⇒ Array<String>
Returns unique method names called anywhere in this class.
-
#class_methods ⇒ Collections::MethodsCollection
Returns class methods (both
self.methodand class << self styles). -
#initialize(node, file_declaration) ⇒ ClassDeclaration
constructor
A new instance of ClassDeclaration.
-
#instance_methods ⇒ Collections::MethodsCollection
Returns instance methods defined directly in this class.
-
#name ⇒ String
Returns the fully-qualified class name including parent modules.
-
#name_without_modules ⇒ String
Returns the class name without module prefixes.
-
#superclass_name ⇒ String?
Returns the superclass name, if any.
-
#superclass_prefix?(prefix) ⇒ Boolean
Checks whether the superclass name starts with the given prefix.
-
#top_level_module ⇒ String?
Returns the top-level module name from the enclosing file.
Methods included from Providers::RaisesProvider
Methods included from Providers::RescuesProvider
Methods included from Providers::MacrosProvider
Methods included from Providers::AttributesProvider
Methods included from Providers::ConstantsProvider
Methods included from Providers::ClassNameProvider
Methods included from Providers::LinesOfCodeProvider
Methods included from Providers::LineNumberProvider
Methods included from Providers::FilePathProvider
Methods included from Providers::BlocksProvider
Methods included from Providers::IfStatementsProvider
Constructor Details
#initialize(node, file_declaration) ⇒ ClassDeclaration
Returns a new instance of ClassDeclaration.
33 34 35 36 |
# File 'lib/rubyzen/declarations/class_declaration.rb', line 33 def initialize(node, file_declaration) @node = node @file_declaration = file_declaration end |
Instance Attribute Details
#file_declaration ⇒ FileDeclaration (readonly)
Returns the file this class belongs to.
29 30 31 |
# File 'lib/rubyzen/declarations/class_declaration.rb', line 29 def file_declaration @file_declaration end |
#node ⇒ RuboCop::AST::Node (readonly)
Returns the class AST node.
26 27 28 |
# File 'lib/rubyzen/declarations/class_declaration.rb', line 26 def node @node end |
Instance Method Details
#called_method_names ⇒ Array<String>
Returns unique method names called anywhere in this class.
105 106 107 |
# File 'lib/rubyzen/declarations/class_declaration.rb', line 105 def called_method_names node.each_descendant(:send).map { |send_node| send_node.method_name.to_s }.uniq end |
#class_methods ⇒ Collections::MethodsCollection
Returns class methods (both self.method and class << self styles).
94 95 96 97 98 99 100 |
# File 'lib/rubyzen/declarations/class_declaration.rb', line 94 def class_methods Collections::MethodsCollection.new( class_method_nodes.map do |method_node| MethodDeclaration.new(method_node, self) end ) end |
#instance_methods ⇒ Collections::MethodsCollection
Returns instance methods defined directly in this class.
83 84 85 86 87 88 89 |
# File 'lib/rubyzen/declarations/class_declaration.rb', line 83 def instance_methods Collections::MethodsCollection.new( instance_method_nodes.map do |def_node| MethodDeclaration.new(def_node, self) end ) end |
#name ⇒ String
Returns the fully-qualified class name including parent modules.
41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/rubyzen/declarations/class_declaration.rb', line 41 def name parent_module_names = [] current_node = node.parent while current_node if current_node.type == :module parent_module_names.unshift(current_node.identifier&.const_name) end current_node = current_node.parent end [parent_module_names, name_without_modules].flatten.compact.join('::') end |
#name_without_modules ⇒ String
Returns the class name without module prefixes.
58 59 60 |
# File 'lib/rubyzen/declarations/class_declaration.rb', line 58 def name_without_modules node.identifier&.const_name end |
#superclass_name ⇒ String?
Returns the superclass name, if any.
65 66 67 68 69 70 |
# File 'lib/rubyzen/declarations/class_declaration.rb', line 65 def superclass_name super_node = node.children[1] return nil unless super_node&.type == :const super_node.const_name end |
#superclass_prefix?(prefix) ⇒ Boolean
Checks whether the superclass name starts with the given prefix.
76 77 78 |
# File 'lib/rubyzen/declarations/class_declaration.rb', line 76 def superclass_prefix?(prefix) superclass_name&.start_with?(prefix) end |
#top_level_module ⇒ String?
Returns the top-level module name from the enclosing file.
112 113 114 |
# File 'lib/rubyzen/declarations/class_declaration.rb', line 112 def top_level_module file_declaration.top_level_module_name end |