Class: Vanilla::App

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_file = nil) ⇒ App

Returns a new instance of App.



9
10
11
12
13
# File 'lib/vanilla/app.rb', line 9

def initialize(config_file=nil)
  prepare_configuration(config_file)
  Soup.base = YAML.load(File.read('config/database.yml'))['production']
  Soup.prepare
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



7
8
9
# File 'lib/vanilla/app.rb', line 7

def config
  @config
end

#requestObject (readonly)

Returns the value of attribute request.



7
8
9
# File 'lib/vanilla/app.rb', line 7

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.



7
8
9
# File 'lib/vanilla/app.rb', line 7

def response
  @response
end

Class Method Details

.save!Object



83
84
85
# File 'lib/vanilla/app.rb', line 83

def @config.save!
  File.open(self[:filename], 'w') { |f| f.puts self.to_yaml }
end

Instance Method Details

#call(env) ⇒ Object

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



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/vanilla/app.rb', line 16

def call(env)
  @request = Vanilla::Request.new(env)
  @response = Rack::Response.new

  begin
    output = formatted_render(request.snip, request.part, request.format)
  rescue => e
    @response.status = 500
    output = e.to_s
  end
  response_format = request.format      
  response_format = 'plain' if response_format == 'raw'
  @response['Content-Type'] = "text/#{response_format}"
  @response.write(output)
  @response.finish # returns the array
end

#formatted_render(snip, part = nil, format = nil) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/vanilla/app.rb', line 33

def formatted_render(snip, part=nil, format=nil)
  case format
  when 'html', nil
    Renderers::Erb.new(self).render(Vanilla.snip('system'), :main_template)
  when 'raw', 'css', 'js'
    Renderers::Raw.new(self).render(snip, part || :content)
  when 'text', 'atom', 'xml'
    render(snip, part || :content)
  else
    raise "Unknown format '#{format}'"
  end
end

#render(snip, part = :content, args = []) ⇒ 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.



49
50
51
52
53
# File 'lib/vanilla/app.rb', line 49

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

#render_missing_snip(snip_name) ⇒ Object

Other things can call this when a snip cannot be loaded.



73
74
75
# File 'lib/vanilla/app.rb', line 73

def render_missing_snip(snip_name)
  "[snip '#{snip_name}' cannot be found]"
end

#renderer_for(snip) ⇒ Object

Returns the renderer class for a given snip



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

def renderer_for(snip)
  return Renderers::Base unless snip.render_as && !snip.render_as.empty?
  Vanilla::Renderers.const_get(snip.render_as)
end

#rendering(snip) ⇒ Object

Given the snip and parameters, yield an instance of the appropriate Vanilla::Render::Base subclass



57
58
59
60
61
62
63
64
# File 'lib/vanilla/app.rb', line 57

def rendering(snip)
  renderer_instance = renderer_for(snip).new(self)
  yield renderer_instance
rescue Exception => e
  "<pre>[Error rendering '#{snip.name}' - \"" + 
    e.message.gsub("<", "&lt;").gsub(">", "&gt;") + "\"]\n" + 
    e.backtrace.join("\n").gsub("<", "&lt;").gsub(">", "&gt;") + "</pre>"
end