Module: CrazyHashFilter

Defined in:
lib/crazy_hash_filter.rb,
lib/crazy_hash_filter/version.rb

Constant Summary collapse

VERSION =
"0.0.3"

Class Method Summary collapse

Class Method Details

.process(hash, rules) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/crazy_hash_filter.rb', line 7

def self.process(hash, rules)
  return nil if hash.nil?

  raise "Is not a Hash" unless hash.is_a?(Hash)

  rules       = HashWithIndifferentAccess.new(rules || {}) unless rules.is_a?(HashWithIndifferentAccess)
  child_rules = rules[:child_rules] || {}
  result      = {}
  select      = rules[:select].collect(&:to_sym) unless rules[:select].nil?

  hash.each do |key, value|
    next if !select.nil? && !select.include?(key.to_sym)

    if child_rules[key].nil?
      result[key] = value
    else
      if value.is_a?(Array)
        result[key] = self.filter_array(value, child_rules[key])
      else
        result[key] = self.process(value, child_rules[key])
      end
    end
  end

  result
end