class Raconteur::Processor

Constants

ATTRS
DEFAULTS

Public Class Methods

all() click to toggle source

return array of all processors

# File lib/raconteur/processor.rb, line 40
def self.all
  @@raconteur.send(:data)[:processors]
end
deregister!(tag) click to toggle source

delete existing processor by tag name

# File lib/raconteur/processor.rb, line 21
def self.deregister!(tag)
  all.delete(find(tag))
  self
end
find(tag) click to toggle source

find processor by tag name

# File lib/raconteur/processor.rb, line 45
def self.find(tag)
  all.detect { |processor| processor.tag == tag }
end
inspect() click to toggle source

print array

# File lib/raconteur/processor.rb, line 50
def self.inspect
  "#{all} (Raconteur::Processor array)"
end
method_missing(method_sym, *arguments, &block) click to toggle source

treat class as array

# File lib/raconteur/processor.rb, line 55
def self.method_missing(method_sym, *arguments, &block)
  if !arguments.empty? && block_given?
    all.send(method_sym, *arguments, &block)
  elsif !arguments.empty?
    all.send(method_sym, *arguments)
  elsif block_given?
    all.send(method_sym, &block)
  else
    all.send(method_sym)
  end
end
new(tag, customizations={}) click to toggle source

new processor

# File lib/raconteur/processor.rb, line 71
def initialize(tag, customizations={})
  @data = Marshal.load(Marshal.dump(DEFAULTS))
  @processor = self
  @processor.tag = tag
  @processor.template = customizations[:template] if customizations[:template].is_a?(String)
  @processor.handler = customizations[:handler] if customizations[:handler].is_a?(Proc)
  @processor.payload = customizations[:payload] if customizations.has_key?(:payload)
  @processor
end
register!(tag, customizations={}) click to toggle source

register new processor by providing a tag name + any payload (optional)

# File lib/raconteur/processor.rb, line 11
def self.register!(tag, customizations={})
  if find(tag)
    raise 'Processor already exists!'
  else
    all << Raconteur::Processor.new(tag, customizations)
  end
  self
end
scoped(raconteur) click to toggle source

scoped

# File lib/raconteur/processor.rb, line 34
def self.scoped(raconteur)
  @@raconteur = raconteur
  self
end
update!(tag, customizations={}) click to toggle source

update existing processor by providing its tag name and passing in any customizations (optional)

# File lib/raconteur/processor.rb, line 27
def self.update!(tag, customizations={})
  deregister!(tag)
  register!(tag, customizations)
  self
end

Public Instance Methods

execute(content="", settings={}) click to toggle source

execute the processor

# File lib/raconteur/processor.rb, line 102
def execute(content="", settings={})
  output = content
  if self.handler
    # if the processor has a custom handler, then pass everything to it for processing
    output = self.handler.call(settings)
    if output.is_a?(Hash) && self.template
      # if the handler returns a hash and has a template, then pass the hash as options to the template
      # (this allows for custom variable setting and overriding before the template is rendered)
      output = Raconteur::Parse.render_template(self.template, output)
    end
  elsif self.template
    # if there's no handler but there is a processor, simply render the template with the tag's settings
    output = Raconteur::Parse.render_template(self.template, settings)
  end
  output
end
inspect() click to toggle source

prettier print

# File lib/raconteur/processor.rb, line 82
def inspect
  "#<Raconteur::Processor:0x#{object_id} #{ATTRS.map { |att| "@#{att}=#{@processor.send(att)}" }.join(', ')}>"
end
regex() click to toggle source

regex for matching tag and its settings

# File lib/raconteur/processor.rb, line 97
def regex
  /^\s*#{Regexp.quote(@processor.tag)}(:(?<settings>.*?))?\s*$/im
end