Class: Rubyzen::Declarations::FileDeclaration

Inherits:
Object
  • Object
show all
Includes:
Providers::BlocksProvider, Providers::CallSiteProvider, Providers::ConstantsProvider, Providers::FilePathProvider, Providers::LineNumberProvider, Providers::LinesOfCodeProvider, Providers::RequiresProvider
Defined in:
lib/rubyzen/declarations/file_declaration.rb

Overview

Represents a parsed Ruby source file. This is the root of the declaration hierarchy — all other declarations are accessed through a FileDeclaration.

Examples:

project = Rubyzen::Project.new("/app/src")
file = project.files.first
file.name      #=> "user.rb"
file.classes   #=> [ClassDeclaration, ...]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Providers::BlocksProvider

#blocks

Methods included from Providers::CallSiteProvider

#call_sites

Methods included from Providers::RequiresProvider

#requires

Methods included from Providers::ConstantsProvider

#constants

Methods included from Providers::LinesOfCodeProvider

#lines_of_code

Methods included from Providers::LineNumberProvider

#line

Methods included from Providers::FilePathProvider

#file_path

Constructor Details

#initialize(path, ast) ⇒ FileDeclaration

Returns a new instance of FileDeclaration.

Parameters:

  • path (String)

    absolute file path

  • ast (RuboCop::AST::Node)

    parsed AST root node



30
31
32
33
# File 'lib/rubyzen/declarations/file_declaration.rb', line 30

def initialize(path, ast)
  @path = path
  @node = ast
end

Instance Attribute Details

#nodeRuboCop::AST::Node (readonly) Also known as: ast

Returns the root AST node.

Returns:

  • (RuboCop::AST::Node)

    the root AST node



25
26
27
# File 'lib/rubyzen/declarations/file_declaration.rb', line 25

def node
  @node
end

#pathString (readonly)

Returns absolute path to the source file.

Returns:

  • (String)

    absolute path to the source file



22
23
24
# File 'lib/rubyzen/declarations/file_declaration.rb', line 22

def path
  @path
end

Instance Method Details

#classesArray<ClassDeclaration>

Returns all classes defined in this file.

Returns:



45
46
47
48
49
# File 'lib/rubyzen/declarations/file_declaration.rb', line 45

def classes
  node.each_node(:class).map do |class_node|
    ClassDeclaration.new(class_node, self)
  end
end

#modulesArray<ModuleDeclaration>

Returns all modules defined in this file.

Returns:



62
63
64
65
66
# File 'lib/rubyzen/declarations/file_declaration.rb', line 62

def modules
  node.each_node(:module).map do |module_node|
    Rubyzen::Declarations::ModuleDeclaration.new(module_node, self)
  end
end

#nameString

Returns the basename of the file.

Returns:

  • (String)

    e.g. “user.rb”



38
39
40
# File 'lib/rubyzen/declarations/file_declaration.rb', line 38

def name
  File.basename(path)
end

#top_level_module_nameString?

Returns the name of the first module in the file, used to determine the top-level namespace.

Returns:

  • (String, nil)


55
56
57
# File 'lib/rubyzen/declarations/file_declaration.rb', line 55

def top_level_module_name
  modules.first&.name_without_modules
end