Class: Rubyzen::Declarations::ClassDeclaration

Overview

Represents a Ruby class definition. Provides access to methods, attributes, macros, and other class-level constructs.

Examples:

klass = file.classes.first
klass.name                #=> "Admin::UsersController"
klass.superclass_name     #=> "ApplicationController"
klass.instance_methods    #=> MethodsCollection

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Providers::RaisesProvider

#raises

Methods included from Providers::RescuesProvider

#rescues

Methods included from Providers::MacrosProvider

#macros

Methods included from Providers::AttributesProvider

#attributes

Methods included from Providers::ConstantsProvider

#constants

Methods included from Providers::ClassNameProvider

#class_name

Methods included from Providers::LinesOfCodeProvider

#lines_of_code

Methods included from Providers::LineNumberProvider

#line

Methods included from Providers::FilePathProvider

#file_path

Methods included from Providers::BlocksProvider

#blocks

Methods included from Providers::IfStatementsProvider

#if_statements

Constructor Details

#initialize(node, file_declaration) ⇒ ClassDeclaration

Returns a new instance of ClassDeclaration.

Parameters:



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_declarationFileDeclaration (readonly)

Returns the file this class belongs to.

Returns:



29
30
31
# File 'lib/rubyzen/declarations/class_declaration.rb', line 29

def file_declaration
  @file_declaration
end

#nodeRuboCop::AST::Node (readonly)

Returns the class AST node.

Returns:

  • (RuboCop::AST::Node)

    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_namesArray<String>

Returns unique method names called anywhere in this class.

Returns:

  • (Array<String>)


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_methodsCollections::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_methodsCollections::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

#nameString

Returns the fully-qualified class name including parent modules.

Returns:

  • (String)

    e.g. “Admin::UsersController”



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_modulesString

Returns the class name without module prefixes.

Returns:

  • (String)

    e.g. “UsersController”



58
59
60
# File 'lib/rubyzen/declarations/class_declaration.rb', line 58

def name_without_modules
  node.identifier&.const_name
end

#superclass_nameString?

Returns the superclass name, if any.

Returns:

  • (String, nil)

    e.g. “ApplicationController”



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.

Parameters:

  • prefix (String)

Returns:

  • (Boolean)


76
77
78
# File 'lib/rubyzen/declarations/class_declaration.rb', line 76

def superclass_prefix?(prefix)
  superclass_name&.start_with?(prefix)
end

#top_level_moduleString?

Returns the top-level module name from the enclosing file.

Returns:

  • (String, nil)


112
113
114
# File 'lib/rubyzen/declarations/class_declaration.rb', line 112

def top_level_module
  file_declaration.top_level_module_name
end