Module: Fog::Hyperv::ModelExtends
- Included in:
- Compute::Server, Model
- Defined in:
- lib/fog/hyperv/model.rb
Instance Method Summary collapse
-
#collection(name, collection_name = nil, options = {}) ⇒ Object
Attach a collection of sub-models.
Instance Method Details
#collection(name, collection_name = nil, options = {}) ⇒ Object
Attach a collection of sub-models
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, = {}) # rubocop:disable Style/OptionHash -- upstream design collection_name ||= name Fog::Hyperv::Associations::Collection.new(self, name, collection_name, ) 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 |