Class: Hash

Inherits:
Object show all
Defined in:
lib/riak/core_ext/blank.rb,
lib/riak/core_ext/slice.rb,
lib/riak/core_ext/deep_dup.rb,
lib/riak/core_ext/to_param.rb,
lib/riak/core_ext/stringify_keys.rb,
lib/riak/core_ext/symbolize_keys.rb

Instance Method Summary collapse

Instance Method Details

#deep_dupObject

Returns a deep copy of hash.



4
5
6
7
8
9
10
11
# File 'lib/riak/core_ext/deep_dup.rb', line 4

def deep_dup
  duplicate = self.dup
  duplicate.each_pair do |k, v|
    tv = duplicate[k]
    duplicate[k] = tv.is_a?(Hash) && v.is_a?(Hash) ? tv.deep_dup : v
  end
  duplicate
end

#slice(*keys) ⇒ Object



3
4
5
6
7
8
# File 'lib/riak/core_ext/slice.rb', line 3

def slice(*keys)
  allowed = Set.new(respond_to?(:convert_key) ? keys.map { |key| convert_key(key) } : keys)
  hash = {}
  allowed.each { |k| hash[k] = self[k] if has_key?(k) }
  hash
end

#slice!(*keys) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/riak/core_ext/slice.rb', line 10

def slice!(*keys)
  keys = keys.map! { |key| convert_key(key) } if respond_to?(:convert_key)
  omit = slice(*self.keys - keys)
  hash = slice(*keys)
  replace(hash)
  omit
end

#stringify_keysObject



3
4
5
6
7
8
# File 'lib/riak/core_ext/stringify_keys.rb', line 3

def stringify_keys
  inject({}) do |hash, pair|
    hash[pair[0].to_s] = pair[1]
    hash
  end
end

#symbolize_keysObject



3
4
5
6
7
8
# File 'lib/riak/core_ext/symbolize_keys.rb', line 3

def symbolize_keys
  inject({}) do |hash, pair|
    hash[pair[0].to_sym] = pair[1]
    hash
  end
end

#to_param(namespace = nil) ⇒ Object



25
26
27
28
29
# File 'lib/riak/core_ext/to_param.rb', line 25

def to_param(namespace = nil)
  collect do |key, value|
    value.to_query(namespace ? "#{namespace}[#{key}]" : key)
  end.sort * '&'
end