Sha256: 1992dc8c006f204f7b9c5cc48d617f860fbb680ba8b37dbcaa6d58ebe57e2066

Contents?: true

Size: 1.02 KB

Versions: 7

Compression:

Stored size: 1.02 KB

Contents

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

    def scrub_string(string)
      string.gsub!(@match_regex) do
        quote1 = Regexp.last_match[1]
        sep = Regexp.last_match[2]
        whitespace = Regexp.last_match[3]
        quote2 = Regexp.last_match[4]
        rest = Regexp.last_match[6]
        "#{quote1}#{@key}#{quote1}#{sep}#{whitespace}#{quote2}#{@replacement}#{quote2}#{rest}"
      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.2.1.alpha.4 lib/hedgelog/scrub_replacement.rb
hedgelog-0.2.1.alpha.3 lib/hedgelog/scrub_replacement.rb
hedgelog-0.2.1.alpha.2 lib/hedgelog/scrub_replacement.rb
hedgelog-0.1.13.alpha.1 lib/hedgelog/scrub_replacement.rb
hedgelog-0.2.1.alpha1 lib/hedgelog/scrub_replacement.rb
hedgelog-0.2.0 lib/hedgelog/scrub_replacement.rb
hedgelog-0.1.12 lib/hedgelog/scrub_replacement.rb