Method: Jsonify::Builder#method_missing
- Defined in:
- lib/jsonify/builder.rb
#method_missing(sym, args = nil, &block) ⇒ Object
Adds a new JsonPair to the builder where the key of the pair is set to the method name (sym). When passed a block, the value of the pair is set to the result of that block; otherwise, the value is set to the argument(s) (args).
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 the JsonArray.
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/jsonify/builder.rb', line 192 def method_missing(sym, args=nil, &block) # When no block given, simply add the symbol and arg as key - value for a JsonPair to current return __store( sym, args ) unless block # In a block; create a JSON pair (with no value) and add it to the current object pair = Generate.pair_value(sym) __store pair # Now process the block @level += 1 if args.nil? block.call else array!(args, &block) end # Set the value on the pair to the object at the top of the stack pair.value = @stack[@level] # Pop current off the top of the stack; we are done with it at this point @stack.pop @level -= 1 end |