Module: Fog::Hyperv::ModelExtends

Included in:
Compute::Server, Model
Defined in:
lib/fog/hyperv/model.rb

Instance Method Summary collapse

Instance Method Details

#collection(name, collection_name = nil, options = {}) ⇒ Object

Attach a collection of sub-models

Parameters:

  • name (Symbol)

    the attribute name to store the collection under

  • collection_name (Symbol) (defaults to: nil)

    the name of the collection on the service

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

    the options to create the collection attachment with

See Also:



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/fog/hyperv/model.rb', line 10

def collection(name, collection_name = nil, options = {}) # rubocop:disable Style/OptionHash -- upstream design
  collection_name ||= name
  Fog::Hyperv::Associations::Collection.new(self, name, collection_name, options)

  case to_s
  when 'Fog::Hyperv::Compute::Server'
    define_method name do
      associations[name] ||= service.send(collection_name, vm: self, computer_name:, vm_id: id)
    end
  when 'Fog::Hyperv::Compute::Host'
    define_method name do
      associations[name] ||= service.send(collection_name, computer: self, computer_name:)
    end
  else
    raise "Unknown class #{self}"
  end

  define_method :"#{name}=" do |data|
    assoc = associations[name] || send(name)
    assoc.clear
    assoc.instance_variable_set :@loaded, true
    [data].flatten.each do |obj|
      obj = assoc.new obj if obj.is_a?(Hash)
      assoc << obj
    end
    assoc
  end
end