class Semverve::VersionLiteralRewriter
Rewrites a named capture inside the first match for a safe version literal.
Public Class Methods
# File lib/semverve/version_literal_rewriter.rb, line 17 def initialize(pattern:, replacement:, capture: :version) @pattern = pattern @replacement = replacement.to_s @capture_name = capture.to_s @capture_key = capture.to_sym validate_capture end
Initializes a literal rewriter.
@param [Regexp] pattern @param [String, #to_s] replacement @param [Symbol, String] capture
@return [Semverve::VersionLiteralRewriter]
Public Instance Methods
Source
# File lib/semverve/version_literal_rewriter.rb, line 32 def rewrite(text) text.sub(pattern) do |matched_text| match = Regexp.last_match capture_start = match.begin(capture_key) - match.begin(0) capture_end = match.end(capture_key) - match.begin(0) "#{matched_text[0...capture_start]}#{replacement}#{matched_text[capture_end..]}" end end
Rewrites the configured capture in text.
@param [String] text
@return [String]