Sha256: e6eae2535b56fec50a1a39c300041e1df50e290eaa6bda5ee0623770381c8b53

Contents?: true

Size: 1.33 KB

Versions: 9

Compression:

Stored size: 1.33 KB

Contents

# frozen_string_literal: true

module Qonfig
  # @api private
  # @since 0.1.0
  class CommandSet
    # @return [Array<Qonfig::Commands::Base>]
    #
    # @api private
    # @since 0.1.0
    attr_reader :commands

    # @api private
    # @since 0.1.0
    def initialize
      @commands = []
      @access_lock = Mutex.new
    end

    # @param command [Qonfig::Commands::Base]
    # @return [void]
    #
    # @api private
    # @since 0.1.0
    def add_command(command)
      thread_safe { commands << command }
    end
    alias_method :<<, :add_command

    # @param block [Proc]
    # @return [Enumerable]
    #
    # @api private
    # @since 0.1.0
    def each(&block)
      thread_safe { block_given? ? commands.each(&block) : commands.each }
    end

    # @param command_set [Qonfig::CommandSet]
    # @return [void]
    #
    # @api private
    # @since 0.1.0
    def concat(command_set)
      thread_safe { commands.concat(command_set.commands) }
    end

    # @return [Qonfig::CommandSet]
    #
    # @api private
    # @since 0.2.0
    def dup
      thread_safe do
        self.class.new.tap { |duplicate| duplicate.concat(self) }
      end
    end

    private

    # @param block [Proc]
    # @return [Object]
    #
    # @api private
    # @since 0.2.0
    def thread_safe(&block)
      @access_lock.synchronize(&block)
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
qonfig-0.10.0 lib/qonfig/command_set.rb
qonfig-0.9.0 lib/qonfig/command_set.rb
qonfig-0.8.0 lib/qonfig/command_set.rb
qonfig-0.7.0 lib/qonfig/command_set.rb
qonfig-0.6.0 lib/qonfig/command_set.rb
qonfig-0.5.0 lib/qonfig/command_set.rb
qonfig-0.4.0 lib/qonfig/command_set.rb
qonfig-0.3.0 lib/qonfig/command_set.rb
qonfig-0.2.0 lib/qonfig/command_set.rb