Class: Campo::Outputter

Inherits:
Object
  • Object
show all
Defined in:
lib/campo/campo.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{n: 0, tab: 2}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tab = nil, &block) ⇒ Outputter

Returns a new instance of Outputter.



403
404
405
406
# File 'lib/campo/campo.rb', line 403

def initialize( tab=nil, &block )
  options[:tab] = tab unless tab.nil? 
  instance_eval( &block ) if block
end

Instance Attribute Details

#outputObject

Returns the value of attribute output.



408
409
410
# File 'lib/campo/campo.rb', line 408

def output
  @output
end

Instance Method Details

#after_output(&block) ⇒ Object



381
382
383
# File 'lib/campo/campo.rb', line 381

def after_output( &block )
  afters << block
end

#aftersObject



391
392
393
# File 'lib/campo/campo.rb', line 391

def afters
  @afters ||= check_for_plugins :afters
end

#before_output(&block) ⇒ Object



376
377
378
# File 'lib/campo/campo.rb', line 376

def before_output( &block )
  befores << block
end

#beforesObject



386
387
388
# File 'lib/campo/campo.rb', line 386

def befores
  @befores ||= check_for_plugins :befores
end

#check_for_plugins(type) ⇒ Object



396
397
398
399
400
# File 'lib/campo/campo.rb', line 396

def check_for_plugins( type )
  Campo.plugins.reduce [] do |mem, (_,plugin)|
    mem + plugin.send(:"#{type}" )
  end
end

#optionsObject



413
414
415
# File 'lib/campo/campo.rb', line 413

def options
  @options ||= DEFAULT_OPTIONS
end

#run(fields, opts = {}) ⇒ Object



418
419
420
421
422
423
424
425
426
427
# File 'lib/campo/campo.rb', line 418

def run( fields, opts={} )
  opts = options.merge opts
  tab = opts.delete(:tab) || @tab
  
  output = ""
  befores.each{|f| instance_exec( fields, options, &f ) } 
  output = Base.output( fields, output, options[:n], tab )
  output = afters.reduce(output){|mem,obj| instance_exec mem, opts, &obj }
  output
end