Class: ParamsCleaner::Whitelist
- Inherits:
-
Object
- Object
- ParamsCleaner::Whitelist
- Defined in:
- lib/params_cleaner/whitelist.rb
Instance Method Summary collapse
- #_sanitize_hash(hash, parent) ⇒ Object
- #_top_level_keys ⇒ Object
- #_valid_keys_for_parent(parent) ⇒ Object
- #_whitelist_hash ⇒ Object
-
#initialize(whitelist) ⇒ Whitelist
constructor
A new instance of Whitelist.
- #sanitize(item, parent = nil) ⇒ Object
Constructor Details
#initialize(whitelist) ⇒ Whitelist
Returns a new instance of Whitelist.
3 4 5 |
# File 'lib/params_cleaner/whitelist.rb', line 3 def initialize(whitelist) @whitelist = whitelist end |
Instance Method Details
#_sanitize_hash(hash, parent) ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/params_cleaner/whitelist.rb', line 17 def _sanitize_hash(hash, parent) valid_keys = _valid_keys_for_parent(parent) valid_pairs = hash.select { |key, value| valid_keys.include?(key.to_sym) } sanitized_pairs = valid_pairs.map { |key, value| [key, sanitize(value, key.to_sym)] } HashWithIndifferentAccess.new(Hash[sanitized_pairs]) end |
#_top_level_keys ⇒ Object
25 26 27 |
# File 'lib/params_cleaner/whitelist.rb', line 25 def _top_level_keys @top_level_keys ||= @whitelist.reject { |item| item.kind_of?(Hash) } end |
#_valid_keys_for_parent(parent) ⇒ Object
29 30 31 32 33 34 |
# File 'lib/params_cleaner/whitelist.rb', line 29 def _valid_keys_for_parent(parent) _whitelist_hash.keys.tap do |keys| keys.concat(_top_level_keys) if parent.nil? keys.concat(_whitelist_hash[parent]) if _whitelist_hash.has_key?(parent) end end |
#_whitelist_hash ⇒ Object
36 37 38 |
# File 'lib/params_cleaner/whitelist.rb', line 36 def _whitelist_hash @whitelist_hash ||= @whitelist.last.is_a?(Hash) ? @whitelist.last : {} end |
#sanitize(item, parent = nil) ⇒ Object
7 8 9 10 11 12 13 14 15 |
# File 'lib/params_cleaner/whitelist.rb', line 7 def sanitize(item, parent = nil) if item.kind_of?(Hash) _sanitize_hash(item, parent) elsif item.kind_of?(Array) item.map { |item| sanitize(item, parent) } else item end end |