Sha256: bb8ca423c2cb1c60c0bcb0a665ce367689aaa07a0b1c885e0d6371f422612421

Contents?: true

Size: 1.46 KB

Versions: 10

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 [SmartCore::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

10 entries across 10 versions & 1 rubygems

Version Path
smart_core-0.8.1 lib/smart_core/validator/attribute_set.rb
smart_core-0.8.0 lib/smart_core/validator/attribute_set.rb
smart_core-0.7.0 lib/smart_core/validator/attribute_set.rb
smart_core-0.6.0 lib/smart_core/validator/attribute_set.rb
smart_core-0.5.2 lib/smart_core/validator/attribute_set.rb
smart_core-0.5.1 lib/smart_core/validator/attribute_set.rb
smart_core-0.5.0 lib/smart_core/validator/attribute_set.rb
smart_core-0.4.0 lib/smart_core/validator/attribute_set.rb
smart_core-0.3.0 lib/smart_core/validator/attribute_set.rb
smart_core-0.2.0 lib/smart_core/validator/attribute_set.rb