Class: Occi::Core::Model

Inherits:
Object
  • Object
show all
Includes:
Helpers::ArgumentValidator, Helpers::Renderable, Yell::Loggable
Defined in:
lib/occi/core/model.rb

Overview

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

Direct Known Subclasses

Collection, Infrastructure::Model

Constant Summary

Constants included from Helpers::Renderable

Helpers::Renderable::RENDERER_FACTORY_CLASS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers::Renderable

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

Constructor Details

#initialize(args = {}) ⇒ Model

Constructs an instance with the given information. All arguments are optional and will default to empty `Set` instances if not provided.

Examples:

my_model = Occi::Core::Model.new
my_model << mixin << kind << action

Parameters:

  • args (Hash) (defaults to: {})

    arguments with model instance information

Options Hash (args):

  • :categories (Set)

    set of categories associated with this model instance


28
29
30
31
32
33
34
35
# File 'lib/occi/core/model.rb', line 28

def initialize(args = {})
  pre_initialize(args)
  default_args! args

  @categories = args.fetch(:categories)

  post_initialize(args)
end

Instance Attribute Details

#categoriesSet

set of categories associated with this model instance

Returns:

  • (Set)

    the current value of categories


12
13
14
# File 'lib/occi/core/model.rb', line 12

def categories
  @categories
end

Instance Method Details

#<<(object) ⇒ Occi::Core::Model Also known as: add

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:


180
181
182
183
184
185
186
187
188
189
# File 'lib/occi/core/model.rb', line 180

def <<(object)
  case object
  when Occi::Core::Category
    categories << object
  else
    raise ArgumentError, "Cannot automatically assign #{object.inspect}"
  end

  self
end

#actionsSet

Collects all `Occi::Core::Action` instances in this model.

Returns:

  • (Set)

    all `Occi::Core::Action` instances from this model


91
92
93
# File 'lib/occi/core/model.rb', line 91

def actions
  typed_set(categories, Occi::Core::Action)
end

#allSet

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

Returns:

  • (Set)

    content of this model as a new `Set` instance


43
44
45
# File 'lib/occi/core/model.rb', line 43

def all
  Set.new categories
end

#associated_actionsSet

Collects all `Occi::Core::Action` instances specified in `actions` in one or more other mixins/kinds in this model. These instances may not appear in the model itself if it has not been successfully validated yet.

Returns:

  • (Set)

    associated `Occi::Core::Action` instances from this model


101
102
103
104
105
106
107
# File 'lib/occi/core/model.rb', line 101

def associated_actions
  associated = kinds + mixins
  associated.collect!(&:actions)
  associated.flatten!
  associated.reject!(&:nil?)
  associated
end

#depended_on_mixinsSet

Collects all `Occi::Core::Mixin` instances specified in `depends` in one or more other mixins in this model. These instances may not appear in the model itself if it has not been successfully validated yet.

Returns:

  • (Set)

    depended on `Occi::Core::Mixin` instances from this model


80
81
82
83
84
85
86
# File 'lib/occi/core/model.rb', line 80

def depended_on_mixins
  depended_on = mixins
  depended_on.collect!(&:depends)
  depended_on.flatten!
  depended_on.reject!(&:nil?)
  depended_on
end

#empty?TrueClass, FalseClass Also known as: nothing?

Reports emptiness of the model.

Returns:


234
235
236
# File 'lib/occi/core/model.rb', line 234

def empty?
  categories.empty?
end

#find_by_identifier(identifier) ⇒ Set

Collects all `Occi::Core::Category` successors with the given identifier.

Parameters:

  • identifier (String)

    expected identifier

Returns:

  • (Set)

    set of found categories


145
146
147
# File 'lib/occi/core/model.rb', line 145

def find_by_identifier(identifier)
  filtered_set(categories, key: 'identifier', value: identifier)
end

#find_by_identifier!(identifier) ⇒ Object

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

Parameters:

  • identifier (String)

    expected identifier

Returns:

  • (Object)

    found category

Raises:


153
154
155
156
157
# File 'lib/occi/core/model.rb', line 153

def find_by_identifier!(identifier)
  found = categories.detect { |elm| elm.identifier == identifier }
  raise Occi::Core::Errors::ModelLookupError, "Category #{identifier.inspect} not found in the model" unless found
  found
end

#find_by_location(location) ⇒ Set

Collects everything with the given location. This method looks for an explicit/full match on the location.

Parameters:

  • location (URI)

    expected location

Returns:

  • (Set)

    set of results possibly containing a mix of types


134
135
136
137
138
139
# File 'lib/occi/core/model.rb', line 134

def find_by_location(location)
  filtered_set(
    all.select { |elm| elm.respond_to?(:location) },
    key: 'location', value: location
  )
