Class: Waw::ActionController

Inherits:
Controller show all
Extended by:
ActionUtils, FullState::OnClass, FullState::OnInstance
Includes:
ActionUtils
Defined in:
lib/waw/controllers/action_controller.rb,
lib/waw/controllers/action/action.rb,
lib/waw/controllers/action/action_utils.rb,
lib/waw/controllers/action/js_generation.rb

Overview

Defines a specific application controller for executing actions.

Defined Under Namespace

Modules: ActionUtils Classes: Action, JSGeneration

Constant Summary collapse

@@controllers =

All subclasses

[]

Constants included from EnvironmentUtils

EnvironmentUtils::DEPRECATED_MSG

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ActionUtils

extract_action_name, find_action, has_action?

Methods included from FullState::OnClass

__full_state_variables, query_var, session_var

Methods included from FullState::OnInstance

query_var, session_var

Methods inherited from Controller

#call

Methods included from Rack::Delegator

#_visit, #delegate, #find_rack_app, #find_url_of, #has_delegate?, #is_delegate?, #visit

Methods included from EnvironmentUtils

#env, #session_get, #session_has_key?, #session_set, #session_unset

Methods included from ScopeUtils

#config, #find_kernel_context, #logger, #params, #rack_env, #real_session, #request, #resources, #response, #root_folder, #session

Class Method Details

.actionsObject

Returns installed actions



53
54
55
# File 'lib/waw/controllers/action_controller.rb', line 53

def actions
  @actions ||= {}
end

.controllersObject

Returns known controllers



20
21
22
# File 'lib/waw/controllers/action_controller.rb', line 20

def controllers
  @@controllers
end

.inherited(child) ⇒ Object

When this class is inherited we track the new controller, it becomes a Singleton and we install code generation as start hook if not already done.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/waw/controllers/action_controller.rb', line 26

def inherited(child)
  super(child)
  # Adds the controller as a child
  controllers << child
  
  # Let it become a singleton
  child.instance_eval { include Singleton }
  
  # And install start hook for code generation
  if controllers.size==1 && (k = Waw.kernel)
    k.add_start_hook(JSGeneration.new) 
    k.add_unload_hook ::Kernel.lambda { @@controllers = [] }
  end
end

.method_added(name) ⇒ Object

If a signature has been installed, let the next added method become an action



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/waw/controllers/action_controller.rb', line 71

def method_added(name)
  if @signature and not(@critical)
    @critical = true

    # Create the action instance
    meth = instance_method(name)
    if meth.arity != 1
      raise WawError, "Action #{self}::#{name} is missing its 'params' argument"
    else
      action = Waw::ActionController::Action.new(name, @signature, @routing, self, meth)
      actions[name] = action
    end
    
    # Installs the class method that returns the action
    instance_eval <<-EOF
      class << self
        def #{name}
          actions[:#{name}]
        end
      end
    EOF
    
    # Installs the instance method that execute the action
    define_method name do |params|
      action.execute(params)
    end 
  end
  @signature, @routing, @critical = nil, nil, false
end

.routing(&block) ⇒ Object

Installs a routing block for the next action



65
66
67
# File 'lib/waw/controllers/action_controller.rb', line 65

def routing(&block)
  @routing = Waw::Routing::ActionRouting.new(&block)
end

.signature(signature = nil, &block) ⇒ Object

Fired when a signature will be next installed



58
59
60
61
62
# File 'lib/waw/controllers/action_controller.rb', line 58

def signature(signature=nil, &block)
  signature = (signature.nil? ? Waw::Validation::Signature.new : signature)
  signature = signature.merge(&block) if block
  @signature = signature
end

.urlObject

Returns the url on which this controller is mapped



48
49
50
# File 'lib/waw/controllers/action_controller.rb', line 48

def url
  self.instance.url
end

Instance Method Details

#actionsObject

Returns the actions installed on this controller



109
110
111
# File 'lib/waw/controllers/action_controller.rb', line 109

def actions
  self.class.actions
end

#encapsulate(action, actual_params, &block) ⇒ Object

Ensapsules the action call



114
115
116
# File 'lib/waw/controllers/action_controller.rb', line 114

def encapsulate(action, actual_params, &block)
  yield
end

#execute(env, request, response) ⇒ Object

Executes the controller



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/waw/controllers/action_controller.rb', line 119

def execute(env, request, response)
  action_name = request.respond_to?(:path) ? request.path : request[:action]
  logger.debug("Executing the action whose name is #{action_name}")
  action = find_action(action_name)
  if action
    actual_params = request.params.symbolize_keys
    result = encapsulate(action, actual_params) do 
      action.execute(actual_params)
    end
    logger.debug("ActionResult is #{result.inspect}")
    [200, {}, result]
  else
    logger.warn("Action #{action_name} has not been found")
    [200, {}, [:error, :action_not_found]]
  end
end

#urlObject

Returns the url on which this controller is mapped



104
105
106
# File 'lib/waw/controllers/action_controller.rb', line 104

def url
  find_kernel_context.find_url_of(self)
end