Module: Solargraph::YardMap::Directives::AttributeDirective

Defined in:
lib/solargraph/yard_map/directives/attribute_directive.rb

Class Method Summary collapse

Class Method Details

.closure_at(pins, position) ⇒ Pin::Closure

Parameters:

Returns:



59
60
61
# File 'lib/solargraph/yard_map/directives/attribute_directive.rb', line 59

def closure_at pins, position
  pins.select { |pin| pin.is_a?(Pin::Closure) and pin.location&.range&.contain?(position) }.last
end

.process_directive(source, pins, source_position, comment_position, directive) ⇒ Array<Solargraph::Pin::Method>

Parameters:

Returns:



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/solargraph/yard_map/directives/attribute_directive.rb', line 15

def process_directive source, pins, source_position, comment_position, directive
  new_pins = []
  location = Location.new(source.filename, Range.new(comment_position, comment_position))
  docstring = Solargraph::Source.parse_docstring(directive.tag.text.to_s).to_docstring
  return [] if directive.tag.name.nil?
  namespace = closure_at(pins, source_position)
  t = directive.tag.types.nil? || directive.tag.types.empty? ? nil : directive.tag.types.join
  if t.nil? || t.include?('r')
    new_pins.push Solargraph::Pin::Method.new(
      location: location,
      closure: namespace,
      name: directive.tag.name,
      comments: docstring.all.to_s,
      scope: namespace.is_a?(Pin::Singleton) ? :class : :instance,
      visibility: :public,
      explicit: false,
      attribute: true,
      source: :yard_map
    )
  end
  if t.nil? || t.include?('w')
    write_pin = Solargraph::Pin::Method.new(
      location: location,
      closure: namespace,
      name: "#{directive.tag.name}=",
      comments: docstring.all.to_s,
      scope: namespace.is_a?(Pin::Singleton) ? :class : :instance,
      visibility: :public,
      attribute: true,
      source: :yard_map
    )
    new_pins.push(write_pin)
    write_pin.parameters.push Pin::Parameter.new(name: 'value', decl: :arg, closure: write_pin, source: :yard_map)
    if write_pin.return_type&.defined?
      write_pin.docstring.add_tag YARD::Tags::Tag.new(:param, '', write_pin.return_type.to_s.split(', '), 'value')
    end
  end

  new_pins.compact
end