Class: Windoo::BaseClasses::ArrayManager

Inherits:
Object
  • Object
show all
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

CriteriaManager, KillAppManager, PatchManager

Constant Summary collapse

PP_OMITTED_INST_VARS =

Constants

%i[@container].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, container:) ⇒ ArrayManager

Returns a new instance of ArrayManager.

Parameters:

  • data (Array<Hash>)

    A JSON array of hashes from the API containing data the to construct one of these manager objects.

  • container (Object)

    the object that contains this managed array of criteria



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

#containerAPICollection (readonly)

Returns the API object that contains this manager.

Returns:

  • (APICollection)

    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

Returns:

  • (Object)


109
110
111
# File 'lib/windoo/base_classes/array_manager.rb', line 109

def [](idx)
  @managed_array[idx]
end

#each(&block) ⇒ Array

Returns:

  • (Array)


147
148
149
# File 'lib/windoo/base_classes/array_manager.rb', line 147

def each(&block)
  to_a.each(&block)
end

#empty?Boolean

Returns:

  • (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?

Returns:

  • (Object, nil)


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?

Returns:

  • (Object, nil)


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

#firstObject

Returns:

  • (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?

Returns:

  • (Integer, nil)


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

#lastObject

Returns:

  • (Object)


121
122
123
# File 'lib/windoo/base_classes/array_manager.rb', line 121

def last
  @managed_array.last
end

#pretty_print_instance_variablesArray

Only selected items are displayed with prettyprint otherwise its too much data in irb.

Returns:

  • (Array)

    the desired instance_variables



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

#sizeInteger Also known as: count, length

Returns:

  • (Integer)


133
134
135
# File 'lib/windoo/base_classes/array_manager.rb', line 133

def size
  @managed_array.size
end

#to_aArray<Windoo::BaseClasses::Criterion>

Returns A dup’d and frozen copy of the array of criteria maintained by this class.

Returns:



98
99
100
# File 'lib/windoo/base_classes/array_manager.rb', line 98

def to_a
  @managed_array.dup.freeze
end