Sha256: 8774d313dfb14da54ecb9fbff08f40ee1950537a10d079f054a4ef0131e68f34
Contents?: true
Size: 1008 Bytes
Versions: 2
Compression:
Stored size: 1008 Bytes
Contents
module Commandos class IAmACommandRegistry def initialize(storage: DefaultStorageStrategy) @storage = storage end def [](command) handlers.fetch(storage_strategy.call command) { NullHandler }.tap do |handler| yield handler if block_given? end end alias_method :find_by, :[] protected def handlers @handlers ||= {} end def storage_strategy @storage ||= DefaultStorageStrategy end def register(command, handler: nil) unless handler.nil? handlers[storage_strategy.call command] = handler end end end DefaultStorageStrategy = ->(command) do return command.class unless command.kind_of? Class command end class NullHandler def initialize(command) @command = command end def call puts "-" * 65 puts " NO HANDLER REGISTERED FOR #{command.class.upcase}" puts "-" * 65 puts command.inspect end private attr_reader :command end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
commandos-0.1.5 | lib/commandos/registry.rb |
commandos-0.1.4 | lib/commandos/registry.rb |