Class: VirtualBox::Proxies::Collection

Inherits:
Array
  • Object
show all
Defined in:
lib/virtualbox/proxies/collection.rb

Overview

A relationship which can be described as a collection, which is a set of items.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, item_klass = nil, *args) ⇒ Collection

Returns a new instance of Collection.



8
9
10
11
12
13
14
# File 'lib/virtualbox/proxies/collection.rb', line 8

def initialize(parent, item_klass=nil, *args)
  super()

  @parent = parent
  @item_klass = item_klass
  @other = args
end

Instance Attribute Details

#parentObject (readonly)

Returns the value of attribute parent.



6
7
8
# File 'lib/virtualbox/proxies/collection.rb', line 6

def parent
  @parent
end

Instance Method Details

#<<(item) ⇒ Object



40
41
42
43
# File 'lib/virtualbox/proxies/collection.rb', line 40

def <<(item)
  item.added_to_relationship(self) if item.respond_to?(:added_to_relationship)
  push(item)
end

#clearObject



45
46
47
48
49
# File 'lib/virtualbox/proxies/collection.rb', line 45

def clear
  each do |item|
    delete(item)
  end
end

#create(*args) ⇒ Object

Creates a new item for this collection and returns the instance. The item is automatically put into this collection. ‘create` happens immediately, meaning that even without a `save`, the item will already exist.



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/virtualbox/proxies/collection.rb', line 20

def create(*args)
  item =  nil

  if @item_klass.respond_to?(:create)
    args = @other + args
    item = @item_klass.create(self, *args)
    self << item
  end

  item
end

#delete(item, no_callback = false) ⇒ Object



51
52
53
54
# File 'lib/virtualbox/proxies/collection.rb', line 51

def delete(item, no_callback=false)
  return unless super(item)
  item.removed_from_relationship(self) if !no_callback && item.respond_to?(:removed_from_relationship)
end

#errorsObject

Returns the errors associated with all the items in this collection



34
35
36
37
38
# File 'lib/virtualbox/proxies/collection.rb', line 34

def errors
  collect do |item|
    item.respond_to?(:errors) ? item.errors : {}
  end
end