Module: Carnivore::Utils::Params

Included in:
Carnivore::Utils
Defined in:
lib/carnivore/utils/params.rb

Overview

Parameter helper methods generally aimed at Hash instances

Instance Method Summary (collapse)

Instance Method Details

- (Object, NilClass) retrieve(hash, *args)

Retrieve value in hash at given path

Parameters:

  • hash (Hash)

    hash to walk into

  • args (String, Symbol)

    argument list to walk in hash

Returns:

  • (Object, NilClass)


32
33
34
35
36
37
38
39
40
# File 'lib/carnivore/utils/params.rb', line 32

def retrieve(hash, *args)
  valids = [::Hash, hash.is_a?(Class) ? hash : hash.class]
  args.flatten.inject(hash) do |memo, key|
    break unless valids.detect{ |valid_type|
      memo.is_a?(valid_type) || memo == valid_type
    }
    memo[key.to_s] || memo[key.to_sym] || break
  end
end

- (Hash) symbolize_hash(hash)

Symbolize keys in hash

Parameters:

Returns:

  • (Hash)

    new hash instance with symbolized keys



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/carnivore/utils/params.rb', line 11

def symbolize_hash(hash)
  Hash[*(
      hash.map do |k,v|
        if(k.is_a?(String))
          key = k.gsub(/(?<![A-Z])([A-Z])/, '_\1').sub(/^_/, '').downcase.to_sym
        else
          key = k
        end
        [
          key,
          v.is_a?(Hash) ? symbolize_hash(v) : v
        ]
      end.flatten(1)
  )]
end