module Sanitize::Config

Constants

BASIC
DEFAULT
RELAXED
RESTRICTED

Public Class Methods

freeze_config(config) click to toggle source

Deeply freezes and returns the given configuration Hash.

# File lib/sanitize/config.rb, line 9
def self.freeze_config(config)
  if Hash === config
    config.each_value {|c| freeze_config(c) }
  elsif Array === config || Set === config
    config.each {|c| freeze_config(c) }
  end

  config.freeze
end
merge(config, other_config = {}) click to toggle source

Returns a new Hash containing the result of deeply merging other_config into config. Does not modify config or other_config.

This is the safest way to use a built-in Sanitize config as the basis for your own custom config.

# File lib/sanitize/config.rb, line 24
def self.merge(config, other_config = {})
  raise ArgumentError, 'config must be a Hash' unless Hash === config
  raise ArgumentError, 'other_config must be a Hash' unless Hash === other_config

  merged = {}
  keys   = Set.new(config.keys + other_config.keys)

  keys.each do |key|
    oldval = config[key]

    if other_config.has_key?(key)
      newval = other_config[key]

      if Hash === oldval && Hash === newval
        merged[key] = oldval.empty? ? newval.dup : merge(oldval, newval)
      elsif Array === newval && key != :transformers
        merged[key] = Set.new(newval)
      else
        merged[key] = can_dupe?(newval) ? newval.dup : newval
      end
    else
      merged[key] = can_dupe?(oldval) ? oldval.dup : oldval
    end
  end

  merged
end

Private Class Methods

can_dupe?(value) click to toggle source

Returns ‘true` if `dup` may be safely called on value, `false` otherwise.

# File lib/sanitize/config.rb, line 54
def self.can_dupe?(value)
  !(true == value || false == value || value.nil? || Method === value || Numeric === value || Symbol === value)
end