Module: Ruter::Plugin

Included in:
Ruter
Defined in:
lib/ruter/plugin.rb

Overview

Public: Ruter’s plugin system.

Instance Method Summary collapse

Instance Method Details

#plugin(plugin, *args, &block) ⇒ Object

Public: Loads given plugin into the application.

Examples

require "ruter"
require "ruter/protection"
require "ruter/session"

# Use some standard plugins

Ruter.plugin(Ruter::Protection)
Ruter.plugin(Ruter::Session, secret: "__a_random_secret_key__")

# Make your own custom plugin:

require "redcarpet"

class MarkdownPlugin
  def self.setup(app, options = {})
    app.settings[:markdown_options] = options
  end

  module ClassMethods
    def markdown_renderer
      @_markdown_renderer ||= Redcarpet::Markdown.new(Redcarpet::Render::HTML, markdown_options)
    end

    def markdown_options
      settings[:markdown_options]
    end
  end

  module InstanceMethods
    def markdown(text)
      res.write(markdown_renderer.render(text))
    end
  end
end

Ruter.plugin(MarkdownPlugin, fenced_code_blocks: true)

Ruter.define do
  root do
    get do
      markdown("This is *bongos*, indeed.")
    end
  end
end


53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/ruter/plugin.rb', line 53

def plugin(plugin, *args, &block)
  if defined?(plugin::InstanceMethods)
    include(plugin::InstanceMethods)
  end

  if defined?(plugin::ClassMethods)
    extend(plugin::ClassMethods)
  end

  if plugin.respond_to?(:setup)
    plugin.setup(self, *args, &block)
  end
end