Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/repo_manager/core/hash.rb

Direct Known Subclasses

RepoManager::Settings

Instance Method Summary collapse

Instance Method Details

#deep_cloneObject

From ActiveSupport CoreExt

Returns a deep copy of hash.

hash = { :a => { :b => 'b' } }
dup  = hash.deep_clone
dup[:a][:c] = 'c'

hash[:a][:c] #=> nil
dup[:a][:c]  #=> "c"


93
94
95
96
97
98
99
100
# File 'lib/repo_manager/core/hash.rb', line 93

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

#recursively_stringify_keys!Object



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/repo_manager/core/hash.rb', line 71

def recursively_stringify_keys!
  self.stringify_keys!
  self.values.each do |v|
    if v.is_a? Hash
      v.recursively_stringify_keys!
    elsif v.is_a? Array
      v.recursively_stringify_keys!
    end
  end
  self
end

#recursively_symbolize_keys!Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/repo_manager/core/hash.rb', line 48

def recursively_symbolize_keys!
  self.symbolize_keys!
  self.values.each do |v|
    if v.is_a? Hash
      v.recursively_symbolize_keys!
    elsif v.is_a? Array
      v.recursively_symbolize_keys!
    end
  end
  self
end

#stringify_keysObject



64
65
66
67
68
69
# File 'lib/repo_manager/core/hash.rb', line 64

def stringify_keys
  inject({}) do |options, (key, value)|
    options[(key.to_s rescue key) || key] = value
    options
  end
end

#stringify_keys!Object



60
61
62
# File 'lib/repo_manager/core/hash.rb', line 60

def stringify_keys!
  self.replace(self.stringify_keys)
end

#symbolize_keysObject



41
42
43
44
45
46
# File 'lib/repo_manager/core/hash.rb', line 41

def symbolize_keys
  inject({}) do |options, (key, value)|
    options[(key.to_sym rescue key) || key] = value
    options
  end
end

#symbolize_keys!Object

active_support hash key functions



37
38
39
# File 'lib/repo_manager/core/hash.rb', line 37

def symbolize_keys!
  self.replace(self.symbolize_keys)
end

#to_confObject

YAML suitable for configuration files

returns sorted YAML if Ruby 1.8 returns insertion ordered YAML on Ruby 1.9+



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/repo_manager/core/hash.rb', line 9

def to_conf
  unless RUBY_VERSION =~ /^1.8/
    # fix issue with strings that can end up in non UTF-8 encodings, like
    # ASCII-8BIT, force encoding so that they are not written to YAML as binary
    self.each_pair { |k,v| self[k] = ((v.is_a? String) ? v.force_encoding("UTF-8") : v)}

    # allow 1.9+ to_yaml to do insertion ordered conf files
    return to_yaml
  end

  opts = {}
  YAML::quick_emit( object_id, opts ) do |out|
    out.map( taguri, to_yaml_style ) do |map|
      sorted_keys = keys
      sorted_keys = begin
        sorted_keys.sort
      rescue
        sorted_keys.sort_by {|k| k.to_s} rescue sorted_keys
      end

      sorted_keys.each do |k|
        map.add( k, fetch(k) )
      end
    end
  end
end