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
16
17
18
# File 'lib/babylon/base/controller.rb', line 12

def initialize(stanza = nil)
  @stanza = stanza
  @view = nil
  
  # used to render basic strings
  @content = 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.



36
37
38
39
40
41
42
43
44
# File 'lib/babylon/base/controller.rb', line 36

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



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

def evaluate
  return @view.evaluate if @view
  return @content
end

#perform(action) ⇒ Object

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



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/babylon/base/controller.rb', line 22

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


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/babylon/base/controller.rb', line 52

def render(options = {}) 
  return if @view and !options[:force] # Avoid double rendering, if we have already attached a view
  
  if options == :nothing
    @view = Babylon::Base::View.new()
  elsif options.kind_of? String
    @content = options
  elsif options == {} # default rendering
    # Babylon.logger.debug("rendering default view #{default_template_name}")
    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
      Babylon.logger.debug("rendering from here #{file} #{view_path(file)}")
      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