Module: EyCloudAwareness::HashExt
- Defined in:
- lib/ey_cloud_awareness/hash_ext.rb
Constant Summary collapse
- XML_ITEM_KEYS =
[ :item, 'item' ]
Instance Method Summary collapse
-
#kill_xml_item_keys! ⇒ Object
:sam => { :item => [{ :foo => :bar }] } into :sam => [=> :bar].
- #recursive_kill_xml_item_keys! ⇒ Object
- #recursive_underscore_keys! ⇒ Object
- #underscore_keys! ⇒ Object
Instance Method Details
#kill_xml_item_keys! ⇒ Object
:sam => { :item => [{ :foo => :bar }] } into :sam => [=> :bar]
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/ey_cloud_awareness/hash_ext.rb', line 31 def kill_xml_item_keys! if keys.length == 1 and XML_ITEM_KEYS.include?(keys.first) raise ArgumentError, "You need to call kill_xml_item_keys! on { :foo => { :items => [...] } } not on { :items => [...] }" end keys.each do |key| if self[key].is_a?(Hash) and self[key].keys.length == 1 and XML_ITEM_KEYS.include?(self[key].keys.first) # self[:sam] = self[:sam]["item"] (using values.first because we don't know if it's :item or "item") self[key] = delete(key).values.first end end self end |
#recursive_kill_xml_item_keys! ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/ey_cloud_awareness/hash_ext.rb', line 44 def recursive_kill_xml_item_keys! kill_xml_item_keys! values.select { |v| v.is_a?(Hash) }.each do |hsh| hsh.recursive_kill_xml_item_keys! end # burst thru at least one level of arrays values.select { |v| v.is_a?(Array) }.each do |ary| ary.each do |v| v.recursive_kill_xml_item_keys! if v.is_a?(Hash) end end self end |
#recursive_underscore_keys! ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/ey_cloud_awareness/hash_ext.rb', line 12 def recursive_underscore_keys! underscore_keys! values.select { |v| v.is_a?(Hash) }.each do |hsh| hsh.recursive_underscore_keys! end # burst thru at least one level of arrays values.select { |v| v.is_a?(Array) }.each do |ary| ary.each do |v| v.recursive_underscore_keys! if v.is_a?(Hash) end end self end |
#underscore_keys! ⇒ Object
4 5 6 7 8 9 |
# File 'lib/ey_cloud_awareness/hash_ext.rb', line 4 def underscore_keys! keys.each do |key| self[key.to_s.underscore] = delete(key) end self end |