Sha256: d89b73e1154e6fc4d8781f890b91a2aef1844da0e3b63843942625d6b10e855a

Contents?: true

Size: 1.2 KB

Versions: 2

Compression:

Stored size: 1.2 KB

Contents

# frozen_string_literal: true

# @api private
# @since 0.1.0
class Qonfig::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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
qonfig-0.12.0 lib/qonfig/command_set.rb
qonfig-0.11.0 lib/qonfig/command_set.rb