Class: Wunderbar::JsonBuilder

Inherits:
BuilderBase show all
Defined in:
lib/wunderbar/builder.rb

Instance Method Summary collapse

Methods inherited from BuilderBase

#get_binding, #set_variables_from_params

Constructor Details

#initialize(scope) ⇒ JsonBuilder

Returns a new instance of JsonBuilder.



292
293
294
295
# File 'lib/wunderbar/builder.rb', line 292

def initialize(scope)
  @_scope = scope
  @_target = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

forward to Wunderbar, @_target, or @_scope



304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# File 'lib/wunderbar/builder.rb', line 304

def method_missing(method, *args, &block)

  if method.to_s =~ /^_(\w*)$/
    name = $1
  elsif Wunderbar.respond_to? method
    return Wunderbar.send method, *args, &block
  elsif @_target.respond_to? method
    return @_target.send method, *args, &block
  elsif @_scope and @_scope.respond_to? method
    return @_scope.send method, *args, &block
  else
    super
  end

  if args.length == 0
    return self unless block
    result = JsonBuilder.new(@_scope).encode(&block)
  elsif args.length == 1
    result = args.first

    if block
      if Symbol === result or String === result
        result = {result.to_s => JsonBuilder.new(@_scope).encode(&block)}
      else
        result = result.map {|n| @_target = {}; block.call(n); @_target} 
      end
    end
  elsif block
    ::Kernel::raise ::ArgumentError, 
      "can't mix multiple arguments with a block"
  else
    object = args.shift

    if not Enumerable === object or String === object or Struct === object
      result = {}
      args.each {|arg| result[arg.to_s] = object.send arg}
    else
      result = object.map do |item|
        args.inject({}) {|hash, arg| hash[arg.to_s] = item.send arg; hash}
      end
    end
  end

  if name != ''
    unless Hash === @_target or @_target.empty?
      ::Kernel::raise ::ArgumentError, "mixed array and hash calls"
    end

    @_target[name.to_s] = result
  elsif args.length == 0 or (args.length == 1 and not block)
    @_target = [] if @_target == {}

    if Hash === @_target 
      ::Kernel::raise ::ArgumentError, "mixed hash and array calls"
    end

    @_target << result
  else
    @_target = result
  end

  self
end

Instance Method Details

#_!(object) ⇒ Object



368
369
370
# File 'lib/wunderbar/builder.rb', line 368

def _!(object)
  @_target = object
end

#_exception(*args) ⇒ Object



372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# File 'lib/wunderbar/builder.rb', line 372

def _exception(*args)
  exception = args.first
  if exception.respond_to? :backtrace
    Wunderbar.error exception.inspect
    super(exception.inspect)
    @_target['backtrace'] = []
    exception.backtrace.each do |frame| 
      next if CALLERS_TO_IGNORE.any? {|re| frame =~ re}
      Wunderbar.warn "  #{frame}"
      @_target['backtrace'] << frame 
    end
  else
    super
  end
end

#encode(&block) ⇒ Object



297
298
299
300
301
# File 'lib/wunderbar/builder.rb', line 297

def encode(&block)
  set_variables_from_params
  self.instance_eval(&block)
  @_target
end

#target!Object



388
389
390
391
392
393
394
# File 'lib/wunderbar/builder.rb', line 388

def target!
  begin
    JSON.pretty_generate(@_target)+ "\n"
  rescue
    @_target.to_json + "\n"
  end
end