Class: Vanilla::App

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Routes

#edit_link, #existing_link, #link_to, #new_link, #url_to, #url_to_raw

Constructor Details

#initialize(config_file = nil) ⇒ App

Returns a new instance of App.



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

def initialize(config_file=nil)
  prepare_configuration(config_file)
  @soup = SoupWithTimestamps.new(config[:soup])
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
end

#requestObject (readonly)

Returns the value of attribute request.



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

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.



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

def response
  @response
end

#soupObject (readonly)

Returns the value of attribute soup.



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

def soup
  @soup
end

Class Method Details

.save!Object



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

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)



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/vanilla/app.rb', line 23

def call(env)
  @request = Vanilla::Request.new(env, self)
  @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



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/vanilla/app.rb', line 40

def formatted_render(snip, part=nil, format=nil)
  case format
  when 'html', nil
    Renderers::Erb.new(self).render(soup['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 = [], 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.



56
57
58
59
60
# File 'lib/vanilla/app.rb', line 56

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

#render_missing_snip(snip_name) ⇒ Object

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



80
81
82
# File 'lib/vanilla/app.rb', line 80

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



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

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



64
65
66
67
68
69
70
71
# File 'lib/vanilla/app.rb', line 64

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

#snip(attributes) ⇒ Object



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

def snip(attributes)
  @soup.new_snip(attributes)
end