class RenameGem::Renamer::StringReplacer

Constants

ChainError
NoMatchError

Attributes

content[R]
target[R]

Public Class Methods

new(content) click to toggle source
# File lib/rename_gem/renamer/string_replacer.rb, line 11
def initialize(content)
  @content = content
end

Public Instance Methods

replace(target) click to toggle source
# File lib/rename_gem/renamer/string_replacer.rb, line 15
def replace(target)
  @target = target

  self
end
with(replacement) click to toggle source
# File lib/rename_gem/renamer/string_replacer.rb, line 21
def with(replacement)
  raise ChainError, "Usage: replacer.replace('x').with('y')" if target.nil? || replacement.nil?
  raise NoMatchError, "#{target} not found in #{content}" unless exists?(target)

  content.gsub(target, replacement).gsub(pascal_case(target), pascal_case(replacement))
end

Private Instance Methods

exists?(target) click to toggle source
# File lib/rename_gem/renamer/string_replacer.rb, line 30
def exists?(target)
  content.include?(target) || content.include?(pascal_case(target))
end
pascal_case(snake_case) click to toggle source
# File lib/rename_gem/renamer/string_replacer.rb, line 34
def pascal_case(snake_case)
  snake_case.split('_').map(&:capitalize).join
end