Class: Mithril::Controllers::AbstractController
- Inherits:
-
Object
- Object
- Mithril::Controllers::AbstractController
- Extended by:
- Mixins::MixinWithActions
- Defined in:
- lib/mithril/controllers/abstract_controller.rb
Overview
Base class for Mithril controllers. Extending controller functionality can be implemented either through direct class inheritance, e.g.
ModuleController > ProxyController > AbstractController
or through mixing in shared functionality with a Mixin, but all controllers ought to extend AbstractController unless you have a very compelling reason otherwise.
Direct Known Subclasses
Instance Method Summary collapse
-
#allow_empty_action? ⇒ Boolean
If this method evaluates to true, if the controller does not recognize an action from the input text, it will attempt to invoke the empty action :“” with the full arguments list.
-
#can_invoke?(input) ⇒ Boolean
True if this controller has a command matching the provided input.
-
#command_missing(input) ⇒ Object
Default output when a command cannot be found for a given input.
-
#commands ⇒ Array
All commands available to this controller.
-
#has_command?(text) ⇒ Boolean
True if this controller has the specified command.
-
#initialize(request) ⇒ AbstractController
constructor
A new instance of AbstractController.
-
#invoke_command(text) ⇒ Object
Parses input into a command and arguments, then matches the command to an available action (if any), invokes the action, and returns the result.
-
#parse_command(input) ⇒ Array
Delegates to the parser instance.
-
#parser ⇒ Object
The parser object used to process input into a command and an arguments object.
Methods included from Mixin
Constructor Details
#initialize(request) ⇒ AbstractController
Returns a new instance of AbstractController.
25 26 27 28 29 30 31 |
# File 'lib/mithril/controllers/abstract_controller.rb', line 25 def initialize(request) unless request.respond_to? :session raise ArgumentError.new "expected request to respond_to :session" end # unless @request = request end |
Instance Method Details
#allow_empty_action? ⇒ Boolean
If this method evaluates to true, if the controller does not recognize an action from the input text, it will attempt to invoke the empty action :“” with the full arguments list.
126 127 128 |
# File 'lib/mithril/controllers/abstract_controller.rb', line 126 def allow_empty_action? false end |
#can_invoke?(input) ⇒ Boolean
Returns True if this controller has a command matching the provided input. Otherwise false.
82 83 84 |
# File 'lib/mithril/controllers/abstract_controller.rb', line 82 def can_invoke?(input) self.allow_empty_action? || !self.parse_command(input).first.nil? end |
#command_missing(input) ⇒ Object
Default output when a command cannot be found for a given input.
89 90 91 92 |
# File 'lib/mithril/controllers/abstract_controller.rb', line 89 def command_missing(input) "I'm sorry, I don't know how to \"#{input.to_s}\". Please try another" + " command, or enter \"help\" for assistance." end |
#commands ⇒ Array
Returns All commands available to this controller.
59 60 61 |
# File 'lib/mithril/controllers/abstract_controller.rb', line 59 def commands actions.keys.map do |key| key.to_s.gsub '_', ' '; end end |
#has_command?(text) ⇒ Boolean
Returns True if this controller has the specified command. Otherwise false.
69 70 71 |
# File 'lib/mithril/controllers/abstract_controller.rb', line 69 def has_command?(text) commands.include? text.to_s end |
#invoke_command(text) ⇒ Object
Parses input into a command and arguments, then matches the command to an available action (if any), invokes the action, and returns the result. If there is no matching command, but the controller has an empty action :“” defined and allow_empty_action? evaluates to true, the controller will instead invoke the empty action with the parsed arguments. If no matching command is found, returns the result of command_missing.
108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/mithril/controllers/abstract_controller.rb', line 108 def invoke_command(text) # Mithril.logger.debug "#{class_name}.invoke_command(), text =" + # " #{text.inspect}" command, args = self.parse_command text if self.has_action? command self.invoke_action command, args elsif allow_empty_action? self.invoke_action :"", args else command_missing(text) end # unless-elsif end |
#parse_command(input) ⇒ Array
Delegates to the parser instance. Note that if a custom parser is used that does not conform to the expected API, the return values may be different than listed.
54 55 56 |
# File 'lib/mithril/controllers/abstract_controller.rb', line 54 def parse_command(input) self.parser.parse_command input end |
#parser ⇒ Object
The parser object used to process input into a command and an arguments object.
43 44 45 |
# File 'lib/mithril/controllers/abstract_controller.rb', line 43 def parser @parser ||= Mithril::Parsers::SimpleParser.new(self) end |