Class: SimpleConsole::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/application.rb,
lib/init.rb,
lib/simpleconsole.rb

Overview

Runs the application with the given controller and view.

Sample Usage

Inside the script myapp:

#!/usr/bin/env ruby -w
require "rubygems"
require "simpleconsole"
require "your_files_with_controller_and_view.rb"

Application.run(ARGV, ControllerClassConstant, ViewClassConstant)
# .. the ViewClassConstant is optional, so run(ARGV, ControllerClassConstant) is valid

From the command line:

myapp [action] [id] [options]
myapp action id --key value
  • action - this will be implemented as a method in the controller and/or view

  • id - this is accessible in the controller/view as params

  • options - will become available through the params hash. So “–key value” will be params = value.

Routing Rules

  • Usually, ARGV is passed to argv for parsing and execution.

  • ARGV is taken to be the action to execute

  • In the case that ARGV does not exist, then the controller and view are called with the method ‘default’.

  • If controller_klass implements action as a method, it is executed.

  • If view_klass implements action as a method, it is executed after the controller.

  • If there is no view_klass, no action is taken for the view.

  • If the action only exists in the controller_klass or view_klass, only that single action is executed and no errors are raised.

  • If no action exists in the controller_klass or view_klass, controller_klass’s method_missing is called and the view is ignored.

Class Method Summary collapse

Class Method Details

.run(argv, controller_klass, view_klass = nil) ⇒ Object

This is the method to call to run your program from the top-level script. Check out the rdoc on SimpleConsole::Application for usage.



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/application.rb', line 36

def self.run(argv, controller_klass, view_klass = nil)
  @@control = controller_klass.new
  @@view = view_klass ? view_klass.new(@@control) : nil
  @@control.set_params(argv)
  @@control.set_action(argv[0] =~ /^-/ ? :default : argv[0] || :default)

  @@control.execute_action
  @@view.render_action unless @@view.nil?

  @@control.method_missing(@@control.params[:action]) if !control_implements? && !view_implements?
end