Class: SmartCore::Initializer::Attribute::List Private
- Inherits:
-
Object
- Object
- SmartCore::Initializer::Attribute::List
- 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.
Instance Method Summary collapse
- #add(attribute) ⇒ void (also: #<<) private
- #concat(list) ⇒ void private
- #count(&block) ⇒ Integer private
- #each(&block) ⇒ Enumerable private
- #fetch(attribute_name) ⇒ SmartCore::Initializer::Atribute (also: #[]) private
- #include?(attribute) ⇒ void private
- #initialize ⇒ void constructor private
-
#names ⇒ Array<Symbol>
private
Names of all attributes in the list.
-
#required_option_names ⇒ Array<Symbol>
private
Names of options that must be provided explicitly.
- #size ⇒ Integer private
Constructor Details
#initialize ⇒ 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.
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.
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.
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.
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.
Lock-free: iterates the immutable snapshot maintained on mutation.
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.
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.
101 102 103 |
# File 'lib/smart_core/initializer/attribute/list.rb', line 101 def include?(attribute) @lock.read_sync { attributes.key?(attribute.name) } end |
#names ⇒ Array<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.
Lock-free, memoized on mutation. Used on the instantiation path.
Returns names of all attributes in the list.
68 69 70 |
# File 'lib/smart_core/initializer/attribute/list.rb', line 68 def names @names end |
#required_option_names ⇒ Array<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.
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.
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 |
#size ⇒ 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.
Lock-free: reads the size of the immutable snapshot.
124 125 126 |
# File 'lib/smart_core/initializer/attribute/list.rb', line 124 def size @snapshot.size end |