Class: RKS::Support::Routable::Router

Inherits:
Object
  • Object
show all
Defined in:
lib/rks/support/routable.rb

Defined Under Namespace

Classes: RouteNotFound

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(owner:) ⇒ Router

Returns a new instance of Router.



19
20
21
22
# File 'lib/rks/support/routable.rb', line 19

def initialize(owner:)
  @owner = owner
  @routes = {}
end

Instance Attribute Details

#ownerObject

Returns the value of attribute owner.



17
18
19
# File 'lib/rks/support/routable.rb', line 17

def owner
  @owner
end

#routesObject

Returns the value of attribute routes.



17
18
19
# File 'lib/rks/support/routable.rb', line 17

def routes
  @routes
end

Instance Method Details

#draw {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



32
33
34
# File 'lib/rks/support/routable.rb', line 32

def draw
  yield(self)
end

#find(name) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/rks/support/routable.rb', line 24

def find(name)
  if route = routes[name]
    route
  else
    raise RouteNotFound, "#{name} is not found in #{owner} routes"
  end
end

#on(name, to:, options: {}) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/rks/support/routable.rb', line 36

def on(name, to:, options: {})
  klass_name, action = to.split('#')
  klass = Object.const_get(klass_name)

  block = Proc.new { |correlation_id, payload| klass.new(correlation_id: correlation_id, payload: payload).send(action.to_sym) }
  routes.merge!({name => {block: block, options: options}})
end