Sha256: c17892e5746c1bd6c9e88566a2955f323e0da5308f5033bffcf782f2149d7415

Contents?: true

Size: 1.67 KB

Versions: 13

Compression:

Stored size: 1.67 KB

Contents

# frozen_string_literal: true

# @api private
# @since 0.1.0
class SmartCore::Initializer::Attribute::List
  # @since 0.1.0
  include Enumerable

  # @return [void]
  #
  # @api private
  # @since 0.1.0
  def initialize
    @attributes = {}
    @lock = SmartCore::Engine::Lock.new
  end

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

  # @param list [SmartCore::Initializer::Attribute::List]
  # @return [void]
  #
  # @api private
  # @since 0.1.0
  def concat(list)
    thread_safe do
      list.each { |attribute| add(attribute.dup) }
    end
  end

  # @param attribute [SmartCore::Initializer::Attribute]
  # @return [void]
  #
  # @api private
  # @since 0.1.0
  def include?(attribute)
    thread_safe { attributes.key?(attribute.name) }
  end

  # @param block [Block]
  # @return [Enumerable]
  #
  # @api private
  # @since 0.1.0
  def each(&block)
    thread_safe do
      block_given? ? attributes.values.each(&block) : attributes.values.each
    end
  end

  # @return [Integer]
  #
  # @api private
  # @since 0.1.0
  def size
    thread_safe { attributes.size }
  end

  # @param block [Block]
  # @return [Integer]
  #
  # @api private
  # @since 0.1.0
  def count(&block)
    thread_safe { attributes.values.count(&block) }
  end

  private

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

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

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
smart_initializer-0.7.0 lib/smart_core/initializer/attribute/list.rb
smart_initializer-0.6.0 lib/smart_core/initializer/attribute/list.rb
smart_initializer-0.5.0 lib/smart_core/initializer/attribute/list.rb
smart_initializer-0.4.0 lib/smart_core/initializer/attribute/list.rb
smart_initializer-0.3.2 lib/smart_core/initializer/attribute/list.rb
smart_initializer-0.3.1 lib/smart_core/initializer/attribute/list.rb
smart_initializer-0.3.0 lib/smart_core/initializer/attribute/list.rb
smart_initializer-0.2.0 lib/smart_core/initializer/attribute/list.rb
smart_initializer-0.1.0 lib/smart_core/initializer/attribute/list.rb
smart_initializer-0.1.0.alpha4 lib/smart_core/initializer/attribute/list.rb
smart_initializer-0.1.0.alpha3 lib/smart_core/initializer/attribute/list.rb
smart_initializer-0.1.0.alpha2 lib/smart_core/initializer/attribute/list.rb
smart_initializer-0.1.0.alpha1 lib/smart_core/initializer/attribute/list.rb