Class: Wunderbar::JsonBuilder

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

Instance Method Summary collapse

Methods inherited from BuilderClass

#websocket

Methods inherited from BuilderBase

#get_binding, #set_variables_from_params

Constructor Details

#initialize(scope) ⇒ JsonBuilder

Returns a new instance of JsonBuilder.



483
484
485
486
# File 'lib/wunderbar/builder.rb', line 483

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



497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
# File 'lib/wunderbar/builder.rb', line 497

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 = []
      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



563
564
565
# File 'lib/wunderbar/builder.rb', line 563

def _!(object)
  @_target = object
end

#_exception(*args) ⇒ Object



567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
# File 'lib/wunderbar/builder.rb', line 567

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



488
489
490
491
492
493
494
# File 'lib/wunderbar/builder.rb', line 488

def encode(&block)
  set_variables_from_params
  before = @_target.dup
  result = self.instance_eval(&block)
  _! result if before.empty? and result and @_target == before
  @_target
end

#system(*args) ⇒ Object

execute a system command, echoing stdin, stdout, and stderr



584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
# File 'lib/wunderbar/builder.rb', line 584

def system(*args)
  opts = {}
  opts = args.pop if Hash === args.last

  transcript = opts[:transcript]  || 'transcript'
  output_prefix = opts[:prefix] || {}
  output_prefix[:stdin]  ||= '$ '

  if @_target[transcript]
    @_target[transcript] << ''
  else
    @_target[transcript] = []
  end

  if Hash === args.last # support original code which needed two hashes
    super do |kind, line|
      @_target[transcript] << "#{output_prefix[kind]}#{line}"
    end
  else
    super(*args, opts) do |kind, line|
      @_target[transcript] << "#{output_prefix[kind]}#{line}"
    end
  end
end

#system!(*args) ⇒ Object

execute a system command, ensuring the result is a success

Raises:

  • (RuntimeError)


610
611
612
613
614
615
616
# File 'lib/wunderbar/builder.rb', line 610

def system!(*args)
  rc = system(args)
  
  raise RuntimeError.new("exit code: #{rc}") if rc != 0

  rc
end

#target!Object



618
619
620
621
622
623
624
# File 'lib/wunderbar/builder.rb', line 618

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

#target?(type = nil) ⇒ Boolean

Returns:

  • (Boolean)


626
627
628
629
630
631
632
# File 'lib/wunderbar/builder.rb', line 626

def target?(type=nil)
  if Class === type
    type === @_target
  else
    @_target
  end
end