Class: SmartCore::Initializer::Attribute::List Private

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/smart_core/initializer/attribute/list.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.0

Version:

  • 0.10.0

Instance Method Summary collapse

Constructor Details

#initializevoid

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.0

Version:

  • 0.12.1



15
16
17
18
19
20
21
22
23
# File 'lib/smart_core/initializer/attribute/list.rb', line 15

def initialize
  @attributes = {}
  # NOTE: frozen snapshot of attribute values, rebuilt on each mutation so the
  #   read-heavy instantiation path (see Constructor) can iterate lock-free.
  @snapshot = [].freeze
  @names = [].freeze
  @required_option_names = nil
  @lock = SmartCore::Engine::ReadWriteLock.new
end

Instance Method Details

#add(attribute) ⇒ void Also known as: <<

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Parameters:

Since:

  • 0.1.0

Version:

  • 0.12.1



52
53
54
55
56
57
58
59
# File 'lib/smart_core/initializer/attribute/list.rb', line 52

def add(attribute)
  @lock.write_sync do
    attributes[attribute.name] = attribute
    @snapshot = attributes.values.freeze
    @names = @snapshot.map(&:name).freeze
    @required_option_names = nil
  end
end

#concat(list) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Parameters:

Since:

  • 0.1.0

Version:

  • 0.10.0



90
91
92
93
94
# File 'lib/smart_core/initializer/attribute/list.rb', line 90

def concat(list)
  @lock.write_sync do
    list.each { |attribute| add(attribute.dup) }
  end
end

#count(&block) ⇒ Integer

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • block (Block)

Returns:

  • (Integer)

Since:

  • 0.1.0

Version:

  • 0.10.0



134
135
136
# File 'lib/smart_core/initializer/attribute/list.rb', line 134

def count(&block)
  @lock.read_sync { attributes.values.count(&block) }
end

#each(&block) ⇒ Enumerable

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

Lock-free: iterates the immutable snapshot maintained on mutation.

Parameters:

  • block (Block)

Returns:

  • (Enumerable)

Since:

  • 0.1.0

Version:

  • 0.12.1



113
114
115
# File 'lib/smart_core/initializer/attribute/list.rb', line 113

def each(&block)
  block_given? ? @snapshot.each(&block) : @snapshot.each
end

#fetch(attribute_name) ⇒ SmartCore::Initializer::Atribute Also known as: []

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • attribute_name (Symbol)

Returns:

  • (SmartCore::Initializer::Atribute)

Raises:

Since:

  • 0.8.0

Version:

  • 0.10.0



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/smart_core/initializer/attribute/list.rb', line 33

def fetch(attribute_name)
  @lock.read_sync do
    raise(
      ::SmartCore::Initializer::UndefinedAttributeError,
      "Attribute with `#{attribute_name}` name is not defined in your constructor. " \
      "Please, check your attribute definitions inside your class."
    ) unless attributes.key?(attribute_name)

    attributes[attribute_name]
  end
end

#include?(attribute) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Parameters:

Since:

  • 0.1.0



101
102
103
# File 'lib/smart_core/initializer/attribute/list.rb', line 101

def include?(attribute)
  @lock.read_sync { attributes.key?(attribute.name) }
end

#namesArray<Symbol>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

Lock-free, memoized on mutation. Used on the instantiation path.

Returns names of all attributes in the list.

Returns:

  • (Array<Symbol>)

    names of all attributes in the list

Since:

  • 0.12.1



68
69
70
# File 'lib/smart_core/initializer/attribute/list.rb', line 68

def names
  @names
end

#required_option_namesArray<Symbol>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

Lock-free, memoized (invalidated on mutation). Only meaningful for an options list — its members respond to #has_default?/#optional?.

Returns names of options that must be provided explicitly.

Returns:

  • (Array<Symbol>)

    names of options that must be provided explicitly

Since:

  • 0.12.1



79
80
81
82
# File 'lib/smart_core/initializer/attribute/list.rb', line 79

def required_option_names
  @required_option_names ||=
    @snapshot.reject(&:has_default?).reject(&:optional?).map(&:name).freeze
end

#sizeInteger

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

Lock-free: reads the size of the immutable snapshot.

Returns:

  • (Integer)

Since:

  • 0.1.0

Version:

  • 0.12.1



124
125
126
# File 'lib/smart_core/initializer/attribute/list.rb', line 124

def size
  @snapshot.size
end