Class: SimpleConsole::View

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

Overview

The View is meant to render the output for each action in the Controller. Each ‘view template’ is just another method definition, usually with a lot of ‘puts’ method calls. The instance variables of the Controller is injected into the View, so if @var is set in the controller, you can access it in the view.

Sample Usage

class MyController < SimpleConsole::Controller
  def say_hello
    @word = "Hello!"
  end
end

class MyView < SimpleConsole::View
  def say_hello
    puts @word
  end
end

SimpleConsole::Application.run(ARGV, MyController, MyView)

What it did

The above example defines the action “say_hello”. So in the console:

% myapp say_hello
Hello!

Related Methods

Check out SimpleConsole::Controller’s render method, which can change which view template is rendered.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(controller) ⇒ View

Llinks the view to the controller



35
36
37
# File 'lib/view.rb', line 35

def initialize(controller) #:nodoc
  @control = controller
end

Instance Attribute Details

#paramsObject

Returns the value of attribute params.



32
33
34
# File 'lib/view.rb', line 32

def params
  @params
end

Instance Method Details

#render_action(action = @control.params[:action]) ⇒ Object

Renders the current action, the default being @control’s params



40
41
42
43
44
45
46
47
# File 'lib/view.rb', line 40

def render_action(action = @control.params[:action]) #:nodoc
  set_variables
  begin
    send(action) if respond_to?(action)
  rescue TypeError => error
    raise TypeError, "params[:action] should be set to :default if no action is given"
  end
end