Class: Hash

Inherits:
Object show all
Defined in:
lib/poolparty/core/hash.rb,
lib/poolparty/schema.rb

Overview

Hash extentions

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/poolparty/schema.rb', line 68

def method_missing(sym, *args, &block)
  if has_key?(sym.to_sym)
    fetch(sym)
  elsif has_key?(sym.to_s)
    fetch(sym.to_s)
  else
    super
  end
end

Instance Method Details

#append(other_hash) ⇒ Object

TODO: deprecate def extract!(&block)

o = Hash[*select(&block).flatten]
o.keys.each {|k| self.delete(k) }
o

end



50
51
52
53
54
55
56
57
# File 'lib/poolparty/core/hash.rb', line 50

def append(other_hash)
  returning Hash.new do |h|
    h.merge!(self)
    other_hash.each do |k,v|
      h[k] = has_key?(k) ? [self[k], v].flatten.uniq : v
    end
  end
end

#append!(other_hash) ⇒ Object



59
60
61
62
63
64
# File 'lib/poolparty/core/hash.rb', line 59

def append!(other_hash)
  other_hash.each do |k,v|
    self[k] = has_key?(k) ? [self[k], v].flatten.uniq : v
  end
  self
end

#choose(&block) ⇒ Object

Return a hash of all the elements where the block evaluates to true



21
22
23
# File 'lib/poolparty/core/hash.rb', line 21

def choose(&block)
  Hash[*self.select(&block).inject([]){|res,(k,v)| res << k << v}]
end

#next_sorted_key(from) ⇒ Object



80
81
82
83
# File 'lib/poolparty/core/hash.rb', line 80

def next_sorted_key(from)
  idx = (size - keys.sort.index(from))
  keys.sort[idx - 1]
end

#safe_merge(other_hash) ⇒ Object



66
67
68
# File 'lib/poolparty/core/hash.rb', line 66

def safe_merge(other_hash)
  merge(other_hash.delete_if {|k,v| has_key?(k) })
end

#safe_merge!(other_hash) ⇒ Object



70
71
72
# File 'lib/poolparty/core/hash.rb', line 70

def safe_merge!(other_hash)
  merge!(other_hash.delete_if {|k,v| has_key?(k) && !v.nil? })
end

#stringify_keysObject



85
86
87
# File 'lib/poolparty/core/hash.rb', line 85

def stringify_keys
  dup.stringify_keys!
end

#stringify_keys!Object

Converts all of the keys to strings



90
91
92
93
94
95
96
97
98
# File 'lib/poolparty/core/hash.rb', line 90

def stringify_keys!
  keys.each{|k| 
    v = delete(k)
    self[k.to_s] = v
    v.stringify_keys! if v.is_a?(Hash)
    v.each{|p| p.stringify_keys! if p.is_a?(Hash)} if v.is_a?(Array)
  }
  self
end

#symbolize_keys(key_modifier = nil) ⇒ Object



100
101
102
# File 'lib/poolparty/core/hash.rb', line 100

def symbolize_keys(key_modifier=nil)
  dup.symbolize_keys!(key_modifier)
end

#symbolize_keys!(key_modifier = nil) ⇒ Object

Converts all of the keys to strings can pass in a :key_modifier that will be sent to each key, before being symbolized. This can be usefull if you want to downcase, or snake_case each key. >> => {‘AvailabilityZone’=>“us-east-1a” }.symbolize_keys(:snake_case)

> :placement=>{:availability_zone=>“us-east-1a”}



109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/poolparty/core/hash.rb', line 109

def symbolize_keys!(key_modifier=nil) 
  keys.each{|k| 
    v = delete(k)
    if key_modifier && k.respond_to?(key_modifier)
      k = k.send(key_modifier)
    end
    self[k.to_sym] = v
    v.symbolize_keys!(key_modifier) if v.is_a?(Hash)
    v.each{|p| p.symbolize_keys!(key_modifier) if p.is_a?(Hash)} if v.is_a?(Array)
  }
  self
end

#to_cloudObject



122
123
124
125
126
127
128
# File 'lib/poolparty/core/hash.rb', line 122

def to_cloud
  $:.unshift("#{::File.dirname(__FILE__)}/../../poolparty")
  require "poolparty"
  cld = Cloud.new((fetch(:name) rescue "hashed_cloud"))
  cld.set_vars_from_options(self)
  cld
end

#to_instance_variables(inst = nil) ⇒ Object



25
26
27
28
29
30
# File 'lib/poolparty/core/hash.rb', line 25

def to_instance_variables(inst=nil)
  each do |k,v|
    inst.instance_variable_set "@#{k}", v
    inst.class.send :attr_reader, k if inst
  end
end

#to_osObject



74
75
76
77
78
# File 'lib/poolparty/core/hash.rb', line 74

def to_os
  m={}
  each {|k,v| m[k] = v.to_os }
  MyOpenStruct.new(m)
end

#values_at(*indices) ⇒ Object

extracted from activesupport Returns an array of the values at the specified indices:

hash = HashWithIndifferentAccess.new
hash[:a] = "x"
hash[:b] = "y"
hash.values_at("a", "b") # => ["x", "y"]


39
40
41
# File 'lib/poolparty/core/hash.rb', line 39

def values_at(*indices)
  indices.collect {|key| self[key]}
end