Class: Windoo::BaseClasses::ArrayManager
- Inherits:
-
Object
- Object
- Windoo::BaseClasses::ArrayManager
- Defined in:
- lib/windoo/base_classes/array_manager.rb
Overview
The common code for dealing with a managed array of objects in Software Titles.
Array Managers manage an Array of instances of API objects, preventing direct access to the Array, but providing methods for adding, removing, updating, and moving array members, while appropriatly interacting with the API and maintaining consistency between the local Array and the server.
This base class provides management of the actual Array, and doesn’t intentionally communicate with the server at all. However, it may cause server interaction when calling methods on the objects held in the Array.
Instances of subclasses of this class are held by API objects instead of the raw Array.
For example, SoftwareTitles have a #patches method, which is a list of all the patches for the title. In the raw API data Hash, the :patches key contains an array of hashes of patch data.
However, the SoftwareTitle#patches method does not return an Array. Intead it returns an instance of Windoo::PatchManager, a subclass of this class, which provides ways to add, update, and delete patches from the title.
CAUTION: Do not instantiate (with .create) or delete members of the Array directly, use the ‘add_*` and `delete_` methods of the Array Manager, so that the local array automatically stays in sync with the server.
TODO: Prevent instantiation of those objects outside of approved methods.
Subclasses MUST define the constant MEMBER_CLASS to indicate the class of the items we are managing
Direct Known Subclasses
Constant Summary collapse
- PP_OMITTED_INST_VARS =
Constants
%i[@container].freeze
Instance Attribute Summary collapse
-
#container ⇒ APICollection
readonly
The API object that contains this manager.
Instance Method Summary collapse
- #[](idx) ⇒ Object
- #each(&block) ⇒ Array
- #empty? ⇒ Boolean
- #find(if_none = nil, &block) ⇒ Object?
- #find_by_attr(attr_name, value) ⇒ Object?
- #first ⇒ Object
- #index(obj = nil, &block) ⇒ Integer?
-
#initialize(data, container:) ⇒ ArrayManager
constructor
A new instance of ArrayManager.
- #last ⇒ Object
-
#pretty_print_instance_variables ⇒ Array
Only selected items are displayed with prettyprint otherwise its too much data in irb.
- #size ⇒ Integer (also: #count, #length)
-
#to_a ⇒ Array<Windoo::BaseClasses::Criterion>
A dup’d and frozen copy of the array of criteria maintained by this class.
Constructor Details
#initialize(data, container:) ⇒ ArrayManager
Returns a new instance of ArrayManager.
71 72 73 74 75 76 77 78 79 |
# File 'lib/windoo/base_classes/array_manager.rb', line 71 def initialize(data, container:) @container = container @managed_array = [] return unless data @managed_array = data.map do |member_data| self.class::MEMBER_CLASS.instantiate_from_container(container: container, **member_data) end end |
Instance Attribute Details
#container ⇒ APICollection (readonly)
Returns the API object that contains this manager.
59 60 61 |
# File 'lib/windoo/base_classes/array_manager.rb', line 59 def container @container end |
Instance Method Details
#[](idx) ⇒ Object
109 110 111 |
# File 'lib/windoo/base_classes/array_manager.rb', line 109 def [](idx) @managed_array[idx] end |
#each(&block) ⇒ Array
147 148 149 |
# File 'lib/windoo/base_classes/array_manager.rb', line 147 def each(&block) to_a.each(&block) end |
#empty? ⇒ Boolean
127 128 129 |
# File 'lib/windoo/base_classes/array_manager.rb', line 127 def empty? @managed_array.empty? end |
#find(if_none = nil, &block) ⇒ Object?
153 154 155 |
# File 'lib/windoo/base_classes/array_manager.rb', line 153 def find(if_none = nil, &block) to_a.find if_none, &block end |
#find_by_attr(attr_name, value) ⇒ Object?
159 160 161 162 163 164 |
# File 'lib/windoo/base_classes/array_manager.rb', line 159 def find_by_attr(attr_name, value) return if empty? return unless @managed_array.first.respond_to? attr_name @managed_array.find { |i| i.send(attr_name) == value } end |
#first ⇒ Object
115 116 117 |
# File 'lib/windoo/base_classes/array_manager.rb', line 115 def first @managed_array.first end |
#index(obj = nil, &block) ⇒ Integer?
168 169 170 171 172 |
# File 'lib/windoo/base_classes/array_manager.rb', line 168 def index(obj = nil, &block) return to_a.index(obj) if obj to_a.index(&block) end |
#last ⇒ Object
121 122 123 |
# File 'lib/windoo/base_classes/array_manager.rb', line 121 def last @managed_array.last end |
#pretty_print_instance_variables ⇒ Array
Only selected items are displayed with prettyprint otherwise its too much data in irb.
91 92 93 |
# File 'lib/windoo/base_classes/array_manager.rb', line 91 def pretty_print_instance_variables instance_variables - PP_OMITTED_INST_VARS end |
#size ⇒ Integer Also known as: count, length
133 134 135 |
# File 'lib/windoo/base_classes/array_manager.rb', line 133 def size @managed_array.size end |
#to_a ⇒ Array<Windoo::BaseClasses::Criterion>
Returns A dup’d and frozen copy of the array of criteria maintained by this class.
98 99 100 |
# File 'lib/windoo/base_classes/array_manager.rb', line 98 def to_a @managed_array.dup.freeze end |