Class: Controller::Base

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

Direct Known Subclasses

NotFound, ServerError

Constant Summary collapse

@@before_view =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#bodyObject

Returns the value of attribute body.



11
12
13
# File 'lib/controller.rb', line 11

def body
  @body
end

#default_commandObject

Returns the value of attribute default_command.



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

def default_command
  @default_command
end

#inputObject

Returns the value of attribute input.



11
12
13
# File 'lib/controller.rb', line 11

def input
  @input
end

#new_routeObject

Returns the value of attribute new_route.



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

def new_route
  @new_route
end

#promptObject

Returns the value of attribute prompt.



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

def prompt
  @prompt
end

#renderedObject

Returns the value of attribute rendered.



11
12
13
# File 'lib/controller.rb', line 11

def rendered
  @rendered
end

Class Method Details

.before_viewObject



131
132
133
# File 'lib/controller.rb', line 131

def Base.before_view
  return @@before_view
end

Instance Method Details

#before_view_fileObject



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(options = {})
  raise "Double Render Error" if self.rendered
  self.rendered = true
  return if options[:nothing]
  if options[:text]
    @body = options[: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(options={})
  config = {:controller => self.class, :command => nil, :prompt => "", :view => :default}
  config.update options if options.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

#sessionObject



123
124
125
# File 'lib/controller.rb', line 123

def session()
  return @session
end

#to_sObject



127
128
129
# File 'lib/controller.rb', line 127

def to_s
  "#{@body}"
end

#view_dirObject



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