Class: Vanilla::Renderers::Ruby

Inherits:
Base
  • Object
show all
Defined in:
lib/vanilla/renderers/ruby.rb

Overview

Snips that render_as “Ruby” should define a class. The class should have instance methods for any HTTP request methods that the dynasnip should respond to, i.e. get(), post(), and so on. Alternatively, it can respond to ‘handle’.

The result of the method invocation always has #to_s called on it. The last line of the content should be the name of that class, so that it is returned by “eval” and we can instantiate it. If the dynasnip needs access to the ‘context’ (i.e. probably the request itself), it should be a subclass of Dynasnip (or define an initializer that accepts the context as its first argument).

Instance Attribute Summary

Attributes inherited from Base

#app

Instance Method Summary collapse

Methods inherited from Base

escape_curly_braces, #include_snips, #initialize, #raw_content, render, #render, #render_without_including_snips, snip_regexp

Constructor Details

This class inherits a constructor from Vanilla::Renderers::Base

Instance Method Details

#prepare(snip, part = :content, args = []) ⇒ Object



16
17
18
19
# File 'lib/vanilla/renderers/ruby.rb', line 16

def prepare(snip, part=:content, args=[])
  @args = args
  @snip = snip
end

#process_text(content) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/vanilla/renderers/ruby.rb', line 21

def process_text(content)
  handler_klass = eval(content, binding, @snip.name)
  instance = if handler_klass.ancestors.include?(Vanilla::Renderers::Base)
    handler_klass.new(app)
  else
    handler_klass.new
  end
  if (method = app.request.method) && instance.respond_to?(method)
    instance.send(method, *@args).to_s
  else
    instance.handle(*@args).to_s
  end
end