Class: Issuesrc::Tag

Inherits:
Object
  • Object
show all
Defined in:
lib/issuesrc/tag.rb

Overview

A tag is an annotation found in the source code of a file that holds information about the issue it corresponds to, the author or assignee, a label, a title for the isssue, and its position in the file.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(label, issue_id, author, title, file, line, begin_pos, end_pos) ⇒ Tag

Returns a new instance of Tag.



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/issuesrc/tag.rb', line 16

def initialize(label, issue_id, author, title, file, line, 
               begin_pos, end_pos)
  @label = label
  @issue_id = issue_id.nil? || issue_id.empty? ? nil : issue_id
  @author = author.nil? || author.empty? ? nil : author
  @title = title
  @file = file
  @line = line
  @begin_pos = begin_pos
  @end_pos = end_pos
end

Instance Attribute Details

#authorObject (readonly)

Returns the value of attribute author.



9
10
11
# File 'lib/issuesrc/tag.rb', line 9

def author
  @author
end

#begin_posObject

Returns the value of attribute begin_pos.



13
14
15
# File 'lib/issuesrc/tag.rb', line 13

def begin_pos
  @begin_pos
end

#end_posObject

Returns the value of attribute end_pos.



14
15
16
# File 'lib/issuesrc/tag.rb', line 14

def end_pos
  @end_pos
end

#fileObject

Returns the value of attribute file.



11
12
13
# File 'lib/issuesrc/tag.rb', line 11

def file
  @file
end

#issue_idObject

Returns the value of attribute issue_id.



8
9
10
# File 'lib/issuesrc/tag.rb', line 8

def issue_id
  @issue_id
end

#labelObject (readonly)

Returns the value of attribute label.



7
8
9
# File 'lib/issuesrc/tag.rb', line 7

def label
  @label
end

#lineObject

Returns the value of attribute line.



12
13
14
# File 'lib/issuesrc/tag.rb', line 12

def line
  @line
end

#titleObject (readonly)

Returns the value of attribute title.



10
11
12
# File 'lib/issuesrc/tag.rb', line 10

def title
  @title
end

Instance Method Details

#to_sObject

 The string representation of the tag, to be included in the source file.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/issuesrc/tag.rb', line 29

def to_s
  ret = ""
  ret << @label
  if !@issue_id.nil? || !@author.nil?
    ret << '('
    if !@author.nil?
      ret << @author
    end
    if !@issue_id.nil?
      ret << '#' << @issue_id
    end
    ret << ')'
  end
  if !@title.nil?
    ret << ': ' << @title.strip
  end
  ret
end

#write_in_file(offsets) ⇒ Object

Writes the tag in its file, using its string representation.

Also updates the tag position information depending on offset

Parameters:

  • offsets

    As the tag’s position information might have been outdated by other tags having been written to the file, this function needs to know how much does it need to correct its position. offsets is a list of pairs (position, offset) that tells that at a given position a given offset has been added or substracted.

Returns:

  • offsets, updated with the new offset resulting from editing the file.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/issuesrc/tag.rb', line 59

def write_in_file(offsets)
  total_offset = 0
  offsets.each do |pos, offset|
    if pos <= @begin_pos
      total_offset += offset
    end
  end
  old_begin_pos = @begin_pos
  @begin_pos += total_offset
  @end_pos += total_offset
  file.replace_at(@begin_pos, @end_pos-@begin_pos, to_s())
  new_end_pos = @begin_pos + to_s.length
  offset = new_end_pos - @end_pos
  @end_pos = new_end_pos
  offsets << [old_begin_pos, offset]
  offsets
end