Class: Occi::Model
- Inherits:
-
Collection
- Object
- Collection
- Occi::Model
- Defined in:
- lib/occi/model.rb
Instance Attribute Summary
Attributes inherited from Collection
#action, #actions, #kinds, #links, #mixins, #model, #resources
Instance Method Summary collapse
-
#get(filter = nil) ⇒ Occi::Collection
Return all categories from model.
-
#initialize(collection = nil) ⇒ Model
constructor
A new instance of Model.
- #model=(model) ⇒ Object
- #register(category) ⇒ Object
-
#register_collection(collection) ⇒ Object
register OCCI categories from OCCI collection.
-
#register_core ⇒ Object
register Occi Core categories enitity, resource and link.
-
#register_files(path, scheme_base_url = 'http://localhost') ⇒ Object
register OCCI categories from files.
-
#register_infrastructure ⇒ Object
register Occi Infrastructure categories.
-
#reset ⇒ Object
clear all entities from all categories.
- #unregister(category) ⇒ Object
Methods inherited from Collection
#==, #as_json, #categories, #check, #empty?, #entities, #get_by_id, #get_by_location, #get_related_to, #inspect, #intersect, #intersect!, #merge, #merge!, #to_header, #to_text
Constructor Details
#initialize(collection = nil) ⇒ Model
Returns a new instance of Model.
5 6 7 8 9 |
# File 'lib/occi/model.rb', line 5 def initialize(collection=nil) super(nil, nil) # model must be empty for model class register_core register_collection collection if collection.kind_of? Occi::Collection end |
Instance Method Details
#get(filter = nil) ⇒ Occi::Collection
Return all categories from model. If filter is present, return only the categories specified by filter
81 82 83 |
# File 'lib/occi/model.rb', line 81 def get(filter = nil) filter ? self.(filter) : self end |
#model=(model) ⇒ Object
11 12 13 |
# File 'lib/occi/model.rb', line 11 def model=(model) # will not assign a model inside a model end |
#register(category) ⇒ Object
60 61 62 63 64 65 66 67 |
# File 'lib/occi/model.rb', line 60 def register(category) Occi::Log.debug "### Registering category #{category}" # add model to category as back reference category.model = self @kinds << category unless get_by_id(category.to_s) if category.class.ancestors.include? Occi::Core::Kind @mixins << category unless get_by_id(category.to_s) if category.class.ancestors.include? Occi::Core::Mixin @actions << category unless get_by_id(category.to_s) if category.class.ancestors.include? Occi::Core::Action end |
#register_collection(collection) ⇒ Object
register OCCI categories from OCCI collection
47 48 49 50 51 52 |
# File 'lib/occi/model.rb', line 47 def register_collection(collection) collection.kinds.each { |kind| kind.model = self } collection.mixins.each { |mixin| mixin.model = self } collection.actions.each { |action| action.model = self } merge! collection end |
#register_core ⇒ Object
register Occi Core categories enitity, resource and link
16 17 18 19 20 21 |
# File 'lib/occi/model.rb', line 16 def register_core Occi::Log.info "### Registering OCCI Core categories enitity, resource and link ###" register Occi::Core::Entity.kind register Occi::Core::Resource.kind register Occi::Core::Link.kind end |
#register_files(path, scheme_base_url = 'http://localhost') ⇒ Object
register OCCI categories from files
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/occi/model.rb', line 34 def register_files(path, scheme_base_url='http://localhost') Occi::Log.info "### Initializing OCCI Model from #{path} ###" Dir.glob(path + '/**/*.json').each do |file| collection = Occi::Collection.new(JSON.parse(File.read(file))) # add location of service provider to scheme if it has a relative location collection.kinds.collect { |kind| kind.scheme = scheme_base_url + kind.scheme if kind.scheme.start_with? '/' } if collection.kinds collection.mixins.collect { |mixin| mixin.scheme = scheme_base_url + mixin.scheme if mixin.scheme.start_with? '/' } if collection.mixins collection.actions.collect { |action| action.scheme = scheme_base_url + action.scheme if action.scheme.start_with? '/' } if collection.actions register_collection collection end end |
#register_infrastructure ⇒ Object
register Occi Infrastructure categories
24 25 26 27 |
# File 'lib/occi/model.rb', line 24 def register_infrastructure Occi::Log.info "### Registering OCCI Infrastructure categories ###" Occi::Infrastructure.categories.each { |category| register category } end |
#reset ⇒ Object
clear all entities from all categories
55 56 57 |
# File 'lib/occi/model.rb', line 55 def reset() categories.each { |category| category.entities = [] if category.respond_to? :entities } end |