Class: Rucoa::Definitions::MethodDefinition

Inherits:
Base
  • Object
show all
Defined in:
lib/rucoa/definitions/method_definition.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#description, #location

Instance Method Summary collapse

Constructor Details

#initialize(kind:, method_name:, namespace:, types:, **keyword_arguments) ⇒ MethodDefinition

Returns a new instance of MethodDefinition.

Parameters:



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rucoa/definitions/method_definition.rb', line 19

def initialize(
  kind:,
  method_name:,
  namespace:,
  types:,
  **keyword_arguments
)
  super(**keyword_arguments)
  @kind = kind
  @method_name = method_name
  @namespace = namespace
  @types = types
end

Instance Attribute Details

#kindSymbol (readonly)

Returns:

  • (Symbol)


7
8
9
# File 'lib/rucoa/definitions/method_definition.rb', line 7

def kind
  @kind
end

#method_nameString (readonly)

Returns:

  • (String)


10
11
12
# File 'lib/rucoa/definitions/method_definition.rb', line 10

def method_name
  @method_name
end

#namespaceString (readonly)

Returns:

  • (String)


13
14
15
# File 'lib/rucoa/definitions/method_definition.rb', line 13

def namespace
  @namespace
end

Instance Method Details

#instance_method?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/rucoa/definitions/method_definition.rb', line 34

def instance_method?
  @kind == :instance
end

#parametersArray<Rucoa::Definitions::MethodParameter>

TODO:

Returns:

  • (Array<Rucoa::Definitions::MethodParameter>)


40
41
42
# File 'lib/rucoa/definitions/method_definition.rb', line 40

def parameters
  []
end

#qualified_nameString

Examples:

returns qualified name of method

definition = Rucoa::Source.new(
  content: <<~RUBY,
    module Foo
      module Bar
        def foo
        end
      end
    end
  RUBY
  uri: 'file:///path/to/foo/bar/baz.rb',
).definitions[2]
expect(definition.qualified_name).to eq('Foo::Bar#foo')

Returns:

  • (String)


58
59
60
61
62
63
64
# File 'lib/rucoa/definitions/method_definition.rb', line 58

def qualified_name
  [
    @namespace,
    method_kind_symbol,
    @method_name
  ].join
end

#return_typesArray<String>

Examples:

returns return types

definition = Rucoa::Source.new(
  content: <<~RUBY,
    module Foo
      module Bar
        # @return [String]
        def baz
        end
      end
    end
  RUBY
  uri: 'file:///path/to/foo/bar.rb',
).definitions[2]
expect(definition.return_types).to eq(%w[String])

Returns:

  • (Array<String>)


81
82
83
# File 'lib/rucoa/definitions/method_definition.rb', line 81

def return_types
  @types.map(&:return_type)
end

#signaturesArray<String>

Examples:

returns signature

definition = Rucoa::Source.new(
  content: <<~RUBY,
    module Foo
      module Bar
        attr_writer :baz
      end
    end
  RUBY
  uri: 'file:///path/to/foo/bar.rb',
).definitions[2]
expect(definition.signatures).to eq(
  [
    'Foo::Bar#baz=(value) -> Object'
  ]
)

Returns:

  • (Array<String>)


102
103
104
105
106
107
108
109
110
111
# File 'lib/rucoa/definitions/method_definition.rb', line 102

def signatures
  @types.map do |type|
    format(
      '%<qualified_name>s(%<parameters>s) -> %<return_types>s',
      parameters: type.parameters_string,
      qualified_name: qualified_name,
      return_types: type.return_type
    )
  end
end

#singleton_method?Boolean

Returns:

  • (Boolean)


114
115
116
# File 'lib/rucoa/definitions/method_definition.rb', line 114

def singleton_method?
  !instance_method?
end