Class: SanaRouter

Inherits:
Object
  • Object
show all
Defined in:
lib/sana_router.rb

Overview

Sana Router

Defined Under Namespace

Classes: Events

Instance Method Summary collapse

Constructor Details

#initialize(default_controller = nil) ⇒ SanaRouter

constructor


6
7
8
9
10
11
# File 'lib/sana_router.rb', line 6

def initialize(default_controller = nil)
  @default_controller = default_controller
  @controller = @default_controller
  @route = {}
  @mapping = {}
end

Instance Method Details

#controller(controller) { ... } ⇒ Object

set controller context

Yields:

  • controller context block


71
72
73
74
75
# File 'lib/sana_router.rb', line 71

def controller(controller)
  @controller = controller
  yield if block_given?
  @controller = @default_controller
end

#draw { ... } ⇒ Object

execute register methods in instance context

Yields:

  • instance_exec context


64
65
66
# File 'lib/sana_router.rb', line 64

def draw(&block)
  instance_exec &block
end

#eventsSanaRouter::Events

router events

Examples:

with Sana

sana = Sana.new(router.events)

81
82
83
# File 'lib/sana_router.rb', line 81

def events
  @events ||= Events.new(@route, @mapping, @load_controller, @unload_controller, @default_controller)
end

#loadObject

register SHIORI load() route to controller

Examples:

register to specific controller

router.draw do
  controller DLController do
    load
  end
end

47
48
49
# File 'lib/sana_router.rb', line 47

def load
  @load_controller = @controller
end

#route(events, mapping = {}) ⇒ Object Also known as: r

register event route to controller

Examples:

register to various controllers

router.draw do
  # register to specific controller
  controller BootController do
    route [:OnBoot, :OnFirstBoot]
  end
  controller CloseController do
    route [:OnClose, :OnCloseAll]
  end
  # register to default controller
  route :OnSecondChange
end

mapping usage

route :OnBoot, {shell_name: 0, halted: 6, halted_ghost_name: 7} # mapping for Reference0, 6 and 7

30
31
32
33
34
35
36
# File 'lib/sana_router.rb', line 30

def route(events, mapping = {})
  Array(events).each do |method|
    @route[method.to_sym] = @controller
    @mapping[method.to_sym] = mapping
  end
  return
end

#unloadObject

register SHIORI unload() route to controller

Examples:

register to specific controller

router.draw do
  controller DLController do
    unload
  end
end

58
59
60
# File 'lib/sana_router.rb', line 58

def unload
  @unload_controller = @controller
end