Class: Tamashii::Resolver
- Inherits:
-
Object
- Object
- Tamashii::Resolver
- Defined in:
- lib/tamashii/resolver.rb
Class Method Summary collapse
Instance Method Summary collapse
- #config(&block) ⇒ Object
- #default_handler(handler_class = nil, env = {}) ⇒ Object
- #handle(type, handler_class, options = {}) ⇒ Object
- #handle?(type) ⇒ Boolean
- #handlers ⇒ Object
- #hook(hook_class, env = {}) ⇒ Object
- #hooks ⇒ Object
- #resolve(pkt, env = {}) ⇒ Object
Class Method Details
.instance ⇒ Object
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
33 34 35 36 |
# File 'lib/tamashii/resolver.rb', line 33 def handle(type, handler_class, = {}) raise NotImplementedError.new("Handler should implement resolve method") unless handler_class.method_defined?(:resolve) handlers[type] = [handler_class, ] end |
#handle?(type) ⇒ Boolean
55 56 57 |
# File 'lib/tamashii/resolver.rb', line 55 def handle?(type) handlers.has_key? type end |
#handlers ⇒ Object
16 17 18 |
# File 'lib/tamashii/resolver.rb', line 16 def handlers @handlers ||= {} end |
#hook(hook_class, env = {}) ⇒ Object
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 |
#hooks ⇒ Object
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, = handlers[pkt.type] || @default_handler handler.new(pkt.type, .merge(env)).resolve(pkt.body) if handler end |