Class: Wunderbar::JsonBuilder
- Inherits:
-
BuilderClass
- Object
- BuilderBase
- BuilderClass
- Wunderbar::JsonBuilder
- Defined in:
- lib/wunderbar/builder.rb
Instance Method Summary collapse
- #_!(object) ⇒ Object
- #_exception(*args) ⇒ Object
- #encode(&block) ⇒ Object
-
#initialize(scope) ⇒ JsonBuilder
constructor
A new instance of JsonBuilder.
-
#method_missing(method, *args, &block) ⇒ Object
forward to Wunderbar, @_target, or @_scope.
- #target! ⇒ Object
Methods inherited from BuilderClass
Methods inherited from BuilderBase
#get_binding, #set_variables_from_params
Constructor Details
#initialize(scope) ⇒ JsonBuilder
408 409 410 411 |
# File 'lib/wunderbar/builder.rb', line 408 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
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 |
# File 'lib/wunderbar/builder.rb', line 420 def method_missing(method, *args, &block) if method.to_s =~ /^_(\w*)$/ name = $1 elsif .respond_to? method return .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 = [] result = @_target if name.empty? and @_target.respond_to? :<< object.each do |item| result << Hash[args.map {|arg| [arg.to_s, item.send(arg)]}] 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
486 487 488 |
# File 'lib/wunderbar/builder.rb', line 486 def _!(object) @_target = object end |
#_exception(*args) ⇒ Object
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 |
# File 'lib/wunderbar/builder.rb', line 490 def _exception(*args) exception = args.first if exception.respond_to? :backtrace .error exception.inspect super(exception.inspect) @_target['backtrace'] = [] exception.backtrace.each do |frame| next if CALLERS_TO_IGNORE.any? {|re| frame =~ re} .warn " #{frame}" @_target['backtrace'] << frame end else super end end |
#encode(&block) ⇒ Object
413 414 415 416 417 |
# File 'lib/wunderbar/builder.rb', line 413 def encode(&block) set_variables_from_params self.instance_eval(&block) @_target end |
#target! ⇒ Object
506 507 508 509 510 511 512 |
# File 'lib/wunderbar/builder.rb', line 506 def target! begin JSON.pretty_generate(@_target)+ "\n" rescue @_target.to_json + "\n" end end |