Class: RBS::Annotate::Annotations

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

Defined Under Namespace

Classes: Copy, Skip, Source

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(items) ⇒ Annotations

Returns a new instance of Annotations.



146
147
148
# File 'lib/rbs/annotate/annotations.rb', line 146

def initialize(items)
  @items = items
end

Instance Attribute Details

#itemsObject (readonly)

Returns the value of attribute items.



144
145
146
# File 'lib/rbs/annotate/annotations.rb', line 144

def items
  @items
end

Class Method Details

.parse(annotation) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/rbs/annotate/annotations.rb', line 117

def self.parse(annotation)
  string = annotation.string

  case
  when match = string.match(/\Aannotate:rdoc:skip(:all)?\Z/)
    Skip.new(
      annotation: annotation,
      skip_children: string.end_with?(":all")
    )
  when match = string.match(/\Aannotate:rdoc:source:from=(?<path>.+)\Z/)
    Source.new(
      annotation: annotation,
      include: (match[:path] or raise).strip
    )
  when match = string.match(/\Aannotate:rdoc:source:skip=(?<path>.+)\Z/)
    Source.new(
      annotation: annotation,
      skip: (match[:path] or raise).strip
    )
  when match = string.match(/\Aannotate:rdoc:copy:(?<name>.+)\Z/)
    Copy.new(
      annotation: annotation,
      source: (match[:name] or raise).strip
    )
  end
end

Instance Method Details

#copy_annotationObject



158
159
160
# File 'lib/rbs/annotate/annotations.rb', line 158

def copy_annotation
  _ = items.find {|a| a.is_a?(Copy) }
end

#skip?Boolean

Returns:

  • (Boolean)


150
151
152
# File 'lib/rbs/annotate/annotations.rb', line 150

def skip?
  items.any? {|a| a.is_a?(Skip) }
end

#skip_all?Boolean

Returns:

  • (Boolean)


154
155
156
# File 'lib/rbs/annotate/annotations.rb', line 154

def skip_all?
  items.any? {|a| a.is_a?(Skip) && a.skip_children }
end

#test_path(path) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/rbs/annotate/annotations.rb', line 162

def test_path(path)
  # @type var source_items: Array[Source]
  source_items = _ = items.select {|item| item.is_a?(Source) }

  return true if source_items.empty?

  result = source_items[0].include_source == nil

  items.each do |a|
    if a.is_a?(Source)
      if pat = a.include_source
        if test_path_string(pat, path)
          result = true
        end
      end

      if pat = a.skip_source
        if test_path_string(pat, path)
          result = false
        end
      end
    end
  end

  result
end

#test_path_string(pattern, string) ⇒ Object



189
190
191
192
193
194
# File 'lib/rbs/annotate/annotations.rb', line 189

def test_path_string(pattern, string)
  return true if pattern == string
  return true if string.start_with?(pattern + File::SEPARATOR)

  false
end