class InputSanitizer::RestrictedHash

Public Class Methods

new(allowed_keys) click to toggle source
Calls superclass method
# File lib/input_sanitizer/restricted_hash.rb, line 3
def initialize(allowed_keys)
  @allowed_keys = Set.new(allowed_keys)
  super()
end

Public Instance Methods

[](key) click to toggle source
# File lib/input_sanitizer/restricted_hash.rb, line 8
def [](key)
  raise_not_allowed(key) unless key_allowed?(key)
  fetch(key, nil)
end
[]=(key, val) click to toggle source
Calls superclass method
# File lib/input_sanitizer/restricted_hash.rb, line 13
def []=(key, val)
  @allowed_keys.add(key)
  super
end
key_allowed?(key) click to toggle source
# File lib/input_sanitizer/restricted_hash.rb, line 33
def key_allowed?(key)
  @allowed_keys.include?(key)
end
merge(hash, &block) click to toggle source
Calls superclass method
# File lib/input_sanitizer/restricted_hash.rb, line 28
def merge(hash, &block)
  @allowed_keys.merge(Set[*hash.keys])
  super
end
merge!(hash, &block) click to toggle source
Calls superclass method
# File lib/input_sanitizer/restricted_hash.rb, line 23
def merge!(hash, &block)
  @allowed_keys.merge(Set[*hash.keys])
  super
end
store(key, val) click to toggle source
Calls superclass method
# File lib/input_sanitizer/restricted_hash.rb, line 18
def store(key, val)
  @allowed_keys.add(key)
  super
end
transform_keys() { |key| ... } click to toggle source
# File lib/input_sanitizer/restricted_hash.rb, line 37
def transform_keys
  return enum_for(:transform_keys) unless block_given?

  new_allowed_keys = @allowed_keys.map { |key| yield(key) }
  result = self.class.new(new_allowed_keys)
  each_key do |key|
    result[yield(key)] = self[key]
  end
  result
end
transform_keys!() { |key| ... } click to toggle source
# File lib/input_sanitizer/restricted_hash.rb, line 48
def transform_keys!
  return enum_for(:transform_keys!) unless block_given?

  @allowed_keys.map! { |key| yield(key) }
  keys.each do |key|
    self[yield(key)] = delete(key)
  end
  self
end

Private Instance Methods

raise_not_allowed(key) click to toggle source
# File lib/input_sanitizer/restricted_hash.rb, line 60
def raise_not_allowed(key)
  msg = "Key not allowed: #{key}"
  raise KeyNotAllowedError, msg
end