Class: Rucoa::Yard::DefinitionGenerators::AttributeWriterDefinitionGenerator

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

Constant Summary collapse

WRITER_METHOD_NAMES =
::Set[
  'attr_accessor',
  'attr_writer'
].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_writer node

definitions = Rucoa::Source.new(
  content: <<~RUBY,
    class Foo
      attr_writer :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_writer 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_writer_definition_generator.rb', line 52

def call
  return [] unless @node.is_a?(Nodes::SendNode) && WRITER_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}=",
      namespace: @node.namespace,
      types: return_types.map do |type|
        Types::MethodType.new(
          parameters_string: 'value', # TODO
          return_type: type
        )
      end
    )
  end
end