Skip to content

Commit

Permalink
slightly better.
Browse files Browse the repository at this point in the history
  • Loading branch information
toothrot committed Jul 2, 2010
1 parent 002fed1 commit 406faa5
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions json-hash.rb
Expand Up @@ -2,16 +2,19 @@ class Hash
def dottable!
class << self
def method_missing(sym, *args, &block)
result = fetch(sym.to_s) { fetch(sym) }
if result.respond_to?(:dottable!) then result.dottable! else result end
convert_result(fetch(sym) { fetch(sym.to_s) })
rescue IndexError
super
end

alias_method :regular_reader, :[] unless method_defined?(:regular_reader)

def [](key)
result = regular_reader(key)
convert_result(regular_reader(key))
end

private
def convert_result(result)
if result.respond_to?(:dottable!) then result.dottable! else result end
end
end
Expand Down

0 comments on commit 406faa5

Please sign in to comment.