lib/ruote/util/lookup.rb in ruote-2.1.9 vs lib/ruote/util/lookup.rb in ruote-2.1.10

- old
+ new

@@ -32,12 +32,13 @@ def Ruote.lookup (collection, key, container_lookup=false) key, rest = pop_key(key) value = flookup(collection, key) + return [ key, collection ] if container_lookup && rest.size == 0 return [ rest.first, value ] if container_lookup && rest.size == 1 - return value if rest.empty? + return value if rest.size == 0 return nil if value == nil lookup(value, rest) end @@ -57,10 +58,29 @@ else collection[key] = value end end - protected + # h = { 'customer' => { 'name' => 'alpha', 'rank' => '1st' } } + # r = Ruote.unset(h, 'customer.rank') + # + # h # => { 'customer' => { 'name' => 'alpha' } } + # r # => '1st' + # + def Ruote.unset (collection, key) + + k, c = lookup(collection, key, true) + + return collection.delete(key) unless c + + if c.is_a?(Array) + c.delete_at(Integer(k)) rescue nil + else + c.delete(k) + end + end + + protected # well... def Ruote.pop_key (key) ks = key.is_a?(String) ? key.split('.') : key