Class: Controller::Base
Direct Known Subclasses
Constant Summary collapse
- @@before_view =
{}
Instance Attribute Summary collapse
-
#args ⇒ Object
Returns the value of attribute args.
-
#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.
-
#redirect ⇒ Object
Returns the value of attribute redirect.
-
#rendered ⇒ Object
Returns the value of attribute rendered.
Class Method Summary collapse
- .before_view ⇒ Object
- .command_methods ⇒ Object
- .hidden_commands ⇒ Object
- .hide_action(*names) ⇒ Object
Instance Method Summary collapse
- #before_view_file ⇒ Object
- #command_methods ⇒ Object
-
#find_method(method_string) ⇒ Object
simple begins with abbreviation with alphabetical order.
-
#initialize(command, s = {}) ⇒ Base
constructor
:nodoc:.
-
#load_and_read_file(file_name) ⇒ Object
this method uses view_dir…
-
#redirect_to(options = {}) ⇒ Object
TODO: Implement this.
-
#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
- #rendered? ⇒ Boolean
-
#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:
85 86 87 88 89 |
# File 'lib/controller.rb', line 85 def initialize(command, s = {}) #:nodoc: @method_string = command.downcase @session = s self.redirect = false end |
Instance Attribute Details
#args ⇒ Object
Returns the value of attribute args.
14 15 16 |
# File 'lib/controller.rb', line 14 def args @args end |
#body ⇒ Object
Returns the value of attribute body.
13 14 15 |
# File 'lib/controller.rb', line 13 def body @body end |
#default_command ⇒ Object
Returns the value of attribute default_command.
14 15 16 |
# File 'lib/controller.rb', line 14 def default_command @default_command end |
#input ⇒ Object
Returns the value of attribute input.
13 14 15 |
# File 'lib/controller.rb', line 13 def input @input end |
#new_route ⇒ Object
Returns the value of attribute new_route.
14 15 16 |
# File 'lib/controller.rb', line 14 def new_route @new_route end |
#prompt ⇒ Object
Returns the value of attribute prompt.
14 15 16 |
# File 'lib/controller.rb', line 14 def prompt @prompt end |
#redirect ⇒ Object
Returns the value of attribute redirect.
14 15 16 |
# File 'lib/controller.rb', line 14 def redirect @redirect end |
#rendered ⇒ Object
Returns the value of attribute rendered.
13 14 15 |
# File 'lib/controller.rb', line 13 def rendered @rendered end |
Class Method Details
.before_view ⇒ Object
169 170 171 |
# File 'lib/controller.rb', line 169 def Base.before_view return @@before_view end |
.command_methods ⇒ Object
186 187 188 |
# File 'lib/controller.rb', line 186 def self.command_methods @command_methods ||= Set.new(public_instance_methods - hidden_commands) end |
.hidden_commands ⇒ Object
174 175 176 177 |
# File 'lib/controller.rb', line 174 def hidden_commands write_inheritable_attribute(:hidden_commands, Controller::Base.public_instance_methods) unless read_inheritable_attribute(:hidden_commands) read_inheritable_attribute(:hidden_commands) end |
.hide_action(*names) ⇒ Object
178 179 180 181 |
# File 'lib/controller.rb', line 178 def hide_action(*names) write_inheritable_attribute(:hidden_actions, hidden_actions | names.collect { |n| n.to_s }) end |
Instance Method Details
#before_view_file ⇒ Object
139 140 141 |
# File 'lib/controller.rb', line 139 def before_view_file view_file @@before_view[default_command].to_s end |
#command_methods ⇒ Object
183 184 185 |
# File 'lib/controller.rb', line 183 def command_methods self.class.command_methods end |
#find_method(method_string) ⇒ Object
simple begins with abbreviation with alphabetical order
92 93 94 95 |
# File 'lib/controller.rb', line 92 def find_method(method_string) methods = self.command_methods.sort methods.abbrev[method_string] end |
#load_and_read_file(file_name) ⇒ Object
this method uses view_dir…
144 145 146 147 148 149 150 151 |
# File 'lib/controller.rb', line 144 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 if !@method raise Controller::NoViewException end |
#redirect_to(options = {}) ⇒ Object
TODO: Implement this. Should jump to next controller method right after this one finishes
46 47 48 49 50 51 52 53 54 |
# File 'lib/controller.rb', line 46 def redirect_to(={}) config = {:route => self.class, :command => nil, :prompt => "", :view => :default, :args => ""} config.update if .is_a?(Hash) raise Exception.new("Could not redirect to nil. Please supply a command to redirect to.") if config[:command] == nil self.redirect = true self.args = config[:args] route_to(config) 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.eruby
render :text => "send this to them" #=> send this to them
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/controller.rb', line 65 def render( = {}) raise "Double Render Error" if self.rendered self.rendered = true config = {:view => @method} config.update if .is_a?(Hash) return if config[:nothing] if config[:text] @body = config[:text] return end @body = render_template(view_file(config[:view])) end |
#render_template(vf) ⇒ Object
157 158 159 |
# File 'lib/controller.rb', line 157 def render_template(vf) ERB.new(vf, nil, '-').result(binding) end |
#rendered? ⇒ Boolean
81 82 83 |
# File 'lib/controller.rb', line 81 def rendered? @rendered 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.
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/controller.rb', line 33 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.
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/controller.rb', line 103 def service(*a) @method = find_method(@method_string) if @method == nil render :view=>@method_string else if respond_to? @method if self.method(@method).arity >= 1 send(@method, *a) else send(@method) end end if !self.rendered begin render rescue NoViewException if self.redirect return self end raise end end end @body += self.prompt if self.prompt session[:handle].puts @body self end |
#session ⇒ Object
161 162 163 |
# File 'lib/controller.rb', line 161 def session() return @session end |
#to_s ⇒ Object
165 166 167 |
# File 'lib/controller.rb', line 165 def to_s "#{@body}" end |
#view_dir ⇒ Object
153 154 155 |
# File 'lib/controller.rb', line 153 def view_dir File.join("mud","views",route.to_s.downcase.chomp("controller")) end |
#view_file(method) ⇒ Object
134 135 136 137 |
# File 'lib/controller.rb', line 134 def view_file(method) command_file = method.to_s + ".eruby" load_and_read_file(command_file) end |