class Ping::IssueReference

Constants

REPOSITORY_NAME

Attributes

number[RW]
qualifier[RW]
repository[RW]

Public Class Methods

extract(text) click to toggle source
# File lib/ping/issue_reference.rb, line 61
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
new(qualifier, repository, number) click to toggle source
# File lib/ping/issue_reference.rb, line 93
def initialize(qualifier, repository, number)
  @qualifier = qualifier
  @repository = repository
  @number = number
end
qualifier_regex() click to toggle source
# File lib/ping/issue_reference.rb, line 10
def qualifier_regex
  /#{Ping.config.qualifiers.join('|')}/ix
end
replace(text, &block) click to toggle source
# File lib/ping/issue_reference.rb, line 71
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) { |match, ref| ... } click to toggle source
# File lib/ping/issue_reference.rb, line 82
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
  lead_in = match[0] == ' ' ? ' ' : '' # fix leading space
  lead_in += replacement.qualifier + ' ' if replacement.qualifier
  lead_in + replacement.repository.to_s + '#' + replacement.number.to_s
end
short_pattern() click to toggle source

Match references of the form:

  • #123

  • codetree/feedback#123

  • GH-123

  • needs #123

  • etc…

See rubular.com/r/evB7RlvUfI

# File lib/ping/issue_reference.rb, line 23
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_pattern() click to toggle source

Match references of the form:

# File lib/ping/issue_reference.rb, line 44
def url_pattern
  %r{
    (?:^|\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

Public Instance Methods

==(other) click to toggle source
# File lib/ping/issue_reference.rb, line 99
def ==(other)
  other.to_i == to_i
end
to_i() click to toggle source
# File lib/ping/issue_reference.rb, line 103
def to_i
  number.to_i
end
to_s() click to toggle source
# File lib/ping/issue_reference.rb, line 107
def to_s
  number.to_s
end