Class: Ping::IssueReference

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

Constant Summary collapse

REPOSITORY_NAME =
/[a-z0-9][a-z0-9\-]*\/[a-z0-9][a-z0-9\-_]*/ix

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(qualifier, repository, number) ⇒ IssueReference

Returns a new instance of IssueReference.



92
93
94
95
96
# File 'lib/ping/issue_reference.rb', line 92

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



59
60
61
62
63
64
65
66
67
# File 'lib/ping/issue_reference.rb', line 59

def extract(text)
  [short_pattern, url_pattern].inject([]) do |memo, pattern|
    memo.tap do |m|
      text.scan(pattern).each do |match|
        m << new(*match)
      end
    end
  end
end

.qualifier_regexObject



8
9
10
# File 'lib/ping/issue_reference.rb', line 8

def qualifier_regex
  /#{Ping.config.qualifiers.join('|')}/ix
end

.replace(text, &block) ⇒ Object



69
70
71
72
73
74
75
76
77
78
# File 'lib/ping/issue_reference.rb', line 69

def replace(text, &block)
  [short_pattern, url_pattern].each do |pattern|
    text = text.gsub(pattern) do |match|
      ref = new(*match.scan(pattern).first)
      replace_match(match, ref, &block)
    end
  end

  text
end

.replace_match(match, ref, &_block) ⇒ Object



80
81
82
83
84
85
86
87
88
89
# File 'lib/ping/issue_reference.rb', line 80

def replace_match(match, ref, &_block)
  replacement = yield(match, ref)
  return replacement unless replacement.is_a?(IssueReference)

  # Reformat the given issue reference replacement to match
  new_phrase = match[0] == ' ' ? ' ' : '' # fix leading space
  new_phrase << replacement.qualifier + ' ' if replacement.qualifier
  new_phrase << replacement.repository.to_s
  new_phrase << '#' + replacement.number.to_s
end

.short_patternObject

Match references of the form:

  • #123

  • codetree/feedback#123

  • GH-123

  • needs #123

  • etc…

See rubular.com/r/evB7RlvUfI



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ping/issue_reference.rb', line 21

def short_pattern
  /
    (?:^|\W)                         # beginning of string or non-word char
    (?:(#{qualifier_regex})(?:\s))?  # qualifier (optional)
    (?:(#{REPOSITORY_NAME})?         # repository name (optional)
    \#|(?:GH\-))(\d+)                # issue number
    (?=
      \.+[ \t]|                      # 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
end

.url_patternObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ping/issue_reference.rb', line 42

def url_pattern
  /
    (?:^|\W)                         # beginning of string or non-word char
    (?:(#{qualifier_regex})(?:\s))?  # qualifier (optional)
    https:\/\/github.com\/
    (#{REPOSITORY_NAME})             # repository name
    \/(?:issues|pulls)\/
    (\d+)                            # issue number
    (?=
      \.+[ \t]|                      # 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
end

Instance Method Details

#==(other) ⇒ Object



98
99
100
# File 'lib/ping/issue_reference.rb', line 98

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

#to_iObject



102
103
104
# File 'lib/ping/issue_reference.rb', line 102

def to_i
  number.to_i
end

#to_sObject



106
107
108
# File 'lib/ping/issue_reference.rb', line 106

def to_s
  number.to_s
end