Sha256: 7c28497e7b7a48f6005501776b6c1cb5131ebb37398a5baaf17587b926b06926

Contents?: true

Size: 915 Bytes

Versions: 1

Compression:

Stored size: 915 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.casecmp(@key.to_s).zero?

        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

1 entries across 1 versions & 1 rubygems

Version Path
hedgelog-0.1.11 lib/hedgelog/scrub_replacement.rb