Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed JSON lists.

  • Loading branch information...
commit 356331b981bc247dd38434e0fe47db86240f2620 1 parent 685a761
Julian Connor authored
Showing with 26 additions and 13 deletions.
  1. +26 −13 lib/traverse.rb
View
39 lib/traverse.rb
@@ -145,29 +145,43 @@ def to_s
"<Traversable... >"
end
end
+
class JSON
+
def initialize json
+
setup_underlying_json json
- puts "#{@json}"
- @json.each_pair do |k,v|
- define_singleton_method k do
- if v.is_a? Hash
- JSON.new(v)
- elsif v.is_a? Array
- v.map { |i| JSON.new(i) }
- else
- v
+
+ if @json.is_a? Hash
+ @json.each_pair do |k,v|
+
+ define_singleton_method "_#{k}" do
+ if v.is_a? Hash
+ JSON.new v
+ elsif v.is_a? Array
+ v.map{ |i| JSON.new i }
+ else
+ v
+ end
end
end
- define_singleton_method "_keys" do
+ # _keys_
+ define_singleton_method "_keys_" do
@json.keys
end
+ elsif @json.is_a? Array
+ @json.map! { |i| JSON.new i }
+ end
+ # _length_
+ define_singleton_method "_length_" do
+ @json.length
end
end
private
+ # Overload method_missing, pass method to super
def method_missing m, *args, &block
- self[m] or super
+ @json.send m, *args, &block
end
def setup_underlying_json document
@@ -182,6 +196,5 @@ def setup_underlying_json document
@json = document
end
end
- end
-
+ end # JSON
end
Please sign in to comment.
Something went wrong with that request. Please try again.