Module: Vedeu::Runtime::Router

Extended by:
Vedeu::Repositories::Storage, Router
Includes:
Common
Included in:
Router
Defined in:
lib/vedeu/runtime/router.rb

Overview

Stores all client application controllers with their respective actions.

Instance Method Summary collapse

Methods included from Vedeu::Repositories::Storage

reset!, storage

Methods included from Common

#absent?, #array?, #boolean, #boolean?, #empty_value?, #escape?, #falsy?, #hash?, #line_model?, #numeric?, #positionable?, #present?, #snake_case, #stream_model?, #string?, #symbol?, #truthy?, #view_model?

Instance Method Details

#action_defined?(action, controller) ⇒ Boolean (private)

Returns a boolean indicating whether the given action name is defined for the given controller.

Parameters:

  • action (Symbol)
  • controller (Symbol)

Returns:



110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/vedeu/runtime/router.rb', line 110

def action_defined?(action, controller)
  if registered?(controller)
    return true if storage[controller][:actions].include?(action)

    raise Vedeu::Error::ActionNotFound,
          "#{action} is not registered for #{controller}."

  else
    raise Vedeu::Error::ControllerNotFound,
          "#{controller} is not registered."

  end
end

#add_action(controller, action) ⇒ void

This method returns an undefined value.

Registers an action to the given controller name for the client application.

Parameters:

  • controller (Symbol)
  • action (Symbol)

Raises:



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/vedeu/runtime/router.rb', line 53

def add_action(controller, action)
  if present?(controller) && present?(action)
    Vedeu.log(type:    :create,
              message: "Action: ':#{action}' " \
                       "(for ':#{controller}')")

    if registered?(controller)
      storage[controller][:actions] << action

    else
      add_controller(controller, '')
      add_action(controller, action)

    end

    storage

  else
    raise Vedeu::Error::MissingRequired,
          'Cannot store action without a controller or name ' \
          'attribute.'

  end
end

#add_controller(controller, klass) ⇒ void

This method returns an undefined value.

Registers a controller with the given controller name for the client application.

Parameters:

  • controller (Symbol)
  • klass (String)

Raises:



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/vedeu/runtime/router.rb', line 26

def add_controller(controller, klass)
  unless present?(controller)
    raise Vedeu::Error::MissingRequired,
          'Cannot store controller without a name attribute.'
  end

  Vedeu.log(type:    :create,
            message: "Controller: ':#{controller}'")

  if registered?(controller)
    storage[controller][:klass] = klass

  else
    storage.store(controller, klass: klass, actions: [])

  end

  storage
end

#goto(controller, action, **args) ⇒ void Also known as: action

This method returns an undefined value.

Parameters:

  • controller (Symbol)
  • action (Symbol)
  • args (void)

Raises:



85
86
87
88
89
90
# File 'lib/vedeu/runtime/router.rb', line 85

def goto(controller, action, **args)
  Vedeu.log(type:    :debug,
            message: "Routing: #{controller} #{action}")

  route(controller, action, args) if action_defined?(action, controller)
end

#in_memoryHash<void> (private)

Returns an empty collection ready for the storing of client application controllers and actions.

Returns:

  • (Hash<void>)


165
166
167
# File 'lib/vedeu/runtime/router.rb', line 165

def in_memory
  {}
end

#klass_defined?(controller) ⇒ Boolean (private)

Returns a boolean indicating whether the given controller name has a class defined.

Parameters:

  • controller (Symbol)

Returns:



157
158
159
# File 'lib/vedeu/runtime/router.rb', line 157

def klass_defined?(controller)
  present?(storage[controller][:klass])
end

#klass_for(controller) ⇒ String (private)

Fetch the class for the controller by name.

Parameters:

  • controller (Symbol)

Returns:

  • (String)

Raises:



141
142
143
144
145
146
147
148
149
150
# File 'lib/vedeu/runtime/router.rb', line 141

def klass_for(controller)
  if registered?(controller) && klass_defined?(controller)
    storage[controller][:klass]

  else
    raise Vedeu::Error::MissingRequired,
          "Cannot route to #{controller} as no class defined."

  end
end

#registered?(controller) ⇒ Boolean

Returns a boolean indicating whether the given controller name is already registered.

Parameters:

  • controller (Symbol)

Returns:



98
99
100
# File 'lib/vedeu/runtime/router.rb', line 98

def registered?(controller)
  storage.key?(controller)
end

#route(controller, action, **args) ⇒ void (private)

This method returns an undefined value.

Instantiate the given controller by name, the call the action (method) with any given arguments.

Parameters:

  • controller (Symbol)
  • action (Symbol)
  • args (Symbol)


131
132
133
134
# File 'lib/vedeu/runtime/router.rb', line 131

def route(controller, action, **args)
  klass = Object.const_get(klass_for(controller)).new(**args)
  klass.send(action)
end