Class: Occi::Core::Model
- Inherits:
-
Object
- Object
- Occi::Core::Model
- 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
Constant Summary
Constants included from Helpers::Renderable
Helpers::Renderable::RENDERER_FACTORY_CLASS
Instance Attribute Summary collapse
-
#categories ⇒ Set
set of categories associated with this model instance.
Instance Method Summary collapse
-
#<<(object) ⇒ Occi::Core::Model
(also: #add)
Auto-assigns the given object to the appropriate internal set.
-
#actions ⇒ Set
Collects all ‘Occi::Core::Action` instances in this model.
-
#all ⇒ Set
Collects everything present in this model and merges it into a single set.
-
#associated_actions ⇒ Set
Collects all ‘Occi::Core::Action` instances specified in `actions` in one or more other mixins/kinds in this model.
-
#depended_on_mixins ⇒ Set
Collects all ‘Occi::Core::Mixin` instances specified in `depends` in one or more other mixins in this model.
-
#empty? ⇒ TrueClass, FalseClass
(also: #nothing?)
Reports emptiness of the model.
-
#find_by_identifier(identifier) ⇒ Set
Collects all ‘Occi::Core::Category` successors with the given identifier.
-
#find_by_identifier!(identifier) ⇒ Object
See ‘find_by_identifier`.
-
#find_by_location(location) ⇒ Set
Collects everything with the given location.
-
#find_by_schema(schema) ⇒ Set
Collects all ‘Occi::Core::Category` successors with the given schema.
-
#find_by_term(term) ⇒ Set
Collects all ‘Occi::Core::Category` successors with the given term.
-
#find_dependent(mixin) ⇒ Set
Collects all ‘Occi::Core::Mixin` instances dependent on the given instance.
-
#find_related(kind, options = { directly: false }) ⇒ Set
Collects all ‘Occi::Core::Kind` instances related to the given instance.
-
#initialize(args = {}) ⇒ Model
constructor
Constructs an instance with the given information.
-
#instance_builder ⇒ Occi::Core::InstanceBuilder
Returns an instance of ‘Occi::Core::InstanceBuilder` associated with this model.
-
#kinds ⇒ Set
Collects all ‘Occi::Core::Kind` instances in this model.
-
#load_core! ⇒ Object
Loads OGF’s OCCI Core Standard from ‘Occi::Core::Warehouse`.
-
#mixins ⇒ Set
Collects all ‘Occi::Core::Mixin` instances in this model.
-
#parent_kinds ⇒ Set
Collects all ‘Occi::Core::Kind` instances specified as `parent` in one or more other kinds in this model.
-
#remove(object) ⇒ Occi::Core::Model
Auto-removes the given object from the appropriate internal set.
-
#valid! ⇒ Object
Validates kinds, mixins, and actions stored in this model.
-
#valid? ⇒ TrueClass, FalseClass
Validates kinds, mixins, and actions stored in this model.
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.
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
#categories ⇒ Set
set of categories associated with this model instance
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.
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 |
#actions ⇒ Set
Collects all ‘Occi::Core::Action` instances in this model.
91 92 93 |
# File 'lib/occi/core/model.rb', line 91 def actions typed_set(categories, Occi::Core::Action) end |
#all ⇒ Set
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.
43 44 45 |
# File 'lib/occi/core/model.rb', line 43 def all Set.new categories end |
#associated_actions ⇒ Set
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.
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_mixins ⇒ Set
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.
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.
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.
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.
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.
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.
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.
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.
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 |
#find_related(kind, options = { directly: false }) ⇒ Set
Collects all ‘Occi::Core::Kind` instances related to the given instance.
114 115 116 117 118 |
# File 'lib/occi/core/model.rb', line 114 def (kind, = { directly: false }) raise ArgumentError, 'Kind is a mandatory argument' unless kind method = [:directly] ? :directly_related? : :related? Set.new(kinds.select { |knd| knd.send(method, kind) }) end |
#instance_builder ⇒ Occi::Core::InstanceBuilder
Returns an instance of ‘Occi::Core::InstanceBuilder` associated with this model.
252 253 254 |
# File 'lib/occi/core/model.rb', line 252 def instance_builder Occi::Core::InstanceBuilder.new(model: self) end |
#kinds ⇒ Set
Collects all ‘Occi::Core::Kind` instances in 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`.
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 |
#mixins ⇒ Set
Collects all ‘Occi::Core::Mixin` instances in this model.
70 71 72 |
# File 'lib/occi/core/model.rb', line 70 def mixins typed_set(categories, Occi::Core::Mixin) end |
#parent_kinds ⇒ Set
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.
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.
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!`.
215 216 217 |
# File 'lib/occi/core/model.rb', line 215 def valid? valid_helper? :valid! end |