Class: Occi::Model

Inherits:
Collection show all
Defined in:
lib/occi/model.rb

Instance Attribute Summary

Attributes inherited from Collection

#action, #actions, #kinds, #links, #mixins, #model, #resources

Instance Method Summary collapse

Methods inherited from Collection

#<<, #==, #as_json, #categories, #check, #empty?, #entities, #get_by_id, #get_by_location, #get_related_to, #intersect, #intersect!, #merge, #merge!, #to_header, #to_text

Methods included from Helpers::Inspect

#inspect

Constructor Details

#initialize(collection = nil) ⇒ Model

Returns a new instance of Model

Parameters:


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

Parameters:

Returns:


81
82
83
# File 'lib/occi/model.rb', line 81

def get(filter = nil)
  filter ? self.get_related_to(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

Parameters:


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_coreObject

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

Parameters:

  • path (String)

    to a folder containing files which include OCCI collections in JSON format. The path is recursively searched for files with the extension .json .

  • scheme_base_url (Sting) (defaults to: 'http://localhost')

    base location for provider specific extensions of the OCCI model


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_infrastructureObject

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

#resetObject

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

#unregister(category) ⇒ Object

Parameters:


70
71
72
73
74
75
# File 'lib/occi/model.rb', line 70

def unregister(category)
  Occi::Log.debug "### Unregistering category #{category.type_identifier}"
  @kinds.delete category
  @mixins.delete category
  @actions.delete category
end