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>
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
# 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
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
# File lib/occi/core/collection.rb, line 25 def all super | entities | action_instances end
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
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
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
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
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
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
Collects all `Occi::Core::Link` instances in this collection.
@return [Set] all `Occi::Core::Link` instances from this collection
# File lib/occi/core/collection.rb, line 39 def links typed_set(entities, Occi::Core::Link) end
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
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
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
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
# 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
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
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.
# File lib/occi/core/collection.rb, line 131 def valid! super valid_entities! valid_action_instances! entities.each(&:valid!) action_instances.each(&:valid!) end
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