Class: Hash

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

Overview

Copyright 2010-2015 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



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

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.



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

def downcase
    my_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.



110
111
112
113
114
115
116
117
# File 'lib/arachni/ruby/hash.rb', line 110

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

#my_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.



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

def my_stringify
    apply_recursively(:to_s)
end

#my_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
29
# File 'lib/arachni/ruby/hash.rb', line 22

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

#my_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.



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

def my_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.my_symbolize_keys : v)
    end
    symbolize
end

#recodeHash

Recursively converts the hash’s string data to UTF8.

Returns:

  • (Hash)

    Copy of ‘self` with all strings recoded to UTF8.



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

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

#recode!Object



103
104
105
106
# File 'lib/arachni/ruby/hash.rb', line 103

def recode!
    each { |_, v| v.recode! if v.respond_to?( :recode! ) }
    self
end

#stringify_recursively_and_freezeObject



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

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