lib/ruote/util/lookup.rb in ruote-2.1.11 vs lib/ruote/util/lookup.rb in ruote-2.2.0
- old
+ new
@@ -1,7 +1,7 @@
#--
-# Copyright (c) 2005-2010, John Mettraux, jmettraux@gmail.com
+# Copyright (c) 2005-2011, John Mettraux, jmettraux@gmail.com
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
@@ -27,12 +27,14 @@
# h = { 'a' => { 'b' => [ 1, 3, 4 ] } }
#
# p Ruote.lookup(h, 'a.b.1') # => 3
#
- def Ruote.lookup (collection, key, container_lookup=false)
+ def Ruote.lookup(collection, key, container_lookup=false)
+ return collection if key == '.'
+
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
@@ -46,11 +48,11 @@
#
# Ruote.set(h, 'customer.name', 'bravo')
#
# h #=> { 'customer' => { 'name' => 'bravo' } }
#
- def Ruote.set (collection, key, value)
+ def Ruote.set(collection, key, value)
k, c = lookup(collection, key, true)
if c
k = k.to_i if c.is_a?(Array)
@@ -64,11 +66,11 @@
# r = Ruote.unset(h, 'customer.rank')
#
# h # => { 'customer' => { 'name' => 'alpha' } }
# r # => '1st'
#
- def Ruote.unset (collection, key)
+ def Ruote.unset(collection, key)
k, c = lookup(collection, key, true)
return collection.delete(key) unless c
@@ -79,27 +81,27 @@
end
end
protected # well...
- def Ruote.pop_key (key)
+ def Ruote.pop_key(key)
ks = key.is_a?(String) ? key.split('.') : key
[ narrow_key(ks.first), ks[1..-1] ]
end
- def Ruote.narrow_key (key)
+ def Ruote.narrow_key(key)
return 0 if key == '0'
i = key.to_i
return i if i != 0
key
end
- def Ruote.flookup (collection, key)
+ def Ruote.flookup(collection, key)
value = (collection[key] rescue nil)
if value == nil and key.is_a?(Fixnum)
value = (collection[key.to_s] rescue nil)