Class: Hash

Inherits:
Object show all
Defined in:
lib/arachni/ruby/hash.rb

Overview

Copyright 2010-2014 Tasos Laskos <[email protected]>

This file is part of the Arachni Framework project and is subject to
redistribution and commercial restrictions. Please see the Arachni Framework
web site for more information on licensing and terms of use.

Instance Method Summary collapse

Instance Method Details

#apply_recursively(method, *args) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/arachni/ruby/hash.rb', line 68

def apply_recursively( method, *args )
    modified = {}

    each do |k, v|
        modified[k.send(method, *args)] = v.is_a?( Hash ) ?
            v.apply_recursively(method, *args) : v.send(method, *args)
    end

    modified
end

#downcaseHash

Returns Self with the keys and values converted to lower-case strings.

Returns:

  • (Hash)

    Self with the keys and values converted to lower-case strings.



81
82
83
84
85
86
87
88
# File 'lib/arachni/ruby/hash.rb', line 81

def downcase
    stringify_keys.inject({}) do |h, (k, v)|
        k = k.downcase if k.is_a?( String )
        v = v.downcase if v.is_a?( String )
        h[k] = v
        h
    end
end

#find_symbol_keys_recursivelyArray<Symbol>

Returns all symbol keys from self and children hashes.

Returns:

  • (Array<Symbol>)

    Returns all symbol keys from self and children hashes.



104
105
106
107
108
109
110
111
# File 'lib/arachni/ruby/hash.rb', line 104

def find_symbol_keys_recursively
    flat = []
    each do |k, v|
        flat << k
        flat |= v.find_symbol_keys_recursively if v.is_a?( Hash ) &&v.any?
    end
    flat.reject { |i| !i.is_a? Symbol }
end

#recodeHash

Recursively converts the hash’s string data to UTF8.

Returns:

  • (Hash)

    Copy of self with all strings recoded to UTF8.



94
95
96
97
98
99
100
# File 'lib/arachni/ruby/hash.rb', line 94

def recode
    recoded = {}
    each do |k, v|
        recoded[k] = (v.respond_to?( :recode ) ? v.recode : v)
    end
    recoded
end

#stringifyHash

Returns Hash with self‘s keys and values recursively converted to strings.

Returns:

  • (Hash)

    Hash with self‘s keys and values recursively converted to strings.



50
51
52
# File 'lib/arachni/ruby/hash.rb', line 50

def stringify
    apply_recursively(:to_s)
end

#stringify_keys(recursively = true) ⇒ Hash

Converts the hash keys to strings.

Parameters:

  • recursively (Boolean) (defaults to: true)

    Go through the Hash recursively?

Returns:

  • (Hash)

    Hash with self‘s keys recursively converted to strings.



22
23
24
25
26
27
28
# File 'lib/arachni/ruby/hash.rb', line 22

def stringify_keys( recursively = true )
    stringified = {}
    each do |k, v|
        stringified[k.to_s] = (recursively && v.is_a?( Hash ) ? v.stringify_keys : v)
    end
    stringified
end

#stringify_recursively_and_freezeObject



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/arachni/ruby/hash.rb', line 54

def stringify_recursively_and_freeze
    modified = {}

    each do |k, v|
        if v.is_a?( Hash )
            modified[k.to_s.freeze] = v.stringify_recursively_and_freeze
        else
            modified[k.to_s.freeze] = v.to_s.freeze
        end
    end

    modified.freeze
end

#symbolize_keys(recursively = true) ⇒ Hash

Converts the hash keys to symbols.

Parameters:

  • recursively (Boolean) (defaults to: true)

    Go through the Hash recursively?

Returns:

  • (Hash)

    Hash with self‘s keys recursively converted to symbols.



37
38
39
40
41
42
43
44
45
46
# File 'lib/arachni/ruby/hash.rb', line 37

def symbolize_keys( recursively = true )
    symbolize = {}
    each do |k, v|
        k = k.respond_to?(:to_sym) ? k.to_sym : k

        symbolize[k] = (recursively && v.is_a?( Hash ) ?
            v.symbolize_keys : v)
    end
    symbolize
end