Class: InventoryRefresh::TargetCollection
- Inherits:
-
Object
- Object
- InventoryRefresh::TargetCollection
- Defined in:
- lib/inventory_refresh/target_collection.rb
Instance Attribute Summary collapse
-
#targets ⇒ Object
readonly
Returns the value of attribute targets.
Instance Method Summary collapse
- #add_target(association:, manager_ref:, manager: nil, manager_id: nil, event_id: nil, options: {}) ⇒ Object
-
#id ⇒ String
A String containing an id of each target in the TargetCollection.
-
#initialize(manager: nil, manager_id: nil, event: nil, targets: []) ⇒ TargetCollection
constructor
A new instance of TargetCollection.
-
#manager_refs_by_association ⇒ Object
Returns targets in a format: { :vms => => Set.new([“vm_ref_1”, “vm_ref2”]), :network_ports => {:ems_ref => Set.new([“network_port_1”, “network_port2”]) }.
-
#manager_refs_by_association_reset ⇒ Object
Resets the cached @manager_refs_by_association to enforce reload when calling :manager_refs_by_association method.
-
#name ⇒ String
A String containing a summary.
-
#name_references(collection) ⇒ Array<String>
Returns list of names.
-
#references(collection) ⇒ Array<String>
Returns list of ems_refs.
Constructor Details
#initialize(manager: nil, manager_id: nil, event: nil, targets: []) ⇒ TargetCollection
Returns a new instance of TargetCollection.
14 15 16 17 18 19 |
# File 'lib/inventory_refresh/target_collection.rb', line 14 def initialize(manager: nil, manager_id: nil, event: nil, targets: []) @manager = manager @manager_id = manager_id @event = event @targets = targets end |
Instance Attribute Details
#targets ⇒ Object (readonly)
Returns the value of attribute targets.
5 6 7 |
# File 'lib/inventory_refresh/target_collection.rb', line 5 def targets @targets end |
Instance Method Details
#add_target(association:, manager_ref:, manager: nil, manager_id: nil, event_id: nil, options: {}) ⇒ Object
29 30 31 32 33 34 35 36 |
# File 'lib/inventory_refresh/target_collection.rb', line 29 def add_target(association:, manager_ref:, manager: nil, manager_id: nil, event_id: nil, options: {}) self << InventoryRefresh::Target.new(:association => association, :manager_ref => manager_ref, :manager => manager || @manager, :manager_id => manager_id || @manager_id || @manager.try(:id), :event_id => event_id || @event.try(:id), :options => ) end |
#id ⇒ String
Returns A String containing an id of each target in the TargetCollection.
44 45 46 |
# File 'lib/inventory_refresh/target_collection.rb', line 44 def id "Collection of targets with id: #{targets.collect(&:name)}" end |
#manager_refs_by_association ⇒ Object
Returns targets in a format:
{
:vms => {:ems_ref => Set.new(["vm_ref_1", "vm_ref2"])},
:network_ports => {:ems_ref => Set.new(["network_port_1", "network_port2"])
}
Then we can quickly access all objects affected by:
NetworkPort.where(target_collection.manager_refs_by_association[:network_ports].to_a) =>
return AR objects with ems_refs ["network_port_1", "network_port2"]
And we can get a list of ids for the API query by:
target_collection.manager_refs_by_association[:network_ports][:ems_ref].to_a =>
["network_port_1", "network_port2"]
Only targets of a type InventoryRefresh::Target are processed, any other targets present should be converted to InventoryRefresh::Target, e.g. in the Inventory::Collector code.
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/inventory_refresh/target_collection.rb', line 63 def manager_refs_by_association @manager_refs_by_association ||= targets.select { |x| x.kind_of?(InventoryRefresh::Target) }.each_with_object({}) do |x, obj| if obj[x.association].blank? obj[x.association] = x.manager_ref.each_with_object({}) { |(key, value), hash| hash[key] = Set.new([value]) } else obj[x.association].each do |key, value| value << x.manager_ref[key] end end end end |
#manager_refs_by_association_reset ⇒ Object
Resets the cached @manager_refs_by_association to enforce reload when calling :manager_refs_by_association method
76 77 78 |
# File 'lib/inventory_refresh/target_collection.rb', line 76 def manager_refs_by_association_reset @manager_refs_by_association = nil end |
#name ⇒ String
Returns A String containing a summary.
39 40 41 |
# File 'lib/inventory_refresh/target_collection.rb', line 39 def name "Collection of #{targets.size} targets" end |
#name_references(collection) ⇒ Array<String>
Returns list of names
88 89 90 |
# File 'lib/inventory_refresh/target_collection.rb', line 88 def name_references(collection) manager_refs_by_association.try(:[], collection).try(:[], :name)&.to_a || [] end |
#references(collection) ⇒ Array<String>
Returns list of ems_refs
82 83 84 |
# File 'lib/inventory_refresh/target_collection.rb', line 82 def references(collection) manager_refs_by_association.try(:[], collection).try(:[], :ems_ref)&.to_a || [] end |