class Occi::Core::Model

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.

@attr categories [Set] set of categories associated with this model instance

@author Boris Parak <parak@cesnet.cz>

Attributes

categories[RW]

Public Class Methods

new(args = {}) click to toggle source

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

@example

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

@param args [Hash] arguments with model instance information @option args [Set] :categories set of categories associated with this model instance

# 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

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::Model] self, for chaining purposes

# 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
Also aliased as: add
actions() click to toggle source

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

@return [Set] all `Occi::Core::Action` instances from this model

# File lib/occi/core/model.rb, line 91
def actions
  typed_set(categories, Occi::Core::Action)
end
add(object)
Alias for: <<
all() click to toggle source

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.

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

# File lib/occi/core/model.rb, line 43
def all
  Set.new categories
end
associated_actions() click to toggle source

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.

@return [Set] associated `Occi::Core::Action` instances from this model

# 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_mixins() click to toggle source

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.

@return [Set] depended on `Occi::Core::Mixin` instances from this model

# 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?() click to toggle source

Reports emptiness of the model.

@return [TrueClass] if there are no categories @return [FalseClass] if there are some categories

# File lib/occi/core/model.rb, line 234
def empty?
  categories.empty?
end
Also aliased as: nothing?
find_by_identifier(identifier) click to toggle source

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

@param identifier [String] expected identifier @return [Set] set of found categories

# 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) click to toggle source

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

@param identifier [String] expected identifier @return [Object] found category

# 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) click to toggle source

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

@param location [URI] expected location @return [Set] set of results possibly containing a mix of types

# 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) click to toggle source

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

@param schema [String] expected schema @return [Set] set of found categories

# 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) click to toggle source

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

@param term [String] expected term @return [Set] set of found categories

# File lib/occi/core/model.rb, line 163
def find_by_term(term)
  filtered_set(categories, key: 'term', value: term)
end
find_dependent(mixin) click to toggle source

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

@param mixin [Occi::Core::Mixin] top-level mixin @return [Set] all instances dependent on the given instance

# 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
instance_builder() click to toggle source

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

@return [Occi::Core::InstanceBuilder] instance of IB

# File lib/occi/core/model.rb, line 252
def instance_builder
  Occi::Core::InstanceBuilder.new(model: self)
end
kinds() click to toggle source

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

@return [Set] all `Occi::Core::Kind` instances from this model

# File lib/occi/core/model.rb, line 50
def kinds
  typed_set(categories, Occi::Core::Kind)
end
load_core!() click to toggle source

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

@example

model = Occi::Core::Model.new
model.load_core!
# 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
mixins() click to toggle source

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

@return [Set] all `Occi::Core::Mixin` instances from this model

# File lib/occi/core/model.rb, line 70
def mixins
  typed_set(categories, Occi::Core::Mixin)
end
nothing?()
Alias for: empty?
parent_kinds() click to toggle source

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.

@return [Set] parenting `Occi::Core::Kind` instances from this model

# File lib/occi/core/model.rb, line 60
def parent_kinds
  parents = kinds
  parents.collect!(&:parent)
  parents.reject!(&:nil?)
  parents
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::Model] self, for chaining purposes

# 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!() click to toggle source

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.

# 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?() click to toggle source

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!`.

@return [TrueClass] on successful validation @return [FalseClass] on failed validation

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