Class: Semverve::VersionLiteralRewriter
- Inherits:
-
Object
- Object
- Semverve::VersionLiteralRewriter
- Defined in:
- lib/semverve/version_literal_rewriter.rb
Overview
Rewrites a named capture inside the first match for a safe version literal.
Instance Method Summary collapse
-
#initialize(pattern:, replacement:, capture: :version) ⇒ Semverve::VersionLiteralRewriter
constructor
Initializes a literal rewriter.
-
#rewrite(text) ⇒ String
Rewrites the configured capture in
text.
Constructor Details
#initialize(pattern:, replacement:, capture: :version) ⇒ Semverve::VersionLiteralRewriter
Initializes a literal rewriter.
17 18 19 20 21 22 23 24 |
# 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 |
Instance Method Details
#rewrite(text) ⇒ String
Rewrites the configured capture in text.
32 33 34 35 36 37 38 39 40 |
# 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 |