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-1.0.1 lib/spiderfw/utils/multi_level_hash.rb
spiderfw-1.0.0 lib/spiderfw/utils/multi_level_hash.rb
spiderfw-0.6.39 lib/spiderfw/utils/multi_level_hash.rb
spiderfw-0.6.38 lib/spiderfw/utils/multi_level_hash.rb
spiderfw-0.6.37 lib/spiderfw/utils/multi_level_hash.rb
spiderfw-0.6.35 lib/spiderfw/utils/multi_level_hash.rb
spiderfw-0.6.34 lib/spiderfw/utils/multi_level_hash.rb
spiderfw-0.6.33 lib/spiderfw/utils/multi_level_hash.rb
spiderfw-0.6.32 lib/spiderfw/utils/multi_level_hash.rb
spiderfw-0.6.31 lib/spiderfw/utils/multi_level_hash.rb
spiderfw-0.6.30 lib/spiderfw/utils/multi_level_hash.rb
spiderfw-0.6.29 lib/spiderfw/utils/multi_level_hash.rb
spiderfw-0.6.28 lib/spiderfw/utils/multi_level_hash.rb
spiderfw-0.6.27 lib/spiderfw/utils/multi_level_hash.rb
spiderfw-0.6.26 lib/spiderfw/utils/multi_level_hash.rb
spiderfw-0.6.26.pre1 lib/spiderfw/utils/multi_level_hash.rb
spiderfw-0.6.25 lib/spiderfw/utils/multi_level_hash.rb
spiderfw-0.6.24 lib/spiderfw/utils/multi_level_hash.rb
spiderfw-0.6.23 lib/spiderfw/utils/multi_level_hash.rb
spiderfw-0.6.22 lib/spiderfw/utils/multi_level_hash.rb