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.



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

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.



400
401
402
# File 'lib/campo/campo.rb', line 400

def output
  @output
end

Instance Method Details

#after_output(&block) ⇒ Object



373
374
375
# File 'lib/campo/campo.rb', line 373

def after_output( &block )
  afters << block
end

#aftersObject



383
384
385
# File 'lib/campo/campo.rb', line 383

def afters
  @afters ||= check_for_plugins :afters
end

#before_output(&block) ⇒ Object



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

def before_output( &block )
  befores << block
end

#beforesObject



378
379
380
# File 'lib/campo/campo.rb', line 378

def befores
  @befores ||= check_for_plugins :befores
end

#check_for_plugins(type) ⇒ Object



388
389
390
391
392
# File 'lib/campo/campo.rb', line 388

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

#optionsObject



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

def options
  @options ||= DEFAULT_OPTIONS
end

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



410
411
412
413
414
415
416
417
418
419
# File 'lib/campo/campo.rb', line 410

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