Class: Wunderbar::TextBuilder

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) ⇒ TextBuilder

Returns a new instance of TextBuilder.



411
412
413
414
# File 'lib/wunderbar/builder.rb', line 411

def initialize(scope)
  @_target = StringIO.new
  @_scope = scope
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



430
431
432
433
434
435
436
437
438
439
440
# File 'lib/wunderbar/builder.rb', line 430

def method_missing(method, *args, &block)
  if 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
end

Instance Method Details

#_(*args) ⇒ Object



424
425
426
427
# File 'lib/wunderbar/builder.rb', line 424

def _(*args)
  @_target.puts(*args) if args.length > 0 
  self
end

#_exception(*args) ⇒ Object



442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
# File 'lib/wunderbar/builder.rb', line 442

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

#encode(&block) ⇒ Object



416
417
418
419
420
421
422
# File 'lib/wunderbar/builder.rb', line 416

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

#system(*args) ⇒ Object

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



459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
# File 'lib/wunderbar/builder.rb', line 459

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

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

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

#target!Object



477
478
479
# File 'lib/wunderbar/builder.rb', line 477

def target!
  @_target.string
end