Sha256: 3c89c58a0fa4ab60b5714b039d75ddeb6feaaf4c2b4837374c79770d21638b5d
Contents?: true
Size: 1.27 KB
Versions: 5
Compression:
Stored size: 1.27 KB
Contents
module InputSanitizer class RestrictedHash < Hash def initialize(allowed_keys) @allowed_keys = Set.new(allowed_keys) super() end def [](key) raise_not_allowed(key) unless key_allowed?(key) fetch(key, nil) end def []=(key, val) @allowed_keys.add(key) super end def store(key, val) @allowed_keys.add(key) super end def merge!(hash, &block) @allowed_keys.merge(Set[*hash.keys]) super end def merge(hash, &block) @allowed_keys.merge(Set[*hash.keys]) super end def key_allowed?(key) @allowed_keys.include?(key) end 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 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 def raise_not_allowed(key) msg = "Key not allowed: #{key}" raise KeyNotAllowedError, msg end end end
Version data entries
5 entries across 5 versions & 1 rubygems