Class: Ping::IssueReference
- Inherits:
-
Object
- Object
- Ping::IssueReference
- Defined in:
- lib/ping/issue_reference.rb
Constant Summary collapse
- REPOSITORY_NAME =
%r{[a-z0-9][a-z0-9\-]*/[a-z0-9][a-z0-9\-_]*}ix.freeze
Instance Attribute Summary collapse
-
#number ⇒ Object
Returns the value of attribute number.
-
#qualifier ⇒ Object
Returns the value of attribute qualifier.
-
#repository ⇒ Object
Returns the value of attribute repository.
Class Method Summary collapse
- .extract(text) ⇒ Object
- .qualifier_regex ⇒ Object
- .replace(text, &block) ⇒ Object
- .replace_match(match, ref, &_block) ⇒ Object
-
.short_pattern ⇒ Object
Match references of the form:.
-
.url_pattern ⇒ Object
Match references of the form:.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#initialize(qualifier, repository, number) ⇒ IssueReference
constructor
A new instance of IssueReference.
- #to_i ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(qualifier, repository, number) ⇒ IssueReference
Returns a new instance of IssueReference.
93 94 95 96 97 |
# File 'lib/ping/issue_reference.rb', line 93 def initialize(qualifier, repository, number) @qualifier = qualifier @repository = repository @number = number end |
Instance Attribute Details
#number ⇒ Object
Returns the value of attribute number.
5 6 7 |
# File 'lib/ping/issue_reference.rb', line 5 def number @number end |
#qualifier ⇒ Object
Returns the value of attribute qualifier.
5 6 7 |
# File 'lib/ping/issue_reference.rb', line 5 def qualifier @qualifier end |
#repository ⇒ Object
Returns the value of attribute repository.
5 6 7 |
# File 'lib/ping/issue_reference.rb', line 5 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 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_regex ⇒ Object
10 11 12 |
# File 'lib/ping/issue_reference.rb', line 10 def qualifier_regex /#{Ping.config.qualifiers.join('|')}/ix end |
.replace(text, &block) ⇒ Object
71 72 73 74 75 76 77 78 79 80 |
# 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, &_block) ⇒ Object
82 83 84 85 86 87 88 89 90 |
# 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 ⇒ Object
Match references of the form:
-
#123
-
codetree/feedback#123
-
GH-123
-
needs #123
-
etc…
23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# 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 ⇒ Object
Match references of the form:
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# 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 |
Instance Method Details
#==(other) ⇒ Object
99 100 101 |
# File 'lib/ping/issue_reference.rb', line 99 def ==(other) other.to_i == to_i end |
#to_i ⇒ Object
103 104 105 |
# File 'lib/ping/issue_reference.rb', line 103 def to_i number.to_i end |
#to_s ⇒ Object
107 108 109 |
# File 'lib/ping/issue_reference.rb', line 107 def to_s number.to_s end |