Class: RBS::Annotate::Annotations::Copy

Inherits:
Object
  • Object
show all
Defined in:
sig/annotate/annotations.rbs,
lib/rbs/annotate/annotations.rb

Overview

Copy annotation allows copying the doc from another subject. This helps working with incorrect RDoc annotations.

%a{annotate:rdoc:copy:Bar#baz}
%a{annotate:rdoc:copy:Bar.baz}
%a{annotate:rdoc:copy:Bar::Baz}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(annotation:, source:) ⇒ Copy

Returns a new instance of Copy.

Parameters:



54
55
56
57
# File 'lib/rbs/annotate/annotations.rb', line 54

def initialize(annotation:, source:)
  @annotation = annotation
  @source = source
end

Instance Attribute Details

#annotationAST::Annotation (readonly)

Returns the value of attribute annotation.

Returns:



52
53
54
# File 'lib/rbs/annotate/annotations.rb', line 52

def annotation
  @annotation
end

#sourceString (readonly)

Returns the value of attribute source.

Returns:

  • (String)


52
53
54
# File 'lib/rbs/annotate/annotations.rb', line 52

def source
  @source
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

Parameters:

  • (Object)

Returns:

  • (Boolean)


84
85
86
87
88
# File 'lib/rbs/annotate/annotations.rb', line 84

def ==(other)
  other.is_a?(Copy) &&
    other.annotation == annotation &&
    other.source == source
end

#hashInteger

Returns:

  • (Integer)


80
81
82
# File 'lib/rbs/annotate/annotations.rb', line 80

def hash
  self.class.hash ^ annotation.hash ^ source.hash
end

#method_nameSymbol?

Returns:

  • (Symbol, nil)


64
65
66
67
68
69
# File 'lib/rbs/annotate/annotations.rb', line 64

def method_name
  _, m = partition
  if m
    m[1]
  end
end

#partition[TypeName, [bool, Symbol]?]

Returns a tuple of:

  • Type name
  • A pair of:
    • Boolean which holds if it is singleton
    • Name of method

Returns:



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'sig/annotate/annotations.rbs', line 86

def partition
  case
  when match = source.match(/(?<constant_name>[^#]+)#(?<method_name>.+)/)
    [
      TypeName.parse(match[:constant_name] || raise),
      [
        false,
        (match[:method_name] or raise).to_sym
      ]
    ]
  when match = source.match(/(?<constant_name>[^#]+)\.(?<method_name>.+)/)
    [
      TypeName.parse(match[:constant_name] || raise),
      [
        true,
        (match[:method_name] or raise).to_sym
      ]
    ]
  else
    [
      TypeName.parse(source),
      nil
    ]
  end
end

#singleton?Boolean

Returns:

  • (Boolean)


71
72
73
74
75
76
77
78
# File 'lib/rbs/annotate/annotations.rb', line 71

def singleton?
  _, m = partition
  if m
    m[0]
  else
    false
  end
end

#type_nameTypeName

Returns:



59
60
61
62
# File 'lib/rbs/annotate/annotations.rb', line 59

def type_name
  name, _ = partition
  name
end