Class: Babylon::Base::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/babylon/base/controller.rb

Overview

Your application’s controller should be descendant of this class.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stanza = nil) ⇒ Controller

Creates a new controller (you should not override this class) and assigns the stanza as well as any other value of the hash to instances named after the keys of the hash.



12
13
14
15
# File 'lib/babylon/base/controller.rb', line 12

def initialize(stanza = nil)
  @stanza = stanza
  @view = nil
end

Instance Attribute Details

#action_nameObject

Stanza received by the controller (Nokogiri::XML::Node)



8
9
10
# File 'lib/babylon/base/controller.rb', line 8

def action_name
  @action_name
end

#renderedObject

Stanza received by the controller (Nokogiri::XML::Node)



8
9
10
# File 'lib/babylon/base/controller.rb', line 8

def rendered
  @rendered
end

#stanzaObject

Stanza received by the controller (Nokogiri::XML::Node)



8
9
10
# File 'lib/babylon/base/controller.rb', line 8

def stanza
  @stanza
end

Instance Method Details

#assignsObject

Returns the list of variables assigned during the action.



33
34
35
36
37
38
39
40
41
# File 'lib/babylon/base/controller.rb', line 33

def assigns
  vars = Hash.new
  instance_variables.each do |var|
    if !["@view", "@action_name", "@block"].include? var
      vars[var[1..-1]] = instance_variable_get(var)
    end
  end
  vars
end

#evaluateObject

Actually evaluates the view



70
71
72
# File 'lib/babylon/base/controller.rb', line 70

def evaluate
  @view.evaluate if @view
end

#perform(action) ⇒ Object

Performs the action and calls back the optional block argument : you should not override this function



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/babylon/base/controller.rb', line 19

def perform(action)
  @action_name = action
  begin
    self.send(@action_name)
  rescue
    Babylon.logger.error {
      "#{$!}:\n#{$!.backtrace.join("\n")}"
    }
  end
  self.render
end

#render(options = {}) ⇒ Object

Called by default after each action to “build” a XMPP stanza. By default, it will use the /controller_name/action.xml.builder You can use the following options :

- :file : render a specific file (can be in a different controller)
- :action : render another action of the current controller
- :nothing : doesn't render anything


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/babylon/base/controller.rb', line 49

def render(options = {}) 
  return if @view and !options[:force] # Avoid double rendering, if we have already attached a view
  
  if options == {} # default rendering
    result = render(:file => default_template_name)
  elsif options[:file]
    file = options[:file]
    if file =~ /^\// # Render from view root
      result = render_for_file(File.join("app", "views", "#{file}.xml.builder"))
    else
      result = render_for_file(view_path(file)) 
    end
  elsif action_name = options[:action]
    result = render(:file => default_template_name(action_name.to_s))
  elsif options[:nothing]
    @view = Babylon::Base::View.new()
  end
end

#render_and_evaluate(options = {}) ⇒ Object



74
75
76
77
# File 'lib/babylon/base/controller.rb', line 74

def render_and_evaluate(options = {})
  render(options)
  evaluate
end

#render_evaluate_and_send(options) ⇒ Object



79
80
81
82
# File 'lib/babylon/base/controller.rb', line 79

def render_evaluate_and_send(options)
  response = render_and_evaluate(options)
  Babylon.router.connection.send_xml response 
end