Class: SimpleConsole::Controller

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

Instance Method Summary collapse

Constructor Details

#initializeController

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

#argvObject

Returns the value of attribute argv.



28
29
30
# File 'lib/controller.rb', line 28

def argv
  @argv
end

#paramsObject

Returns the value of attribute params.



28
29
30
# File 'lib/controller.rb', line 28

def params
  @params
end

Instance Method Details

#execute_actionObject

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.message == "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).

Returns:

  • (Boolean)


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