Sha256: 52e5a1d1b3975648f928b40435587bcc01a2d5b3a55759859a466e3acf6304b9

Contents?: true

Size: 925 Bytes

Versions: 3

Compression:

Stored size: 925 Bytes

Contents

class Hedgelog
  class ScrubReplacement
    def initialize(key, replacement)
      @key = key
      @replacement = replacement
      @match_regex = /("?)#{@key}("?[=:]\s*"?)(.+?)(["&,;\s]|$)/
    end

    def scrub_string(string)
      string.gsub!(@match_regex) do
        start = Regexp.last_match[1]
        eql = Regexp.last_match[2]
        delim = Regexp.last_match[4]
        "#{start}#{@key}#{eql}#{@replacement}#{delim}"
      end
    end

    def scrub_hash(hash)
      hash.each do |key, val|
        next hash[key] = @replacement if key.to_s.downcase == @key.to_s.downcase
        scrub_thing(val)
      end
    end

    def scrub_array(array)
      array.each do |val|
        scrub_thing(val)
      end
    end

    private

    def scrub_thing(thing)
      scrub_string(thing) if thing.is_a?(String)
      scrub_array(thing) if thing.is_a?(Array)
      scrub_hash(thing) if thing.is_a?(Hash)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
hedgelog-0.1.2 lib/hedgelog/scrub_replacement.rb
hedgelog-0.1.1 lib/hedgelog/scrub_replacement.rb
hedgelog-0.1.0 lib/hedgelog/scrub_replacement.rb