Class: Occi::Core::Collection

Inherits:
Model
  • Object
show all
Defined in:
lib/occi/core/collection.rb

Overview

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.

Author:

Constant Summary collapse

ALL_KEYS =
i[entities action_instances].freeze
INTERNAL_COLLECTIONS =
(ALL_KEYS + [:categories]).freeze

Constants included from Helpers::Renderable

Helpers::Renderable::RENDERER_FACTORY_CLASS

Instance Attribute Summary collapse

Attributes inherited from Model

#categories

Instance Method Summary collapse

Methods inherited from Model

#actions, #associated_actions, #depended_on_mixins, #find_by_identifier, #find_by_identifier!, #find_by_location, #find_by_schema, #find_by_term, #find_dependent, #find_related, #initialize, #instance_builder, #kinds, #load_core!, #mixins, #parent_kinds

Methods included from Helpers::Renderable

extended, included, #render, #renderer_factory, renderer_factory, renderer_factory_class, #renderer_for

Constructor Details

This class inherits a constructor from Occi::Core::Model

Instance Attribute Details

#action_instancesSet

set of action instances associated with this collection instance

Returns:

  • (Set)

    the current value of action_instances


13
14
15
# File 'lib/occi/core/collection.rb', line 13

def action_instances
  @action_instances
end

#entitiesSet

set of entities associated with this collection instance

Returns:

  • (Set)

    the current value of entities


13
14
15
# File 'lib/occi/core/collection.rb', line 13

def entities
  @entities
end

Instance Method Details

#<<(object) ⇒ Occi::Core::Collection

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

Parameters:

  • object (Object)

    object to be assigned

Returns:


94
95
96
97
98
99
100
101
102
103
104
105
# 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

#allSet

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.

Returns:

  • (Set)

    content of this collection as a new `Set` instance


25
26
27
# File 'lib/occi/core/collection.rb', line 25

def all
  super | entities | action_instances
end

#empty?TrueClass, FalseClass

Reports emptiness of the collection.

Returns:

  • (TrueClass)

    if there are no categories, entities or action instances

  • (FalseClass)

    if there are some categories, entities or action instances


151
152
153
# File 'lib/occi/core/collection.rb', line 151

def empty?
  empties? INTERNAL_COLLECTIONS
end

#find_by_action(action) ⇒ Set

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

Parameters:

Returns:

  • (Set)

    set of found action instances

Raises:

  • (ArgumentError)

57
58
59
60
# 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) ⇒ Set

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

Parameters:

  • id (String)

    expected ID

Returns:

  • (Set)

    set of found entities


75
76
77
# 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) ⇒ Object

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

Parameters:

  • id (String)

    expected ID

Returns:

  • (Object)

    found instance

Raises:


83
84
85
86
87
# 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) ⇒ Set

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

Parameters:

Returns:

  • (Set)

    set of entities with the given kind

Raises:

  • (ArgumentError)

48
49
50
51
# 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) ⇒ Set

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

Parameters:

Returns:

  • (Set)

    set of found entities

Raises:

  • (ArgumentError)

66
67
68
69
# 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.

Returns:

  • (Set)

    all `Occi::Core::Link` instances from this collection


39
40
41
# File 'lib/occi/core/collection.rb', line 39

def links
  typed_set(entities, Occi::Core::Link)
end

#only_action_instances?TrueClass, FalseClass

Reports content of the collection with regards to action instances.

Returns:

  • (TrueClass)

    if there are only action instances

  • (FalseClass)

    if there are not only action instances


175
176
177
# File 'lib/occi/core/collection.rb', line 175

def only_action_instances?
  only? :action_instances
end

#only_categories?TrueClass, FalseClass

Reports content of the collection with regards to categories.

Returns:

  • (TrueClass)

    if there are only categories

  • (FalseClass)

    if there are not only categories


159
160
161
# File 'lib/occi/core/collection.rb', line 159

def only_categories?
  only? :categories
end

#only_entities?TrueClass, FalseClass

Reports content of the collection with regards to entities.

Returns:


167
168
169
# File 'lib/occi/core/collection.rb', line 167

def only_entities?
  only? :entities
end

#remove(object) ⇒ Occi::Core::Collection

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

Parameters:

  • object (Object)

    object to be removed

Returns:


112
113
114
115
116
117
118
119
120
121
122
123
# 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

#resourcesSet

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

Returns:

  • (Set)

    all `Occi::Core::Resource` instances from this collection


32
33
34
# File 'lib/occi/core/collection.rb', line 32

def resources
  typed_set(entities, Occi::Core::Resource)
end

#valid!Object

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.


131
132
133
134
135
136
137
# 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?Boolean

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

See `#valid!` for details.

Returns:


143
144
145
# File 'lib/occi/core/collection.rb', line 143

def valid?
  valid_helper? :valid!
end