Sha256: 7f30c5050d4e1f93ac485c71367285c877ea736805026b4d725287425c3c0f5a

Contents?: true

Size: 707 Bytes

Versions: 1

Compression:

Stored size: 707 Bytes

Contents

require 'delegate'

module Saneitized

  class Hash < ::SimpleDelegator

    def initialize(hash = {}, options = {})
      @options = options
      @key_blacklist = Array(options.fetch(:key_blacklist){[]})
      new_hash = {}
      hash.each do |key, value|
        new_hash[key] = convert_key_value(key, value)
      end
      super(new_hash)
      self
    end

    def []=(key, value)
      super key, Saneitized.convert(value, @options)
    end

    def merge!(*args, &block)
      raise NotImplementedError
    end

    private

    def convert_key_value(key, value)
      if @key_blacklist.include? key
        value
      else
        Saneitized.convert(value, @options)
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
saneitized-1.4.0 lib/saneitized/hash.rb