class Occi::Core::Collection

Implements a generic envelope for all OCCI-related instances. This class can be used directly for various reasons or, in a specific way, as an ancestor for custom classes providing `Collection`-like functionality. Its primary purpose is to provide a tool for working with multiple sets of different instance types, aid with their transport and validation.

@attr entities [Set] set of entities associated with this collection instance @attr action_instances [Set] set of action instances associated with this collection instance

@author Boris Parak <parak@cesnet.cz>

Constants

ALL_KEYS
INTERNAL_COLLECTIONS

Public Instance Methods

<<(object) click to toggle source

Auto-assigns the given object to the appropriate internal set. Unknown objects will result in an `ArgumentError` error.

@param object [Object] object to be assigned @return [Occi::Core::Collection] self, for chaining purposes

Calls superclass method
# File lib/occi/core/collection.rb, line 94
def <<(object)
  case object
  when Occi::Core::Entity
    entities << object
  when Occi::Core::ActionInstance
    action_instances << object
  else
    super
  end

  self
end
all() click to toggle source

Collects everything present in this collection and merges it into a single set. This will include categories, entities, and action instances. The resulting set can be used, for example, in conjunction with the `<<` operator to create an independent copy of the collection.

@return [Set] content of this collection as a new `Set` instance

Calls superclass method
# File lib/occi/core/collection.rb, line 25
def all
  super | entities | action_instances
end
empty?() click to toggle source

Reports emptiness of the collection.

@return [TrueClass] if there are no categories, entities or action instances @return [FalseClass] if there are some categories, entities or action instances

# File lib/occi/core/collection.rb, line 151
def empty?
  empties? INTERNAL_COLLECTIONS
end
find_by_action(action) click to toggle source

Collects all `Occi::Core::ActionInstance` instances with the given action.

@param action [Occi::Core::Action] expected action @return [Set] set of found action instances

# File lib/occi/core/collection.rb, line 57
def find_by_action(action)
  raise ArgumentError, 'Action is a mandatory argument' unless action
  filtered_set(action_instances, key: 'action', value: action)
end
find_by_id(id) click to toggle source

Collects all `Occi::Core::Entity` successors with the given ID.

@param id [String] expected ID @return [Set] set of found entities

# File lib/occi/core/collection.rb, line 75
def find_by_id(id)
  filtered_set(entities, key: 'id', value: id)
end
find_by_id!(id) click to toggle source

See `find_by_id`. Returns first found object or raises an error.

@param id [String] expected ID @return [Object] found instance

# File lib/occi/core/collection.rb, line 83
def find_by_id!(id)
  found = entities.detect { |elm| elm.id == id }
  raise Occi::Core::Errors::CollectionLookupError, "Entity #{id.inspect} not found in the collection" unless found
  found
end
find_by_kind(kind) click to toggle source

Collects all `Occi::Core::Entity` successors with the given kind. The resulting set may contain mixed instance types.

@param kind [Occi::Core::Kind] expected kind @return [Set] set of entities with the given kind

# File lib/occi/core/collection.rb, line 48
def find_by_kind(kind)
  raise ArgumentError, 'Kind is a mandatory argument' unless kind
  filtered_set(entities, key: 'kind', value: kind)
end
find_by_mixin(mixin) click to toggle source

Collects all `Occi::Core::Entity` successors associated with the given mixin.

@param mixin [Occi::Core::Mixin] expected mixin @return [Set] set of found entities

# File lib/occi/core/collection.rb, line 66
def find_by_mixin(mixin)
  raise ArgumentError, 'Mixin is a mandatory argument' unless mixin
  Set.new(entities.select { |elm| elm.mixins.include?(mixin) })
end
only_action_instances?() click to toggle source

Reports content of the collection with regards to action instances.

@return [TrueClass] if there are only action instances @return [FalseClass] if there are not only action instances

# File lib/occi/core/collection.rb, line 175
def only_action_instances?
  only? :action_instances
end
only_categories?() click to toggle source

Reports content of the collection with regards to categories.

@return [TrueClass] if there are only categories @return [FalseClass] if there are not only categories

# File lib/occi/core/collection.rb, line 159
def only_categories?
  only? :categories
end
only_entities?() click to toggle source

Reports content of the collection with regards to entities.

@return [TrueClass] if there are only entities @return [FalseClass] if there are not only entities

# File lib/occi/core/collection.rb, line 167
def only_entities?
  only? :entities
end
remove(object) click to toggle source

Auto-removes the given object from the appropriate internal set. Unknown objects will result in an `ArgumentError` error.

@param object [Object] object to be removed @return [Occi::Core::Collection] self, for chaining purposes

Calls superclass method
# File lib/occi/core/collection.rb, line 112
def remove(object)
  case object
  when Occi::Core::Entity
    entities.delete object
  when Occi::Core::ActionInstance
    action_instances.delete object
  else
    super
  end

  self
end
resources() click to toggle source

Collects all `Occi::Core::Resource` instances in this collection.

@return [Set] all `Occi::Core::Resource` instances from this collection

# File lib/occi/core/collection.rb, line 32
def resources
  typed_set(entities, Occi::Core::Resource)
end
valid!() click to toggle source

Triggers validation on the underlying `Model` instance. In addition, validates all included entities and action instances against categories defined in the collection. Only the existence of categories is checked, no further checks are performed.

See `#valid!` on `Model` for details.

Calls superclass method
# File lib/occi/core/collection.rb, line 131
def valid!
  super
  valid_entities!
  valid_action_instances!
  entities.each(&:valid!)
  action_instances.each(&:valid!)
end
valid?() click to toggle source

Quietly validates the collection. This method does not raise exceptions with detailed descriptions of detected problems.

See `#valid!` for details.

# File lib/occi/core/collection.rb, line 143
def valid?
  valid_helper? :valid!
end