Sha256: 4dcbb3dfc3f6ff8c15372fcdfd6d1ef12610550cde628200806436b45f67e756

Contents?: true

Size: 919 Bytes

Versions: 7

Compression:

Stored size: 919 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

7 entries across 7 versions & 1 rubygems

Version Path
hedgelog-0.1.9 lib/hedgelog/scrub_replacement.rb
hedgelog-0.1.8 lib/hedgelog/scrub_replacement.rb
hedgelog-0.1.7 lib/hedgelog/scrub_replacement.rb
hedgelog-0.1.6 lib/hedgelog/scrub_replacement.rb
hedgelog-0.1.5 lib/hedgelog/scrub_replacement.rb
hedgelog-0.1.4 lib/hedgelog/scrub_replacement.rb
hedgelog-0.1.3 lib/hedgelog/scrub_replacement.rb