Class: VirtualBox::Proxies::Collection
- Inherits:
-
Array
- Object
- Array
- VirtualBox::Proxies::Collection
- 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
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
Instance Method Summary collapse
- #<<(item) ⇒ Object
- #clear ⇒ Object
-
#create(*args) ⇒ Object
Creates a new item for this collection and returns the instance.
- #delete(item, no_callback = false) ⇒ Object
-
#errors ⇒ Object
Returns the errors associated with all the items in this collection.
-
#initialize(parent, item_klass = nil, *args) ⇒ Collection
constructor
A new instance of Collection.
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
#parent ⇒ Object (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 |
#clear ⇒ Object
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 |
#errors ⇒ Object
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 |