Class: Rubyzen::Declarations::MethodDeclaration

Overview

Represents a Ruby method definition (def or def self.).

Examples:

method = klass.instance_methods.first
method.name          #=> "calculate"
method.parameters?   #=> true
method.call_sites    #=> CallSiteCollection
method.visibility    #=> :private

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Providers::AssignmentsProvider

#assignments

Methods included from Providers::ReturnsProvider

#return_expressions, #returns

Methods included from Providers::RaisesProvider

#raises

Methods included from Providers::RescuesProvider

#rescues

Methods included from Providers::VisibilityProvider

#private?, #protected?, #public?, #visibility

Methods included from Providers::LinesOfCodeProvider

#lines_of_code

Methods included from Providers::CallSiteProvider

#call_sites

Methods included from Providers::ConstantsProvider

#constants

Methods included from Providers::LineNumberProvider

#line

Methods included from Providers::ClassNameProvider

#class_name

Methods included from Providers::FilePathProvider

#file_path

Methods included from Providers::BlocksProvider

#blocks

Methods included from Providers::IfStatementsProvider

#if_statements

Constructor Details

#initialize(node, parent_class) ⇒ MethodDeclaration

Returns a new instance of MethodDeclaration.



34
35
36
37
# File 'lib/rubyzen/declarations/method_declaration.rb', line 34

def initialize(node, parent_class)
  @node = node
  @parent_class = parent_class
end

Instance Attribute Details

#nodeRuboCop::AST::Node (readonly)

Returns:

  • (RuboCop::AST::Node)


28
29
30
# File 'lib/rubyzen/declarations/method_declaration.rb', line 28

def node
  @node
end

#parent_classClassDeclaration, ModuleDeclaration (readonly) Also known as: parent



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

def parent_class
  @parent_class
end

Instance Method Details

#nameString

Returns the method name.

Returns:

  • (String)


42
43
44
# File 'lib/rubyzen/declarations/method_declaration.rb', line 42

def name
  node.method_name.to_s
end

#parametersCollections::ParametersCollection

Returns the method’s parameters.



49
50
51
52
53
54
55
# File 'lib/rubyzen/declarations/method_declaration.rb', line 49

def parameters
  Collections::ParametersCollection.new(
    node.arguments.map do |arg|
      ParameterDeclaration.new(arg, self)
    end
  )
end

#parameters?Boolean

Returns whether this method has any parameters.

Returns:

  • (Boolean)


60
61
62
# File 'lib/rubyzen/declarations/method_declaration.rb', line 60

def parameters?
  node.arguments.any?
end