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.



91
92
93
# File 'lib/roda.rb', line 91

def app
  @app
end

#optsObject (readonly)

The settings/options hash for the current class.



94
95
96
# File 'lib/roda.rb', line 94

def opts
  @opts
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.



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

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

#inherited(subclass) ⇒ Object

When inheriting Roda, setup a new rack app builder, copy the default middleware and opts into the subclass, and set the request and response classes in the subclasses to be subclasses of the request and responses classes in the parent class. This makes it so child classes inherit plugins from their parent, but using plugins in child classes does not affect the parent.



110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/roda.rb', line 110

def inherited(subclass)
  super
  subclass.instance_variable_set(:@builder, ::Rack::Builder.new)
  subclass.instance_variable_set(:@middleware, @middleware.dup)
  subclass.instance_variable_set(:@opts, opts.dup)
  
  request_class = Class.new(self::RodaRequest)
  request_class.roda_class = subclass
  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(mixin, *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.



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/roda.rb', line 128

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

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

  if defined?(mixin::InstanceMethods)
    include mixin::InstanceMethods
  end
  if defined?(mixin::ClassMethods)
    extend mixin::ClassMethods
  end
  if defined?(mixin::RequestMethods)
    self::RodaRequest.send(:include, mixin::RequestMethods)
  end
  if defined?(mixin::ResponseMethods)
    self::RodaResponse.send(:include, mixin::ResponseMethods)
  end
  
  if mixin.respond_to?(:configure)
    mixin.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.



158
159
160
# File 'lib/roda.rb', line 158

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.



165
166
167
# File 'lib/roda.rb', line 165

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

#route(&block) ⇒ Object

Setup route definitions for the current class, and build the rack application using the stored middleware.



171
172
173
174
175
# File 'lib/roda.rb', line 171

def route(&block)
  @middleware.each{|a, b| @builder.use(*a, &b)}
  @builder.run lambda{|env| new.call(env, &block)}
  @app = @builder.to_app
end

#use(*args, &block) ⇒ Object

Add a middleware to use for the rack application. Must be called before calling #route.



179
180
181
# File 'lib/roda.rb', line 179

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