Sha256: 9b6d44d0463f8612856fd4dfa08adaebb6485642f05c742cd4404d5fafe6ccad

Contents?: true

Size: 973 Bytes

Versions: 61

Compression:

Stored size: 973 Bytes

Contents

module Spider
    
    # Utility that makes the including Hash accept a dotted syntax for keys.
    # The dotted syntax can be used to access and create on the fly sub-hashes. 
    # Example:
    #   h = MultiLevelHash.new
    #   h['one.two.three'] = 'some val'
    #   p h => {'one' => {'two' => {'three' => 'some val'}}}
    #   p h['one.two.three'] => 'some val'
    #   p h['four.five'] => nil
    module HashDottedAccess
        def []=(key, val)
            parts = key.to_s.split('.', 2)
            return super(key, val) unless parts[1]
            self[parts[0]] ||= self.class.new
            self[parts[0]][parts[1]] = val
        end
        
        def [](key)
            parts = key.to_s.split('.', 2)
            return super(key) unless parts[1]
            return self[parts[0]][parts[1]]
        end
    end
        
    # Hash including HashDottedAccess.
    class MultiLevelHash < Hash
        include HashDottedAccess
        
    end
    
    
end

Version data entries

61 entries across 61 versions & 1 rubygems

Version Path
spiderfw-0.6.1 lib/spiderfw/utils/multi_level_hash.rb
spiderfw-0.6.0 lib/spiderfw/utils/multi_level_hash.rb
spiderfw-0.5.19 lib/spiderfw/utils/multi_level_hash.rb
spiderfw-0.5.18 lib/spiderfw/utils/multi_level_hash.rb
spiderfw-0.5.17 lib/spiderfw/utils/multi_level_hash.rb
spiderfw-0.5.16 lib/spiderfw/utils/multi_level_hash.rb
spiderfw-0.5.15 lib/spiderfw/utils/multi_level_hash.rb
spiderfw-0.5.14 lib/spiderfw/utils/multi_level_hash.rb
spiderfw-0.5.13 lib/spiderfw/utils/multi_level_hash.rb
spiderfw-0.5.12 lib/spiderfw/utils/multi_level_hash.rb
spiderfw-0.5.11 lib/spiderfw/utils/multi_level_hash.rb
spiderfw-0.5.10 lib/spiderfw/utils/multi_level_hash.rb
spiderfw-0.5.9 lib/spiderfw/utils/multi_level_hash.rb
spiderfw-0.5.7 lib/spiderfw/utils/multi_level_hash.rb
spiderfw-0.5.6 lib/spiderfw/utils/multi_level_hash.rb
spiderfw-0.5.5 lib/spiderfw/utils/multi_level_hash.rb
spiderfw-0.5.4 lib/spiderfw/utils/multi_level_hash.rb
spiderfw-0.5.3 lib/spiderfw/utils/multi_level_hash.rb
spiderfw-0.5.2 lib/spiderfw/utils/multi_level_hash.rb
spiderfw-0.5.1 lib/spiderfw/utils/multi_level_hash.rb