Class: Clamour::Registry
- Inherits:
-
Object
- Object
- Clamour::Registry
- Defined in:
- lib/clamour/registry.rb
Instance Attribute Summary collapse
-
#handlers ⇒ Hash<Set<Class>>
readonly
Store handler classes.
- #message_classes ⇒ Hash readonly
Instance Method Summary collapse
- #change(&block) ⇒ Object
-
#initialize(&block) ⇒ Registry
constructor
A new instance of Registry.
- #on(mappings = {}) ⇒ Object
- #route(type, &block) ⇒ Object
Constructor Details
#initialize(&block) ⇒ Registry
Returns a new instance of Registry.
12 13 14 15 16 |
# File 'lib/clamour/registry.rb', line 12 def initialize(&block) @handlers = Hash.new { |mapping, type| mapping[type] = Set.new } @message_classes = Hash.new change(&block) if block_given? end |
Instance Attribute Details
#handlers ⇒ Hash<Set<Class>> (readonly)
Returns store handler classes.
7 8 9 |
# File 'lib/clamour/registry.rb', line 7 def handlers @handlers end |
#message_classes ⇒ Hash (readonly)
10 11 12 |
# File 'lib/clamour/registry.rb', line 10 def @message_classes end |
Instance Method Details
#change(&block) ⇒ Object
31 32 33 34 |
# File 'lib/clamour/registry.rb', line 31 def change(&block) instance_eval(&block) self end |
#on(mappings = {}) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/clamour/registry.rb', line 36 def on(mappings = {}) mappings.each do |, handlers_list| raise ArgumentError.new("#{} must include Clamour::Message") unless < Clamour::Message Array.wrap(handlers_list).flatten.compact.each do |handler| raise ArgumentError.new("Handler #{handler} must be a class") unless handler.is_a?(Class) = .type [] = handlers[].add(handler) end end end |
#route(type, &block) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/clamour/registry.rb', line 18 def route(type, &block) raise ArgumentError.new('Something has to be routed') if type.blank? = [type] found_handlers = handlers[type] if .present? && found_handlers.present? found_handlers.each do |handler| block.call(handler, ) end else puts "Could not find message class or handler for #{type}" end end |