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
REPOSITORY_NAME =
/[a-z0-9][a-z0-9\-]*\/[a-z0-9][a-z0-9\-_]*/ix
SHORT_PATTERN =

Match references of the form:

  • #123

  • codetree/feedback#123

  • GH-123

  • needs #123

  • etc…

See rubular.com/r/evB7RlvUfI

/
  (?:^|\W)                    # beginning of string or non-word char
  (?:(#{QUALIFIERS})(?:\s))?  # qualifier (optional)
  (?:(#{REPOSITORY_NAME})?    # 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
URL_PATTERN =
/
  (?:^|\W)                    # beginning of string or non-word char
  (?:(#{QUALIFIERS})(?:\s))?  # qualifier (optional)
  https:\/\/github.com\/
  (#{REPOSITORY_NAME})        # repository name
  \/(?:issues|pulls)\/
  (\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

Returns a new instance of IssueReference.



55
56
57
58
59
# File 'lib/ping/issue_reference.rb', line 55

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



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

def self.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

.replace(text, &block) ⇒ Object



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

def self.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



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

def self.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

Instance Method Details

#==(other) ⇒ Object



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

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

#to_iObject



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

def to_i
  number.to_i
end

#to_sObject



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

def to_s
  number.to_s
end