Class: SimpleConsole::Controller
- Inherits:
-
Object
- Object
- SimpleConsole::Controller
- Defined in:
- lib/controller.rb,
lib/init.rb,
lib/simpleconsole.rb
Overview
The Controller defines actions for the application. Each action can be implemented as an instance method.
Sample Usage
# .. inside controller.rb
class MyController < SimpleConsole::Controller
def say_hello
puts "hello!"
end
end
# .. inside myapp
# ..
Application.run(ARGV, MyController)
What it did
The above example defines the action “say_hello”. So in the console:
% myapp say_hello
hello!
Related Methods
Check out the “before_filter” and “after_filter” methods, similar to the methods implemented in Rails.
Constant Summary collapse
- @@before_chain =
Filter for methods to call previous to an action
SimpleConsole::Filter.new
- @@after_chain =
Filter for methods to call after an action occurs
SimpleConsole::Filter.new
- @@params_parser =
Keeps track of param types
SimpleConsole::ParamsParser.new
Instance Attribute Summary collapse
-
#argv ⇒ Object
Returns the value of attribute argv.
-
#params ⇒ Object
Returns the value of attribute params.
Instance Method Summary collapse
-
#execute_action ⇒ Object
Executes in order: before_filter_for action actual action after_filter_for action.
-
#initialize ⇒ Controller
constructor
Initializes the “params” hash and creates a new Controller.
-
#respond_to?(action) ⇒ Boolean
Returns true if the controller defines the action given, otherwise returns false (returns false on private methods, also).
-
#set_action(action) ⇒ Object
Sets the current action to the new action parameter.
-
#set_params(argv) ⇒ Object
Sets the controller’s params hash when given ARGV.
Constructor Details
#initialize ⇒ Controller
Initializes the “params” hash and creates a new Controller. Not needed when developing an application with SimpleConsole.
41 42 43 44 |
# File 'lib/controller.rb', line 41 def initialize() @params = Hash.new @argv = Array.new end |
Instance Attribute Details
#argv ⇒ Object
Returns the value of attribute argv.
28 29 30 |
# File 'lib/controller.rb', line 28 def argv @argv end |
#params ⇒ Object
Returns the value of attribute params.
28 29 30 |
# File 'lib/controller.rb', line 28 def params @params end |
Instance Method Details
#execute_action ⇒ Object
Executes in order:
before_filter_for action
actual action
after_filter_for action
80 81 82 83 84 85 86 87 88 89 |
# File 'lib/controller.rb', line 80 def execute_action begin params[:action] ||= :default call_before_filter_for(params[:action]) send(params[:action]) if respond_to?(params[:action]) call_after_filter_for(params[:action]) rescue RuntimeError => error raise error unless error. == "Simple::Console Redirection" end end |
#respond_to?(action) ⇒ Boolean
Returns true if the controller defines the action given, otherwise returns false (returns false on private methods, also).
48 49 50 51 52 53 54 55 56 |
# File 'lib/controller.rb', line 48 def respond_to?(action) if @@block_action.include?(action.to_s) return false elsif private_methods.include?(action.to_s) return false else super end end |
#set_action(action) ⇒ Object
Sets the current action to the new action parameter.
72 73 74 |
# File 'lib/controller.rb', line 72 def set_action(action) params[:action] = action end |
#set_params(argv) ⇒ Object
Sets the controller’s params hash when given ARGV.
59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/controller.rb', line 59 def set_params(argv) # Set params[:id] id = argv[1] if argv[1] && argv[1] !~ /^-/ if id.to_i == 0 && id != "0" params[:id] = id else params[:id] = id.to_i end params.update(@@params_parser.argv_to_params(argv)) argv.concat(argv) end |