Class: Babylon::Base::Controller
- Inherits:
-
Object
- Object
- Babylon::Base::Controller
- Defined in:
- lib/babylon/base/controller.rb
Overview
Your application’s controller should be descendant of this class.
Instance Attribute Summary collapse
-
#action_name ⇒ Object
Stanza received by the controller (Nokogiri::XML::Node).
-
#rendered ⇒ Object
Stanza received by the controller (Nokogiri::XML::Node).
-
#stanza ⇒ Object
Stanza received by the controller (Nokogiri::XML::Node).
Instance Method Summary collapse
-
#assigns ⇒ Object
Returns the list of variables assigned during the action.
-
#evaluate ⇒ Object
Actually evaluates the view.
-
#initialize(stanza = nil) ⇒ Controller
constructor
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.
-
#perform(action) ⇒ Object
Performs the action and calls back the optional block argument : you should not override this function.
-
#render(options = {}) ⇒ Object
Called by default after each action to “build” a XMPP stanza.
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_name ⇒ Object
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 |
#rendered ⇒ Object
Stanza received by the controller (Nokogiri::XML::Node)
8 9 10 |
# File 'lib/babylon/base/controller.rb', line 8 def rendered @rendered end |
#stanza ⇒ Object
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
#assigns ⇒ Object
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 |
#evaluate ⇒ Object
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( = {}) return if @view and ![:force] # Avoid double rendering, if we have already attached a view if == :nothing @view = Babylon::Base::View.new() elsif .kind_of? String @content = elsif == {} # default rendering # Babylon.logger.debug("rendering default view #{default_template_name}") result = render(:file => default_template_name) elsif [:file] file = [: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 = [:action] result = render(:file => default_template_name(action_name.to_s)) elsif [:nothing] @view = Babylon::Base::View.new() end end |