Sha256: 63c7c9a67b126c49bf5acf529ebed4f83f697608faea926077998d49e55f5a13

Contents?: true

Size: 1.46 KB

Versions: 1

Compression:

Stored size: 1.46 KB

Contents

# frozen_string_literal: true

# @api private
# @since 0.1.0
class SmartCore::Validator::AttributeSet
  # @since 0.1.0
  include Enumerable

  # @return [Hash<Symbol, SmartCore::Validator::Attribute>]
  #
  # @api private
  # @since 0.1.0
  attr_reader :attributes

  # @return [void]
  #
  # @api private
  # @since 0.1.0
  def initialize
    @attributes = {}
    @access_lock = Mutex.new
  end

  # @param attribute [Symbiont::Validator::Attribute]
  # @return [void]
  #
  # @api private
  # @since 0.1.0
  def add_attribute(attribute)
    thread_safe { attributes[attribute.name] = attribute }
  end
  alias_method :<<, :add_attribute

  # @param attribute_set [SmartCore::Validator::AttributeSet]
  # @return [void]
  #
  # @api private
  # @sinec 0.1.0
  def concat(attribute_set)
    thread_safe { attributes.merge!(attribute_set.dup.attributes) }
  end

  # @return [SmartCore::Validator::AttributeSet]
  #
  # @api private
  # @since 0.1.0
  def dup
    thread_safe do
      self.class.new.tap do |duplicate|
        attributes.each_value do |attribute|
          duplicate.add_attribute(attribute.dup)
        end
      end
    end
  end

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

  private

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
smart_core-0.1.0 lib/smart_core/validator/attribute_set.rb