Class: Tamashii::Resolver

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.instanceObject



11
12
13
# File 'lib/tamashii/resolver.rb', line 11

def instance
  @instance ||= self.new
end

.method_missing(name, *args, &block) ⇒ Object



7
8
9
# File 'lib/tamashii/resolver.rb', line 7

def method_missing(name, *args, &block)
  self.instance.send(name, *args, &block)
end

Instance Method Details

#config(&block) ⇒ Object



24
25
26
# File 'lib/tamashii/resolver.rb', line 24

def config(&block)
  instance_eval(&block)
end

#default_handler(handler_class = nil, env = {}) ⇒ Object



28
29
30
31
# File 'lib/tamashii/resolver.rb', line 28

def default_handler(handler_class = nil, env = {})
  return @default_handler || [nil, {}] if handler_class.nil?
  @default_handler = [handler_class, env]
end

#handle(type, handler_class, options = {}) ⇒ Object

Raises:

  • (NotImplementedError)


33
34
35
36
# File 'lib/tamashii/resolver.rb', line 33

def handle(type, handler_class, options = {})
  raise NotImplementedError.new("Handler should implement resolve method") unless handler_class.method_defined?(:resolve)
  handlers[type] = [handler_class, options]
end

#handle?(type) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/tamashii/resolver.rb', line 55

def handle?(type)
  handlers.has_key? type
end

#handlersObject



16
17
18
# File 'lib/tamashii/resolver.rb', line 16

def handlers
  @handlers ||= {}
end

#hook(hook_class, env = {}) ⇒ Object

Raises:

  • (NotImplementedError)


38
39
40
41
# File 'lib/tamashii/resolver.rb', line 38

def hook(hook_class, env = {})
  raise NotImplementedError.new("Hook should implement call method") unless hook_class.method_defined?(:call)
  hooks << [hook_class, env]
end

#hooksObject



20
21
22
# File 'lib/tamashii/resolver.rb', line 20

def hooks
  @hooks ||= []
end

#resolve(pkt, env = {}) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/tamashii/resolver.rb', line 43

def resolve(pkt, env = {})
  hooks.each do  |hook_data|
    hook_class, hook_env = hook_data
    if hook_class.new(hook_env.merge(env)).call(pkt)
      # terminates the procedure
      return
    end
  end
  handler, options = handlers[pkt.type] || @default_handler
  handler.new(pkt.type, options.merge(env)).resolve(pkt.body) if handler
end