Class: Rucoa::Yard::DefinitionGenerators::AttributeReaderDefinitionGenerator

Inherits:
Base
  • Object
show all
Defined in:
lib/rucoa/yard/definition_generators/attribute_reader_definition_generator.rb

Constant Summary collapse

READER_METHOD_NAMES =
::Set[
  'attr_accessor',
  'attr_reader'
].freeze

Instance Method Summary collapse

Methods inherited from Base

call, #initialize

Constructor Details

This class inherits a constructor from Rucoa::Yard::DefinitionGenerators::Base

Instance Method Details

#callObject

Examples:

returns method definition for attr_reader node

definitions = Rucoa::Source.new(
  content: <<~RUBY,
    class Foo
      attr_reader :bar
    end
  RUBY
  uri: '/path/to/foo.rb'
).definitions
expect(definitions[1]).to be_a(Rucoa::Definitions::MethodDefinition)

returns method definition for attr_accessor node

definitions = Rucoa::Source.new(
  content: <<~RUBY,
    class Foo
      attr_accessor :bar
    end
  RUBY
  uri: '/path/to/foo.rb'
).definitions
expect(definitions.map(&:qualified_name)).to eq(
  %w[
    Foo
    Foo#bar
    Foo#bar=
  ]
)

ignores unrecognizable attributes

definitions = Rucoa::Source.new(
  content: <<~RUBY,
    class Foo
      attr_reader foo
    end
  RUBY
  uri: '/path/to/foo.rb'
).definitions
expect(definitions.map(&:qualified_name)).to eq(
  %w[
    Foo
  ]
)


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rucoa/yard/definition_generators/attribute_reader_definition_generator.rb', line 52

def call
  return [] unless @node.is_a?(Nodes::SendNode) && READER_METHOD_NAMES.include?(@node.name)

  @node.arguments.filter_map do |argument|
    next unless argument.respond_to?(:value)

    Definitions::MethodDefinition.new(
      description: description,
      kind: :instance,
      location: location,
      method_name: argument.value.to_s,
      namespace: @node.namespace,
      types: return_types.map do |type|
        Types::MethodType.new(
          parameters_string: '', # TODO
          return_type: type
        )
      end
    )
  end
end