lib/dry/cli/registry.rb in dry-cli-0.6.0 vs lib/dry/cli/registry.rb in dry-cli-0.7.0

- old
+ new

@@ -1,8 +1,8 @@ # frozen_string_literal: true -require 'dry/cli/command_registry' +require "dry/cli/command_registry" module Dry class CLI # Registry mixin # @@ -10,10 +10,11 @@ module Registry # @since 0.1.0 # @api private def self.extended(base) base.class_eval do + @_mutex = Mutex.new @commands = CommandRegistry.new end end # Register a command @@ -73,19 +74,19 @@ # prefix.register "action", Generate::Action # end # end # end def register(name, command = nil, aliases: [], &block) + @commands.set(name, command, aliases) + if block_given? prefix = Prefix.new(@commands, name, aliases) if block.arity.zero? prefix.instance_eval(&block) else yield(prefix) end - else - @commands.set(name, command, aliases) end end # Register a before callback. # @@ -168,11 +169,13 @@ # register "hello", Hello # before "hello", Callbacks::Hello # end # end def before(command_name, callback = nil, &blk) - command(command_name).before_callbacks.append(&_callback(callback, blk)) + @_mutex.synchronize do + command(command_name).before_callbacks.append(&_callback(callback, blk)) + end end # Register an after callback. # # @param command_name [String] the name used for command registration @@ -254,21 +257,23 @@ # register "hello", Hello # after "hello", Callbacks::World # end # end def after(command_name, callback = nil, &blk) - command(command_name).after_callbacks.append(&_callback(callback, blk)) + @_mutex.synchronize do + command(command_name).after_callbacks.append(&_callback(callback, blk)) + end end # @since 0.1.0 # @api private def get(arguments) @commands.get(arguments) end private - COMMAND_NAME_SEPARATOR = ' ' + COMMAND_NAME_SEPARATOR = " " # @since 0.2.0 # @api private def command(command_name) get(command_name.split(COMMAND_NAME_SEPARATOR)).tap do |result|