end

#find_by_schema(schema) ⇒ Set

Collects all `Occi::Core::Category` successors with the given schema.

Parameters:

  • schema (String)

    expected schema

Returns:

  • (Set)

    set of found categories


171
172
173
# File 'lib/occi/core/model.rb', line 171

def find_by_schema(schema)
  filtered_set(categories, key: 'schema', value: schema)
end

#find_by_term(term) ⇒ Set

Collects all `Occi::Core::Category` successors with the given term.

Parameters:

  • term (String)

    expected term

Returns:

  • (Set)

    set of found categories


163
164
165
# File 'lib/occi/core/model.rb', line 163

def find_by_term(term)
  filtered_set(categories, key: 'term', value: term)
end

#find_dependent(mixin) ⇒ Set

Collects all `Occi::Core::Mixin` instances dependent on the given instance.

Parameters:

Returns:

  • (Set)

    all instances dependent on the given instance

Raises:

  • (ArgumentError)

124
125
126
127
# File 'lib/occi/core/model.rb', line 124

def find_dependent(mixin)
  raise ArgumentError, 'Mixin is a mandatory argument' unless mixin
  Set.new(mixins.select { |mxn| mxn.depends?(mixin) })
end

Collects all `Occi::Core::Kind` instances related to the given instance.

Parameters:

  • kind (Occi::Core::Kind)

    top-level kind

  • options (Hash) (defaults to: { directly: false })

    look-up modifiers, currently only `directly: true`

Returns:

  • (Set)

    all instances related to the given instance

Raises:

  • (ArgumentError)

114
115
116
117
118
# File 'lib/occi/core/model.rb', line 114

def find_related(kind, options = { directly: false })
  raise ArgumentError, 'Kind is a mandatory argument' unless kind
  method = options[:directly] ? :directly_related? : :related?
  Set.new(kinds.select { |knd| knd.send(method, kind) })
end

#instance_builderOcci::Core::InstanceBuilder

Returns an instance of `Occi::Core::InstanceBuilder` associated with this model.

Returns:


252
253
254
# File 'lib/occi/core/model.rb', line 252

def instance_builder
  Occi::Core::InstanceBuilder.new(model: self)
end

#kindsSet

Collects all `Occi::Core::Kind` instances in this model.

Returns:

  • (Set)

    all `Occi::Core::Kind` instances from this model


50
51
52
# File 'lib/occi/core/model.rb', line 50

def kinds
  typed_set(categories, Occi::Core::Kind)
end

#load_core!Object

Loads OGF's OCCI Core Standard from `Occi::Core::Warehouse`.

Examples:

model = Occi::Core::Model.new
model.load_core!

244
245
246
247
# File 'lib/occi/core/model.rb', line 244

def load_core!
  logger.debug 'Loading Core definitions from Core::Warehouse'
  Occi::Core::Warehouse.bootstrap! self
end

#mixinsSet

Collects all `Occi::Core::Mixin` instances in this model.

Returns:

  • (Set)

    all `Occi::Core::Mixin` instances from this model


70
71
72
# File 'lib/occi/core/model.rb', line 70

def mixins
  typed_set(categories, Occi::Core::Mixin)
end

#parent_kindsSet

Collects all `Occi::Core::Kind` instances specified as `parent` in one or more other kinds in this model. These instances may not appear in the model itself if it has not been successfully validated yet.

Returns:

  • (Set)

    parenting `Occi::Core::Kind` instances from this model


60
61
62
63
64
65
# File 'lib/occi/core/model.rb', line 60

def parent_kinds
  parents = kinds
  parents.collect!(&:parent)
  parents.reject!(&:nil?)
  parents
end

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

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:


197
198
199
200
201
202
203
204
205
206
# File 'lib/occi/core/model.rb', line 197

def remove(object)
  case object
  when Occi::Core::Category
    categories.delete object
  else
    raise ArgumentError, "Cannot automatically delete #{object.inspect}"
  end

  self
end

#valid!Object

Validates kinds, mixins, and actions stored in this model. Validity of each category is considered with regard to other categories. This method will raise an error on the first invalid instance.


222
223
224
225
226
227
228
# File 'lib/occi/core/model.rb', line 222

def valid!
  valid_categories! # checking all identifiers
  valid_parents!    # parentage on kinds
  valid_actions!    # associated actions
  valid_depends!    # dependencies on mixins
  valid_applies!    # applicability on mixins
end

#valid?TrueClass, FalseClass

Validates kinds, mixins, and actions stored in this model. Validity of each category is considered with regard to other categories. If you are looking for a more aggressive version raising validation errors, see `#valid!`.

Returns:


215
216
217
# File 'lib/occi/core/model.rb', line 215

def valid?
  valid_helper? :valid!
end