Class: InventoryRefresh::Target

Inherits:
Object
  • Object
show all
Defined in:
lib/inventory_refresh/target.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(association:, manager_ref:, manager: nil, manager_id: nil, event_id: nil, options: {}) ⇒ Target

Returns a new instance of Target.

Parameters:

  • association (Symbol)

    An existing association on Manager, that lists objects represented by a Target, naming should be the same of association of a counterpart InventoryCollection object

  • manager_ref (Hash)

    A Hash that can be used to find_by on a given association and returning a unique object. The keys should be the same as the keys of the counterpart InventoryObject

  • manager (ManageIQ::Providers::BaseManager) (defaults to: nil)

    The Manager owning the Target

  • manager_id (Integer) (defaults to: nil)

    A primary key of the Manager owning the Target

  • event_id (Integer) (defaults to: nil)

    A primary key of the EmsEvent associated with the Target

  • options (Hash) (defaults to: {})

    A free form options hash



13
14
15
16
17
18
19
20
21
22
# File 'lib/inventory_refresh/target.rb', line 13

def initialize(association:, manager_ref:, manager: nil, manager_id: nil, event_id: nil, options: {})
  raise "Provide either :manager or :manager_id argument" if manager.nil? && manager_id.nil?

  @manager     = manager
  @manager_id  = manager_id
  @association = association
  @manager_ref = manager_ref
  @event_id    = event_id
  @options     = options
end

Instance Attribute Details

#associationObject (readonly)

Returns the value of attribute association.



3
4
5
# File 'lib/inventory_refresh/target.rb', line 3

def association
  @association
end

#event_idObject (readonly)

Returns the value of attribute event_id.



3
4
5
# File 'lib/inventory_refresh/target.rb', line 3

def event_id
  @event_id
end

#manager_refObject (readonly) Also known as: name

Returns the value of attribute manager_ref.



3
4
5
# File 'lib/inventory_refresh/target.rb', line 3

def manager_ref
  @manager_ref
end

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'lib/inventory_refresh/target.rb', line 3

def options
  @options
end

Class Method Details

.dump(obj) ⇒ Hash

A Rails recommended interface for serializing an object

Parameters:

Returns:

  • (Hash)

    serialized object



34
35
36
# File 'lib/inventory_refresh/target.rb', line 34

def self.dump(obj)
  obj.dump
end

.load(**args) ⇒ InventoryRefresh::Target

A Rails recommended interface for deserializing an object

Returns:



26
27
28
# File 'lib/inventory_refresh/target.rb', line 26

def self.load(**args)
  new(**args)
end

Instance Method Details

#dumpHash Also known as: id

Returns a serialized InventoryRefresh::Target object. This can be used to initialize a new object, then the object target acts the same as the object InventoryRefresh::Target.new(target.serialize)

Returns:

  • (Hash)

    serialized object



42
43
44
45
46
47
48
49
50
# File 'lib/inventory_refresh/target.rb', line 42

def dump
  {
    :manager_id  => manager_id,
    :association => association,
    :manager_ref => manager_ref,
    :event_id    => event_id,
    :options     => options
  }
end

#load_from_dbApplicationRecord

Loads InventoryRefresh::Target ApplicationRecord representation from our DB, this requires that InventoryRefresh::Target has been refreshed, otherwise the AR object can be missing.

Returns:

  • (ApplicationRecord)

    A InventoryRefresh::Target loaded from the database as AR object



69
70
71
# File 'lib/inventory_refresh/target.rb', line 69

def load_from_db
  manager.public_send(association).find_by(manager_ref)
end

#managerManageIQ::Providers::BaseManager

Returns The Manager owning the Target.

Returns:

  • (ManageIQ::Providers::BaseManager)

    The Manager owning the Target



56
57
58
# File 'lib/inventory_refresh/target.rb', line 56

def manager
  @manager || ManageIQ::Providers::BaseManager.find(@manager_id)
end

#manager_idInteger

Returns A primary key of the Manager owning the Target.

Returns:

  • (Integer)

    A primary key of the Manager owning the Target



61
62
63
# File 'lib/inventory_refresh/target.rb', line 61

def manager_id
  @manager_id || manager.try(:id)
end