Class: Mithril::Controllers::ProxyController

Inherits:
AbstractController show all
Defined in:
lib/mithril/controllers/proxy_controller.rb

Overview

Redirects incoming commands to a proxy controller based on the :proxy method. If no proxy is present, evaluates commands as normal.

Instance Method Summary collapse

Methods inherited from AbstractController

#allow_empty_action?, #command_missing, #has_command?, #initialize, #parse_command, #parser

Methods included from Mixin

#mixins, #mixins=

Constructor Details

This class inherits a constructor from Mithril::Controllers::AbstractController

Instance Method Details

#allow_own_actions_while_proxied?Boolean

If evalutes to true, then any actions defined on this controller will be available even when a proxy is present. Defaults to true, but can be overriden in subclasses.

Returns:

  • (Boolean)


22
23
24
# File 'lib/mithril/controllers/proxy_controller.rb', line 22

def allow_own_actions_while_proxied?
  true
end

#can_invoke?(input) ⇒ Boolean

As can_invoke?, but returns true iff the command is available on this controller directly, as opposed to through a proxy subject.

Parameters:

  • (String)

Returns:

  • (Boolean)

See Also:



46
47
48
49
50
51
52
53
54
# File 'lib/mithril/controllers/proxy_controller.rb', line 46

def can_invoke?(input)
  if self.proxy.nil?
    super
  elsif self.allow_own_actions_while_proxied? && self.can_invoke_on_self?(input)
    super
  else
    proxy.can_invoke?(input)
  end # if-elsif-else
end

#can_invoke_on_self?Object



38
# File 'lib/mithril/controllers/proxy_controller.rb', line 38

alias_method :can_invoke_on_self?, :can_invoke?

#commandsObject



27
28
29
30
31
32
33
34
35
# File 'lib/mithril/controllers/proxy_controller.rb', line 27

def commands
  if proxy.nil?
    super
  elsif self.allow_own_actions_while_proxied?
    super + proxy.commands
  else
    proxy.commands
  end # if-elsif-else
end

#invoke_command(input) ⇒ Object

If no proxy is present, attempts to invoke the command on self. If a proxy subject is present and the parent can invoke that command and allow_own_actions_while_proxied? evaluates to true, attempts to invoke the command on self. Otherwise, if the proxy subject can invoke that command, invokes the command on the proxy subject.

This precedence order was selected to allow reflection within commands, e.g. the help action in Mixins::HelpActions that lists all available commands.

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:



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/mithril/controllers/proxy_controller.rb', line 72

def invoke_command(input)
  # Mithril.logger.debug "#{class_name}.invoke_command(), text =" +
  #   " #{text.inspect}, session = #{request.session.inspect}, proxy =" +
  #   " #{proxy}"
  
  if self.proxy.nil?
    super
  elsif self.allow_own_actions_while_proxied? && self.can_invoke_on_self?(input)
    super
  elsif proxy.can_invoke? input
    proxy.invoke_command input
  else
    command_missing(input)
  end # if-elsif-else
end

#proxyAbstractController

The subject controller to which commands are redirected. Must be overriden in subclasses.

Returns:



13
14
15
# File 'lib/mithril/controllers/proxy_controller.rb', line 13

def proxy
  nil # override this in sub-classes
end