evrone/evrone_opensource

View on GitHub
app/services/pipeline_service/improvement/readme_gem_improvement_1.rb

Summary

Maintainability
A
20 mins
Test Coverage
A
100%
# frozen_string_literal: true
 
module PipelineService
module Improvement
# Highlight gem syntax in README.md
module ReadmeGemImprovement1
module_function
 
PATTERN = /(?<line>\n\n\s\s\s\s(?<gem>gem\s["'].+["'])\n\n)/.freeze
 
# Replaces PATTERN with highlighted gem definition,
# "
#
# gem '<gem_name-here>'
#
# "
#
# becomes
#
# "
#
# ```ruby
# gem '<gem_name-here>'
# ```
#
# "
Identical blocks of code found in 2 locations. Consider refactoring.
def call(directory)
file = "#{directory}/README.md"
content = File.read(file)
matched = content.match(PATTERN)
 
return unless matched
 
highlight_gem_syntax \
file, content, matched[:line], matched[:gem]
end
 
def highlight_gem_syntax(filepath, content, line, gem)
improvement = <<-STR.strip_heredoc
```ruby
#{gem}
```
STR
 
improvement = "\n\n#{improvement}\n"
 
File.open(filepath, 'wb') do |f|
content.sub!(line, improvement)
f.write(content)
end
end
end
end
end