Class: Hash
- Defined in:
- lib/core_ext/hash/slice.rb,
lib/core_ext/hash/iterate_nested.rb,
lib/core_ext/hash/symbolize_keys.rb,
lib/core_ext/hash/sorted_yaml_style.rb
Overview
from activesupport
Direct Known Subclasses
Instance Attribute Summary collapse
-
#to_yaml_style ⇒ Object
Returns the value of attribute to_yaml_style.
Instance Method Summary collapse
- #delete_nested_if(data = self, keys = [], &block) ⇒ Object
- #each_nested(data = nil, keys = [], &block) ⇒ Object
- #select_nested(data = self, keys = [], &block) ⇒ Object
- #set_yaml_style(style) ⇒ Object
-
#slice(*keys) ⇒ Object
Returns a new hash with only the given keys.
-
#slice!(*keys) ⇒ Object
Replaces the hash with only the given keys.
-
#symbolize_keys ⇒ Object
Return a new hash with all keys converted to symbols.
-
#symbolize_keys! ⇒ Object
Destructively convert all keys to symbols.
- #to_yaml(opts = {}) ⇒ Object
Instance Attribute Details
#to_yaml_style ⇒ Object
Returns the value of attribute to_yaml_style.
2 3 4 |
# File 'lib/core_ext/hash/sorted_yaml_style.rb', line 2 def to_yaml_style @to_yaml_style end |
Instance Method Details
#delete_nested_if(data = self, keys = [], &block) ⇒ Object
26 27 28 29 30 31 32 33 34 |
# File 'lib/core_ext/hash/iterate_nested.rb', line 26 def delete_nested_if(data = self, keys = [], &block) data.each do |key, value| if yield(keys + [key], value) data.delete(key) else delete_nested_if(value, keys + [key], &block) end end if data.is_a?(Hash) end |
#each_nested(data = nil, keys = [], &block) ⇒ Object
2 3 4 5 6 7 8 9 10 11 |
# File 'lib/core_ext/hash/iterate_nested.rb', line 2 def each_nested(data = nil, keys = [], &block) (data || self).each do |key, value| case value when Hash each_nested(value, keys + [key], &block) else yield(keys + [key], value) end end end |
#select_nested(data = self, keys = [], &block) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/core_ext/hash/iterate_nested.rb', line 13 def select_nested(data = self, keys = [], &block) case data when Hash data.inject({}) do |result, (key, value)| value = select_nested(value, keys + [key], &block) result[key] = value unless value.nil? || value.empty? result end else data if yield(keys, data) end end |
#set_yaml_style(style) ⇒ Object
4 5 6 7 |
# File 'lib/core_ext/hash/sorted_yaml_style.rb', line 4 def set_yaml_style(style) self.to_yaml_style = style each { |key, value| value.set_yaml_style(style) if value.is_a?(Hash) } end |
#slice(*keys) ⇒ Object
Returns a new hash with only the given keys.
5 6 7 8 9 |
# File 'lib/core_ext/hash/slice.rb', line 5 def slice(*keys) hash = self.class.new keys.each { |k| hash[k] = self[k] if has_key?(k) } hash end |
#slice!(*keys) ⇒ Object
Replaces the hash with only the given keys. Returns a hash contained the removed key/value pairs
{:a => 1, :b => 2, :c => 3, :d => 4}.slice!(:a, :b) # => {:c => 3, :d =>4}
14 15 16 17 18 19 |
# File 'lib/core_ext/hash/slice.rb', line 14 def slice!(*keys) omit = slice(*self.keys - keys) hash = slice(*keys) replace(hash) omit end |
#symbolize_keys ⇒ Object
Return a new hash with all keys converted to symbols.
3 4 5 6 7 8 |
# File 'lib/core_ext/hash/symbolize_keys.rb', line 3 def symbolize_keys inject({}) do |, (key, value)| [(key.to_sym rescue key) || key] = value end end |
#symbolize_keys! ⇒ Object
Destructively convert all keys to symbols.
11 12 13 |
# File 'lib/core_ext/hash/symbolize_keys.rb', line 11 def symbolize_keys! self.replace(self.symbolize_keys) end |
#to_yaml(opts = {}) ⇒ Object
9 10 11 12 13 14 15 16 |
# File 'lib/core_ext/hash/sorted_yaml_style.rb', line 9 def to_yaml(opts = {}) YAML::quick_emit(object_id, opts) do |out| out.map(taguri, to_yaml_style) do |map| elements = to_yaml_style == :sorted ? sort : self elements.each { |k, v| map.add(k, v) } end end end |