Sha256: 3ad60637a07b23280662be0aaadd7d70ca69b0a876b463e859d259c8ed7d31bc

Contents?: true

Size: 1.13 KB

Versions: 4

Compression:

Stored size: 1.13 KB

Contents

class Pups::ReplaceCommand < Pups::Command
  attr_accessor :text, :from, :to, :filename, :direction, :global

  def self.from_hash(hash, params)
    replacer = new(params)
    replacer.from = guess_replace_type(hash["from"])
    replacer.to = guess_replace_type(hash["to"])
    replacer.text = File.read(hash["filename"])
    replacer.filename = hash["filename"]
    replacer.direction = hash["direction"].to_sym if hash["direction"]
    replacer.global = hash["global"].to_s == "true"
    replacer
  end

  def self.guess_replace_type(item)
    # evaling to get all the regex flags easily
    item[0] == "/" ? eval(item) : item
  end

  def initialize(params)
    @params = params
  end

  def replaced_text
    new_to = to
    if String === to
      new_to = interpolate_params(to)
    end
    if global
      text.gsub(from,new_to)
    elsif direction == :reverse
      index = text.rindex(from)
      text[0..index-1] << text[index..-1].sub(from,new_to)
    else
      text.sub(from,new_to)
    end
  end

  def run
    Pups.log.info("Replacing #{from.to_s} with #{to.to_s} in #{filename}")
    File.open(filename, "w"){|f| f.write replaced_text }
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
pups-1.0.3 lib/pups/replace_command.rb
pups-1.0.2 lib/pups/replace_command.rb
pups-1.0.1 lib/pups/replace_command.rb
pups-1.0.0 lib/pups/replace_command.rb