Class: Mithril::Controllers::AbstractController

Inherits:
Object
  • Object
show all
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

ProxyController

Instance Method Summary collapse

Methods included from Mixin

#mixins, #mixins=

Constructor Details

#initialize(request) ⇒ AbstractController

Returns a new instance of AbstractController.

Parameters:

  • request (Mithril::Request)

    Request object containing the volatile state information needed to build the controller and execute commands.

Raises:

  • (ArgumentError)

    If request does not respond to :session.



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.

Returns:

  • (Boolean)


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.

Examples:

Using :has_command? and :can_invoke?

# With an action "do" defined
has_command?("do something") #=> false
can_invoke?("do something")  #=> true

Parameters:

  • input (Object)

    The sample input to be parsed. Type and format will depend on the parser used.

Returns:

  • (Boolean)

    True if this controller has a command matching the provided input. Otherwise false.

See Also:



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.

Parameters:

  • input (Object)

    The input that failed to match a command.

Returns:

  • (Object)


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

#commandsArray

Returns All commands available to this controller.

Returns:

  • (Array)

    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.

Parameters:

  • text (String, Symbol)

    The command to check. The parameter is converted to a String, and must exactly match a command available to the controller.

Returns:

  • (Boolean)

    True if this controller has the specified command. Otherwise false.

See Also:



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.

Parameters:

  • input (Object)

    The input to be parsed and evaluated. Type and format will depend on the parser used.

Returns:

  • (Object)

    The result of the command. If no command is found, returns the result of command_missing.

See Also:



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.

Parameters:

  • input (Object)

    The input to be parsed.

Returns:

  • (Array)

    The first element will be the matched command, or nil if no command was found. The second element will be the arguments object created by the parser.



54
55
56
# File 'lib/mithril/controllers/abstract_controller.rb', line 54

def parse_command(input)
  self.parser.parse_command input
end

#parserObject

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