Method: Jsonify::Builder#array!

Defined in:
lib/jsonify/builder.rb

#array!(args) ⇒ Object

Creates array of json objects in current element from array passed to this method. Accepts block which yields each array element.

Examples:

Create array in root JSON element

json.array!(@links) do |link|
  json.rel link.first
  json.href link.last
end

compiles to something like …

[
   {
     "rel": "self",
     "href": "http://example.com/people/123"
   },
   {
     "rel": "school",
     "href": "http://gatech.edu"
   }
]


131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/jsonify/builder.rb', line 131

def array!(args)
  __array
  args.each do |arg|
    @level += 1
    yield 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