Class: Controller::Base
Direct Known Subclasses
Constant Summary collapse
- @@before_view =
{}
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
-
#default_command ⇒ Object
Returns the value of attribute default_command.
-
#input ⇒ Object
Returns the value of attribute input.
-
#new_route ⇒ Object
Returns the value of attribute new_route.
-
#prompt ⇒ Object
Returns the value of attribute prompt.
-
#rendered ⇒ Object
Returns the value of attribute rendered.
Class Method Summary collapse
Instance Method Summary collapse
- #before_view_file ⇒ Object
-
#initialize(command, s = {}) ⇒ Base
constructor
:nodoc:.
-
#load_and_read_file(file_name) ⇒ Object
this method uses view_dir…
-
#render(options = {}) ⇒ Object
render sets the text that will be sent back as a response to this command you can only render once per controller.
- #render_template(vf) ⇒ Object
-
#route_to(options = {}) ⇒ Object
route_to is the command that routes a connection’s input to the next command controller, or default command.
-
#service(*a) ⇒ Object
service is the main method that controlls the flow through the controller.
- #session ⇒ Object
- #to_s ⇒ Object
- #view_dir ⇒ Object
- #view_file(method) ⇒ Object
Constructor Details
#initialize(command, s = {}) ⇒ Base
:nodoc:
69 70 71 72 |
# File 'lib/controller.rb', line 69 def initialize(command, s = {}) #:nodoc: @method = command.downcase @session = s end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
11 12 13 |
# File 'lib/controller.rb', line 11 def body @body end |
#default_command ⇒ Object
Returns the value of attribute default_command.
12 13 14 |
# File 'lib/controller.rb', line 12 def default_command @default_command end |
#input ⇒ Object
Returns the value of attribute input.
11 12 13 |
# File 'lib/controller.rb', line 11 def input @input end |
#new_route ⇒ Object
Returns the value of attribute new_route.
12 13 14 |
# File 'lib/controller.rb', line 12 def new_route @new_route end |
#prompt ⇒ Object
Returns the value of attribute prompt.
12 13 14 |
# File 'lib/controller.rb', line 12 def prompt @prompt end |
#rendered ⇒ Object
Returns the value of attribute rendered.
11 12 13 |
# File 'lib/controller.rb', line 11 def rendered @rendered end |
Class Method Details
.before_view ⇒ Object
131 132 133 |
# File 'lib/controller.rb', line 131 def Base.before_view return @@before_view end |
Instance Method Details
#before_view_file ⇒ Object
102 103 104 |
# File 'lib/controller.rb', line 102 def before_view_file view_file @@before_view[default_command].to_s end |
#load_and_read_file(file_name) ⇒ Object
this method uses view_dir…
107 108 109 110 111 112 113 |
# File 'lib/controller.rb', line 107 def load_and_read_file(file_name) file = File.open(File.join(view_dir, file_name)) file.read rescue Errno::ENOENT #if we haven't at least rendered something, we are going to through a nocommand exception here.. raise Controller::NoCommandException end |
#render(options = {}) ⇒ Object
render sets the text that will be sent back as a response to this command you can only render once per controller. This is in place to avoid magical renderings from happening out of the developers control.
render called with no options yields the view file rendered if one exists.
render #=> renders /mud/views/controller/view.rhtml
render :text => "send this to them" #=> send this to them
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/controller.rb', line 58 def render( = {}) raise "Double Render Error" if self.rendered self.rendered = true return if [:nothing] if [:text] @body = [:text] return end @body = render_template(view_file(@method)) end |
#render_template(vf) ⇒ Object
119 120 121 |
# File 'lib/controller.rb', line 119 def render_template(vf) ERB.new(vf, nil, '-').result(binding) end |
#route_to(options = {}) ⇒ Object
route_to is the command that routes a connection’s input to the next command controller, or default command.
route_to :route =>StartController, :command=>:begin, :prompt=>">"
This will make the user's next input goto the StartController
with the command "begin". After rendering the command
it will output ">" to the user.
route_to :view=>"password"
This will keep the user's route the same. After rendering the command
it will output the content of the password view rendered in the context
of the command's binding. The directory for the password view will
be the same as the Controller.
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/controller.rb', line 31 def route_to(={}) config = {:controller => self.class, :command => nil, :prompt => "", :view => :default} config.update if .is_a?(Hash) self.new_route = config[:controller] self.default_command = config[:command] self.prompt = config[:prompt] if config[:prompt] != "" self.prompt = render_template(before_view_file) if config[:view] == :default && @@before_view[config[:command]] end |
#service(*a) ⇒ Object
service is the main method that controlls the flow through the controller. first it runs the method, then it renders the view if something hasn’t already been rendered then it tacks the prompt onto the end of the command.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/controller.rb', line 78 def service(*a) if respond_to? @method if self.method(@method).arity >= 1 send(@method, *a) else send(@method) end end if !self.rendered render end @body += self.prompt if self.prompt session[:handle].puts(@body) self end |
#session ⇒ Object
123 124 125 |
# File 'lib/controller.rb', line 123 def session() return @session end |
#to_s ⇒ Object
127 128 129 |
# File 'lib/controller.rb', line 127 def to_s "#{@body}" end |
#view_dir ⇒ Object
115 116 117 |
# File 'lib/controller.rb', line 115 def view_dir File.join("mud","views",route.to_s.downcase.chomp("controller")) end |
#view_file(method) ⇒ Object
97 98 99 100 |
# File 'lib/controller.rb', line 97 def view_file(method) command_file = method + ".rhtml" load_and_read_file(command_file) end |