Class: Ping::IssueReference

Inherits:
Object
  • Object
show all
Defined in:
lib/ping/issue_reference.rb

Constant Summary collapse

Qualifiers =
/
  close|closes|closed|fix|fixes|fixed|resolve|resolves|resolved|
  need|needs|needed|require|requires|required
/ix
RepositoryName =
/[a-z0-9][a-z0-9\-]*\/[a-z0-9][a-z0-9\-_]*/ix
Pattern =
/
  (?:^|\W)                    # beginning of string or non-word char
  (?:(#{Qualifiers})(?:\s))?  # qualifier (optional)
  (?:(#{RepositoryName})?     # repository name (optional)
  \#|(?:GH\-))(\d+)           # issue number
  (?=
    \.+[ \t\W]|               # dots followed by space or non-word character
    \.+$|                     # dots at end of line
    [^0-9a-zA-Z_.]|           # non-word character except dot
    $                         # end of line
  )
/ix

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(qualifier, repository, number) ⇒ IssueReference



27
28
29
30
31
# File 'lib/ping/issue_reference.rb', line 27

def initialize(qualifier, repository, number)
  @qualifier = qualifier
  @repository = repository
  @number = number
end

Instance Attribute Details

#numberObject

Returns the value of attribute number.



3
4
5
# File 'lib/ping/issue_reference.rb', line 3

def number
  @number
end

#qualifierObject

Returns the value of attribute qualifier.



3
4
5
# File 'lib/ping/issue_reference.rb', line 3

def qualifier
  @qualifier
end

#repositoryObject

Returns the value of attribute repository.



3
4
5
# File 'lib/ping/issue_reference.rb', line 3

def repository
  @repository
end

Class Method Details

.extract(text) ⇒ Object



33
34
35
36
37
# File 'lib/ping/issue_reference.rb', line 33

def self.extract(text)
  text.scan(Pattern).map do |match|
    self.new(*match)
  end
end

.replace(text, &block) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ping/issue_reference.rb', line 39

def self.replace(text, &block)
  text.gsub(Pattern) do |phrase|
    replacement = yield(phrase, self.new(*phrase.scan(Pattern).first))

    if replacement.is_a?(IssueReference)
      new_phrase = phrase[0] == " " ? " " : "" # fix leading space
      new_phrase << replacement.qualifier + " " if replacement.qualifier
      new_phrase << replacement.repository.to_s
      new_phrase << "#" + replacement.number.to_s
    else
      replacement
    end
  end
end

Instance Method Details

#==(other) ⇒ Object



54
55
56
# File 'lib/ping/issue_reference.rb', line 54

def ==(other)
  other.to_i == to_i
end

#to_iObject



58
59
60
# File 'lib/ping/issue_reference.rb', line 58

def to_i
  number.to_i
end

#to_sObject



62
63
64
# File 'lib/ping/issue_reference.rb', line 62

def to_s
  number.to_s
end