Class: RedPack::DottedInstanceDispatcher

Inherits:
Object
  • Object
show all
Includes:
MethodDispatcher
Defined in:
lib/redpack-ruby/method-dispatchers/dotted.rb

Instance Method Summary collapse

Constructor Details

#initialize(named_obj_dict, responder, sep = /\./, dispatch_delegate_klass = RedPack::SimpleInstanceDispatcher) ⇒ DottedInstanceDispatcher

Returns a new instance of DottedInstanceDispatcher.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/redpack-ruby/method-dispatchers/dotted.rb', line 8

def initialize(named_obj_dict, responder, 
               sep=/\./,
               dispatch_delegate_klass=RedPack::SimpleInstanceDispatcher)
  #
  @responder = responder
  @targets = {}
  @sep = sep
  named_obj_dict.each do |name, obj|
    @targets[name] = dispatch_delegate_klass.new(obj, @responder)
  end
end

Instance Method Details

#dispatch(ctx, method, params) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/redpack-ruby/method-dispatchers/dotted.rb', line 24

def dispatch(ctx, method, params)
  a = self.parse_dotted(method)
  obj_name, method_name = a[0], a[1]
  if @targets.key? obj_name then
    @targets[obj_name].dispatch(ctx, method_name, params)
  else
    @responder.fail(ctx, "no-such-object-named: #{obj_name} / #{method}")
  end
end

#parse_dotted(method) ⇒ Object



20
21
22
# File 'lib/redpack-ruby/method-dispatchers/dotted.rb', line 20

def parse_dotted(method)
  return method.split(@sep)
end