Sha256: b2661c7cc45beb19f9c20ee853dc7c71265101e9247a5ba741d57e464cff580f
Contents?: true
Size: 771 Bytes
Versions: 2
Compression:
Stored size: 771 Bytes
Contents
module ConfCtl module Hook # @param name [Symbol] def self.register(name) @hooks ||= {} @hooks[name] ||= [] end # @param name [Symbol] def self.subscribe(name, &block) subscribers = (@hooks || {})[name] raise "hook #{name.inspect} not registered" if subscribers.nil? subscribers << block nil end # @param name [Symbol] # @param args [Array] # @param kwargs [Hash] # @return [any] def self.call(name, args: [], kwargs: {}) return if @hooks.nil? subscribers = @hooks[name] return if subscribers.empty? subscribers.inject(nil) do |ret, sub| hook_kwargs = kwargs.merge(return_value: ret) sub.call(*args, **hook_kwargs) end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
confctl-2.0.0 | lib/confctl/hook.rb |
confctl-1.0.0 | lib/confctl/hook.rb |