Module: Maveric

Defined in:
lib/maveric.rb

Defined Under Namespace

Modules: Class, Helpers, Views Classes: Response

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.mavericsObject (readonly)

Returns the value of attribute maverics.



175
176
177
# File 'lib/maveric.rb', line 175

def maverics
  @maverics
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



23
24
25
# File 'lib/maveric.rb', line 23

def action
  @action
end

#requestObject (readonly)

Returns the value of attribute request.



23
24
25
# File 'lib/maveric.rb', line 23

def request
  @request
end

#routeObject (readonly)

Returns the value of attribute route.



23
24
25
# File 'lib/maveric.rb', line 23

def route
  @route
end

Class Method Details

.call(env) ⇒ Object

Maps to a call to the result of Maveric.urlmap



200
201
202
# File 'lib/maveric.rb', line 200

def call env
  self.urlmap.call env
end

.extend_object(obj) ⇒ Object



232
233
234
235
# File 'lib/maveric.rb', line 232

def extend_object obj
  warn 'Maveric should only be included.'
  return obj
end

.included(obj) ⇒ Object

When a maveric is subclassed, the superclass’ headers inherited. Views and Helpers modules are also included into the subclass’ corresponding modules.

By default, the first class to inherit from Maveric is assigned the mount point of ‘/’.

A maveric’s default mount point is by default derived from it’s full name. For example: Object would be ‘/object’, Module would be ‘/module’, and Rack::Auth::OpenID would be ‘/rack/auth/openid’.



220
221
222
223
224
225
226
227
228
229
230
# File 'lib/maveric.rb', line 220

def included obj
  super
  obj.extend Maveric::Class
  %w'Helpers Views'.each do |name|
    next if obj.const_defined?(name)
    obj.const_set(name, Module.new).
      instance_eval{ include Maveric.const_get(name) }
  end
  obj.mount = '/' if @maverics.empty?
  @maverics << obj
end

.inspectObject



177
178
179
# File 'lib/maveric.rb', line 177

def inspect
  "Maveric#{maverics.inspect}"
end

.mapObject

Generates a Rack::URLMap compatible hash composed of all maverics and their mount points. Will be regenerated if the list of maverics have changed.



184
185
186
187
188
189
190
191
# File 'lib/maveric.rb', line 184

def map
  return @map if @map and @last_hash == @maverics.hash
  @last_hash = @maverics.hash
  @map = @maverics.inject({}) do |h,m|
    h.store m.mount, m
    h
  end
end

.new(app = nil) ⇒ Object

Returns itself, or a Rack::Cascade when provided an app



205
206
207
208
# File 'lib/maveric.rb', line 205

def new app=nil
  return self unless app
  Rack::Cascade.new [app, self]
end

.urlmapObject

Generates a Rack::URLMap from the result of Maveric.map



194
195
196
197
# File 'lib/maveric.rb', line 194

def urlmap
  return @urlmap if @urlmap and @last_hash == @maverics.hash
  @urlmap = Rack::URLMap.new self.map
end

Instance Method Details

#initialize(env) ⇒ Object

The current instance is stored in the passed environment under the key ‘maveric’. A Rack::Request of the current request is provided at @request.



15
16
17
18
19
20
21
22
# File 'lib/maveric.rb', line 15

def initialize env
  env['maveric'] = self
  @request = Rack::Request.new env
  @route, @action = self.class.routes.empty? ?
    [nil, :index] : self.class.routes.find{|(r,d)| r === env['PATH_INFO'] }
  extend self.class::Helpers
  self.init_hook if self.respond_to? :init_hook
end

#response(renew = false) ⇒ Object

Will return a 404 type Response if the set action is invalid. Will generate a new response if a previous one is not cached or renew is true.

If the response responds to :finish, it will return the result, otherwise the response itself will be returned.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/maveric.rb', line 31

def response renew=false
  raise Response, 404 unless @action and respond_to? @action
  @response = __send__ @action if not @response or renew
rescue Response
  warn('Response: '+$!.inspect)
  @response = $!.to_a
rescue
  warn('Rescue: '+$!.inspect)
  body = $!.message + "\n\t" + $@[0..10]*"\n\t"
  @request.env['rack.errors'].puts body
  body << "\n\t" << $@[11..-1]*"\n\t"
  @request.env.sort.each{|(k,v)| body << "\n#{k}\n\t#{v.inspect}" }
  @response = [ 500, {
    'Content-Type' => 'text/plain',
    'Content-Length' => body.length.to_s
  }, body.to_a ]
end