Sha256: ed6dbb3e7935e702a2adb7a503bd86e32932f6ea2811859dfb06f6b4a844ff2a

Contents?: true

Size: 1.42 KB

Versions: 18

Compression:

Stored size: 1.42 KB

Contents

module VirtualBox
  module Proxies
    # A relationship which can be described as a collection, which
    # is a set of items.
    class Collection < Array
      attr_reader :parent

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

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

      # 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.
      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

      # Returns the errors associated with all the items in this
      # collection
      def errors
        collect do |item|
          item.respond_to?(:errors) ? item.errors : {}
        end
      end

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

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

      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
    end
  end
end

Version data entries

18 entries across 18 versions & 3 rubygems

Version Path
virtualbox-0.9.2 lib/virtualbox/proxies/collection.rb
virtualbox-0.9.1 lib/virtualbox/proxies/collection.rb
virtualbox-0.9.0 lib/virtualbox/proxies/collection.rb
virtualbox-0.8.6 lib/virtualbox/proxies/collection.rb
virtualbox-0.8.5 lib/virtualbox/proxies/collection.rb
virtualbox-0.8.4 lib/virtualbox/proxies/collection.rb
velir_virtualbox-0.8.4 lib/virtualbox/proxies/collection.rb
virtualbox-0.8.3 lib/virtualbox/proxies/collection.rb
virtualbox-0.8.2 lib/virtualbox/proxies/collection.rb
virtualbox-0.8.1 lib/virtualbox/proxies/collection.rb
virtualbox-0.8.0 lib/virtualbox/proxies/collection.rb
virtualbox-0.7.9 lib/virtualbox/proxies/collection.rb
virtualbox-0.7.8 lib/virtualbox/proxies/collection.rb
virtualbox-0.7.7 lib/virtualbox/proxies/collection.rb
virtualbox-0.7.6 lib/virtualbox/proxies/collection.rb
bbrowning-virtualbox-0.7.6.dev lib/virtualbox/proxies/collection.rb
virtualbox-0.7.5 lib/virtualbox/proxies/collection.rb
virtualbox-0.7.4 lib/virtualbox/proxies/collection.rb