Module: Sinatra::Extension

Defined in:
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/sinatra-contrib-3.0.5/lib/sinatra/extension.rb

Overview

Sinatra::Extension

Sinatra::Extension is a mixin that provides some syntactic sugar for your extensions. It allows you to call almost any Sinatra::Base method directly inside your extension module. This means you can use get to define a route, before to define a before filter, set to define a setting and so on.

Is important to be aware that this mixin remembers the method calls you make, and then, when your extension is registered, replays them on the Sinatra application that has been extended. In order to do that, it defines a registered method, so, if your extension defines one too, remember to call super.

Usage

Just require the mixin and extend your extension with it:

require 'sinatra/extension'

module MyExtension
  extend Sinatra::Extension

  # set some settings for development
  configure :development do
    set :reload_stuff, true
  end

  # define a route
  get '/' do
    'Hello World'
  end

  # The rest of your extension code goes here...
end

You can also create an extension with the new method:

MyExtension = Sinatra::Extension.new do
  # Your extension code goes here...
end

This is useful when you just want to pass a block to Sinatra::Base.register.

Defined Under Namespace

Classes: DontCall

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)



83
84
85
86
87
88
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/sinatra-contrib-3.0.5/lib/sinatra/extension.rb', line 83

def method_missing(method, *args, &block)
  return super unless Sinatra::Base.respond_to? method

  record(method, *args, &block)
  DontCall.new(method)
end

Class Method Details

.new(&block) ⇒ Object



51
52
53
54
55
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/sinatra-contrib-3.0.5/lib/sinatra/extension.rb', line 51

def self.new(&block)
  ext = Module.new.extend(self)
  ext.class_eval(&block)
  ext
end

Instance Method Details

#configure(*args, &block) ⇒ Object



61
62
63
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/sinatra-contrib-3.0.5/lib/sinatra/extension.rb', line 61

def configure(*args, &block)
  record(:configure, *args) { |c| c.instance_exec(c, &block) }
end

#registered(base = nil, &block) ⇒ Object



65
66
67
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/sinatra-contrib-3.0.5/lib/sinatra/extension.rb', line 65

def registered(base = nil, &block)
  base ? replay(base) : record(:class_eval, &block)
end

#settingsObject



57
58
59
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/sinatra-contrib-3.0.5/lib/sinatra/extension.rb', line 57

def settings
  self
end