Module: Roda::RodaPlugins::Base::ClassMethods

Included in:
Roda
Defined in:
lib/roda.rb

Overview

Class methods for the Roda class.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#appObject (readonly)

The rack application that this class uses.



96
97
98
# File 'lib/roda.rb', line 96

def app
  @app
end

#inherit_middlewareObject

Whether middleware from the current class should be inherited by subclasses. True by default, should be set to false when using a design where the parent class accepts requests and uses run to dispatch the request to a subclass.



101
102
103
# File 'lib/roda.rb', line 101

def inherit_middleware
  @inherit_middleware
end

#optsObject (readonly)

The settings/options hash for the current class.



104
105
106
# File 'lib/roda.rb', line 104

def opts
  @opts
end

#route_blockObject (readonly)

The route block that this class uses.



107
108
109
# File 'lib/roda.rb', line 107

def route_block
  @route_block
end

Instance Method Details

#call(env) ⇒ Object

Call the internal rack application with the given environment. This allows the class itself to be used as a rack application. However, for performance, it’s better to use #app to get direct access to the underlying rack app.



113
114
115
# File 'lib/roda.rb', line 113

def call(env)
  app.call(env)
end

#clear_middleware!Object

Clear the middleware stack



118
119
120
121
# File 'lib/roda.rb', line 118

def clear_middleware!
  @middleware.clear
  build_rack_app
end

#hash_matcher(key, &block) ⇒ Object

Create a match_#key method in the request class using the given block, so that using a hash key in a request match method will call the block. The block should return nil or false to not match, and anything else to match.

class App < Roda
  hash_matcher(:foo) do |v|
    self['foo'] == v
  end

  route do
    r.on :foo=>'bar' do
      # matches when param foo has value bar
    end
  end
end


139
140
141
# File 'lib/roda.rb', line 139

def hash_matcher(key, &block)
  request_module{define_method(:"match_#{key}", &block)}
end

#inherited(subclass) ⇒ Object

When inheriting Roda, copy the shared data into the subclass, and setup the request and response subclasses.



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/roda.rb', line 145

def inherited(subclass)
  super
  subclass.instance_variable_set(:@inherit_middleware, @inherit_middleware)
  subclass.instance_variable_set(:@middleware, @inherit_middleware ? @middleware.dup : [])
  subclass.instance_variable_set(:@opts, opts.dup)
  subclass.opts.to_a.each do |k,v|
    if (v.is_a?(Array) || v.is_a?(Hash)) && !v.frozen?
      subclass.opts[k] = v.dup
    end
  end
  subclass.instance_variable_set(:@route_block, @route_block)
  subclass.send(:build_rack_app)
  
  request_class = Class.new(self::RodaRequest)
  request_class.roda_class = subclass
  request_class.match_pattern_cache = thread_safe_cache
  subclass.const_set(:RodaRequest, request_class)

  response_class = Class.new(self::RodaResponse)
  response_class.roda_class = subclass
  subclass.const_set(:RodaResponse, response_class)
end

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

Load a new plugin into the current class. A plugin can be a module which is used directly, or a symbol represented a registered plugin which will be required and then used.

Roda.plugin PluginModule
Roda.plugin :csrf


174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/roda.rb', line 174

def plugin(plugin, *args, &block)
  if plugin.is_a?(Symbol)
    plugin = RodaPlugins.load_plugin(plugin)
  end

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

  if defined?(plugin::InstanceMethods)
    include(plugin::InstanceMethods)
  end
  if defined?(plugin::ClassMethods)
    extend(plugin::ClassMethods)
  end
  if defined?(plugin::RequestMethods)
    self::RodaRequest.send(:include, plugin::RequestMethods)
  end
  if defined?(plugin::RequestClassMethods)
    self::RodaRequest.extend(plugin::RequestClassMethods)
  end
  if defined?(plugin::ResponseMethods)
    self::RodaResponse.send(:include, plugin::ResponseMethods)
  end
  if defined?(plugin::ResponseClassMethods)
    self::RodaResponse.extend(plugin::ResponseClassMethods)
  end
  
  if plugin.respond_to?(:configure)
    plugin.configure(self, *args, &block)
  end
end

#request_module(mod = nil, &block) ⇒ Object

Include the given module in the request class. If a block is provided instead of a module, create a module using the the block. Example:

Roda.request_module SomeModule

Roda.request_module do
  def description
    "#{request_method} #{path_info}"
  end
end

Roda.route do |r|
  r.description
end


222
223
224
# File 'lib/roda.rb', line 222

def request_module(mod = nil, &block)
  module_include(:request, mod, &block)
end

#response_module(mod = nil, &block) ⇒ Object

Include the given module in the response class. If a block is provided instead of a module, create a module using the the block. Example:

Roda.response_module SomeModule

Roda.response_module do
  def error!
    self.status = 500
  end
end

Roda.route do |r|
  response.error!
end


241
242
243
# File 'lib/roda.rb', line 241

def response_module(mod = nil, &block)
  module_include(:response, mod, &block)
end

#route(&block) ⇒ Object

Setup routing tree for the current Roda application, and build the underlying rack application using the stored middleware. Requires a block, which is yielded the request. By convention, the block argument should be named r. Example:

Roda.route do |r|
  r.root do
    "Root"
  end
end

This should only be called once per class, and if called multiple times will overwrite the previous routing.



258
259
260
261
# File 'lib/roda.rb', line 258

def route(&block)
  @route_block = block
  build_rack_app
end

#thread_safe_cacheObject

A new thread safe cache instance. This is a method so it can be easily overridden for alternative implementations.



265
266
267
# File 'lib/roda.rb', line 265

def thread_safe_cache
  RodaCache.new
end

#use(*args, &block) ⇒ Object

Add a middleware to use for the rack application. Must be called before calling #route to have an effect. Example:

Roda.use Rack::Session::Cookie, :secret=>ENV['secret']


273
274
275
276
# File 'lib/roda.rb', line 273

def use(*args, &block)
  @middleware << [args, block]
  build_rack_app
end