Class: Vanilla::App

Inherits:
Object
  • Object
show all
Includes:
Routing
Defined in:
lib/vanilla/app.rb

Overview

This is the main App class for Vanilla applications; this should be subclassed for each instance of Vanilla that you want to run.

Defined Under Namespace

Classes: NotFound

Constant Summary

Constants included from Routing

Routing::ROOT, Routing::SNIP, Routing::SNIP_AND_PART

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Routing

parse, #url_to

Constructor Details

#initializeApp

Returns a new instance of App.



28
29
30
31
32
# File 'lib/vanilla/app.rb', line 28

def initialize
  @renderers = Hash.new { config.default_renderer }
  @soup = config.soup || prepare_soup
  prepare_renderers
end

Class Attribute Details

.configObject (readonly)

Returns the value of attribute config.



12
13
14
# File 'lib/vanilla/app.rb', line 12

def config
  @config
end

Instance Attribute Details

#requestObject (readonly)

Returns the value of attribute request.



26
27
28
# File 'lib/vanilla/app.rb', line 26

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.



26
27
28
# File 'lib/vanilla/app.rb', line 26

def response
  @response
end

#soupObject (readonly)

Returns the value of attribute soup.



26
27
28
# File 'lib/vanilla/app.rb', line 26

def soup
  @soup
end

Class Method Details

.configure {|@config| ... } ⇒ Object

Yields:



13
14
15
16
17
# File 'lib/vanilla/app.rb', line 13

def configure(&block)
  reset! unless @config
  yield @config
  self
end

.inherited(app) ⇒ Object



21
22
23
# File 'lib/vanilla/app.rb', line 21

def inherited(app)
  Vanilla.apps << app
end

.reset!Object



18
19
20
# File 'lib/vanilla/app.rb', line 18

def reset!
  @config = Vanilla::Config.new
end

Instance Method Details

#atom_feed(options = {}) ⇒ Object



102
103
104
# File 'lib/vanilla/app.rb', line 102

def atom_feed(options={})
  AtomFeed.new(options.merge(:app => self))
end

#call(env) ⇒ Object

Returns a Rack-appropriate 3-element array (via Rack::Response#finish)



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/vanilla/app.rb', line 39

def call(env)
  env['vanilla.app'] = self
  @request = Vanilla::Request.new(env, self)
  @response = Rack::Response.new

  begin
    output = render_in_format(request.snip, request.part, request.format)
  rescue NotFound => e
    @response.status = 404
    output = e.to_s
  rescue => e
    raise e if config.raise_errors
    @response.status = 500
    output = e.to_s + e.backtrace.join("\n")
  end
  response_format = request.format
  response_format = 'plain' if response_format == 'raw'
  @response['Content-Type'] = "text/#{response_format}"
  output = "" if @request.method == "head"
  @response.write(output)
  @response.finish # returns the array
end

#configObject



34
35
36
# File 'lib/vanilla/app.rb', line 34

def config
  self.class.config
end

#default_layout_snipObject



89
90
91
# File 'lib/vanilla/app.rb', line 89

def default_layout_snip
  soup[config.default_layout_snip]
end

#register_renderer(klass, *types) ⇒ Object



93
94
95
96
97
98
99
100
# File 'lib/vanilla/app.rb', line 93

def register_renderer(klass, *types)
  types.each do |type|
    if klass.is_a?(String)
      klass = klass.split("::").inject(Object) { |o, name| o.const_get(name) }
    end
    @renderers[type.to_s] = klass
  end
end

#render(snip, part = :content, args = [], enclosing_snip = snip) ⇒ Object

render a snip using either the renderer given, or the renderer specified by the snip’s “render_as” property, or Render::Base if nothing else is given.

This method can be useful if a dynasnip or other part of the system needs to get a fully rendered version of a snip.



68
69
70
71
72
# File 'lib/vanilla/app.rb', line 68

def render(snip, part=:content, args=[], enclosing_snip=snip)
  rendering_and_handling_errors(snip) do |renderer|
    renderer.render(snip, part, args, enclosing_snip)
  end
end

#renderer_for(snip) ⇒ Object

Returns the renderer class for a given snip



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/vanilla/app.rb', line 75

def renderer_for(snip)
  if snip
    renderer_name = snip.render_as || snip.extension
    renderer_name = nil if renderer_name == ''
  else
    renderer_name = nil
  end
  if renderer_name
    find_renderer(renderer_name)
  else
    config.default_renderer
  end
end