lib/jsonify/builder.rb in jsonify-0.1.3 vs lib/jsonify/builder.rb in jsonify-0.2.0
- old
+ new
@@ -114,15 +114,16 @@
# }
#
# If a block is given and an argument is passed, the argument it is assumed to be an
# Array (more specifically, an object that responds to `each`).
# The argument is iterated over and each item is yielded to the block.
- # The result of the block becomes an array item of a JsonArray.
+ # The result of the block becomes an array item of the JsonArray.
#
# @example Map an of array of links to an array of JSON objects
# json.links(@links) do |link|
- # {:rel => link.first, :href => link.last}
+ # json.rel link.first
+ # json.href link.last
# end
#
# @example compiles to something like ...
# "links": [
# {
@@ -151,11 +152,23 @@
if args.nil?
block.call
else
__array
args.each do |arg|
- @stack[@level].add block.call(arg)
+ @level += 1
+ block.call(arg)
+ @level -= 1
+
+ value = @stack.pop
+
+ # If the object created was an array with a single value
+ # assume that just the value should be added
+ if (JsonArray === value && value.values.length <= 1)
+ value = value.values.first
+ end
+
+ @stack[@level].add value
end
end
# Set the value on the pair to the object at the top of the stack
pair.value = @stack[@level]
@@ -200,6 +213,6 @@
def __array
@stack[@level] ||= JsonArray.new
end
end
-end
\ No newline at end of file
+end