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



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

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

Instance Attribute Details

#nodeRuboCop::AST::Node (readonly)

Returns:

  • (RuboCop::AST::Node)


26
27
28
# File 'lib/rubyzen/declarations/method_declaration.rb', line 26

def node
  @node
end

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



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

def parent_class
  @parent_class
end

Instance Method Details

#nameString

Returns the method name.

Returns:

  • (String)


40
41
42
# File 'lib/rubyzen/declarations/method_declaration.rb', line 40

def name
  node.method_name.to_s
end

#parametersCollections::ParametersCollection

Returns the method’s parameters.



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

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)


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

def parameters?
  node.arguments.any?
end