Class: Occi::Collection

Inherits:
Object
  • Object
show all
Includes:
Helpers::Inspect
Defined in:
lib/occi/collection.rb

Direct Known Subclasses

Model

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers::Inspect

#inspect

Constructor Details

#initialize(collection = {}, model = Occi::Model.new) ⇒ Collection

Initialize a new OCCI Collection by initializing all supplied OCCI objects


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/occi/collection.rb', line 11

def initialize(collection={}, model = Occi::Model.new)
  collection = Hashie::Mash.new(collection) unless collection.kind_of? Occi::Collection

  @kinds = Occi::Core::Kinds.new
  @mixins = Occi::Core::Mixins.new
  @actions = Occi::Core::Actions.new
  @resources = Occi::Core::Resources.new
  @links = Occi::Core::Links.new

  self.model = model if model

  @kinds.merge collection.kinds.to_a.collect { |kind| Occi::Core::Kind.new(kind.scheme, kind.term, kind.title, kind.attributes, kind.related, kind.actions) }
  @mixins.merge collection.mixins.to_a.collect { |mixin| Occi::Core::Mixin.new(mixin.scheme, mixin.term, mixin.title, mixin.attributes, mixin.related, mixin.actions) }
  @actions.merge collection.actions.to_a.collect { |action| Occi::Core::Action.new(action.scheme, action.term, action.title, action.attributes) }
  @resources.merge collection.resources.to_a.collect { |resource| Occi::Core::Resource.new(resource.kind, resource.mixins, resource.attributes, resource.links) }
  @links.merge collection.links.to_a.collect { |link| Occi::Core::Link.new(link.kind, link.mixins, link.attributes) }
  @action = Occi::Core::ActionInstance.new(collection.action, collection.attributes) if collection.action
end

Instance Attribute Details

#actionObject

Returns the value of attribute action


6
7
8
# File 'lib/occi/collection.rb', line 6

def action
  @action
end

#actionsObject

Returns the value of attribute actions


6
7
8
# File 'lib/occi/collection.rb', line 6

def actions
  @actions
end

#kindsObject

Returns the value of attribute kinds


6
7
8
# File 'lib/occi/collection.rb', line 6

def kinds
  @kinds
end

Returns the value of attribute links


6
7
8
# File 'lib/occi/collection.rb', line 6

def links
  @links
end

#mixinsObject

Returns the value of attribute mixins


6
7
8
# File 'lib/occi/collection.rb', line 6

def mixins
  @mixins
end

#modelObject

Returns the value of attribute model


6
7
8
# File 'lib/occi/collection.rb', line 6

def model
  @model
end

#resourcesObject

Returns the value of attribute resources


6
7
8
# File 'lib/occi/collection.rb', line 6

def resources
  @resources
end

Instance Method Details

#<<(object) ⇒ Object


30
31
32
33
34
35
36
# File 'lib/occi/collection.rb', line 30

def <<(object)
  object.kind_of? Occi::Core::Kind and self.kinds << object
  object.kind_of? Occi::Core::Mixin and self.mixins << object
  object.kind_of? Occi::Core::Action and self.actions << object
  object.kind_of? Occi::Core::Resource and self.resources << object
  object.kind_of? Occi::Core::Link and self.links << object
end

#==(category) ⇒ Object


38
39
40
# File 'lib/occi/collection.rb', line 38

def ==(category)
  not intersect(category).empty?
end

#as_json(options = {}) ⇒ Hashie::Mash


147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/occi/collection.rb', line 147

def as_json(options = {})
  collection = Hashie::Mash.new
  collection.kinds = @kinds.collect { |kind| kind.as_json } if @kinds.any?
  collection.mixins = @mixins.collect { |mixin| mixin.as_json } if @mixins.any?
  collection.actions = @actions.collect { |action_category| action_category.as_json } if @actions.any?
  collection.resources = @resources.collect { |resource| resource.as_json } if @resources.any?
  # if there is only one resource and the links inside the resource have no location,
  # then these links must be rendered as separate links inside the collection
  if !collection.resources.nil? && collection.resources.size == 1
    if collection.resources.first.links.blank? && @links.empty?
      lnks = @resources.first.links
    else
      lnks = @links
    end
  else
    lnks = @links
  end
  collection.links = lnks.collect { |link| link.as_json } if lnks.to_a.any?
  collection.action = @action.as_json if @action
  collection
end

#categoriesOcci::Core::Categories


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

def categories
  Occi::Core::Categories.new(@kinds + @mixins + @actions)
end

#checkObject


63
64
65
66
67
68
# File 'lib/occi/collection.rb', line 63

def check
  @resources.check
  @links.check
  # TODO: check action instance format, should check be applicable?
  #@action.check
end

#empty?true, false


131
132
133
# File 'lib/occi/collection.rb', line 131

def empty?
  @kinds.empty? && @mixins.empty? && @actions.empty? && @resources.empty? && @links.empty? && @action.nil?
end

#entitiesOcci::Core::Entities


48
49
50
# File 'lib/occi/collection.rb', line 48

def entities
  Occi::Core::Entities.new(@resources + @links)
end

#get_by_id(id) ⇒ Occi::Core::Category

Returns the category corresponding to a given id


116
117
118
119
120
# File 'lib/occi/collection.rb', line 116

def get_by_id(id)
  object = self.categories.select { |category| category.type_identifier == id }
  object = self.entities.select { |entity| entity.id == id } if object.empty?
  object.first
end

#get_by_location(location) ⇒ Occi::Core::Category

Returns the category corresponding to a given location


126
127
128
# File 'lib/occi/collection.rb', line 126

def get_by_location(location)
  self.categories.select { |category| category.location == location }.first
end

Returns a collection with all categories related to the specified category


139
140
141
142
143
144
# File 'lib/occi/collection.rb', line 139

def get_related_to(category)
  collection = self.class.new
  collection.kinds = @kinds.get_related_to(category)
  collection.mixins = @mixins.get_related_to(category)
  collection
end

#intersect(other_collection, collection = self.clone) ⇒ Occi::Collection


98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/occi/collection.rb', line 98

def intersect(other_collection, collection=self.clone)
  collection.kinds.replace other_collection.kinds.select { |kind| get_by_id(kind.type_identifier) }
  collection.mixins.replace other_collection.mixins.select { |mixin| get_by_id(mixin.type_identifier) }
  collection.actions.replace other_collection.actions.select { |action| get_by_id(action.type_identifier) }
  collection.resources.replace other_collection.resources.select { |resource| get_by_id(resource.id) }
  collection.links.replace other_collection.links.select { |link| get_by_id(link.type_identifier) }
  if collection.action == other_collection.action
    collection.action = other_collection.action
  else
    collection.action = nil
  end
  collection
end

#intersect!(other_collection) ⇒ Occi::Collection


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

def intersect!(other_collection)
  intersect other_collection, self
end

#merge(other_collection, collection = self.clone) ⇒ Occi::Collection


79
80
81
82
83
84
85
86
87
# File 'lib/occi/collection.rb', line 79

def merge(other_collection, collection=self.clone)
  collection.kinds.merge other_collection.kinds.select { |kind| get_by_id(kind.type_identifier).nil? }
  collection.mixins.merge other_collection.mixins.select { |mixin| get_by_id(mixin.type_identifier).nil? }
  collection.actions.merge other_collection.actions.select { |action| get_by_id(action.type_identifier).nil? }
  collection.resources.merge other_collection.resources.select { |resource| get_by_id(resource.id).nil? }
  collection.links.merge other_collection.links.select { |link| get_by_id(link.type_identifier).nil? }
  collection.action = other_collection.action if other_collection.action
  collection
end

#merge!(other_collection) ⇒ Occi::Collection


72
73
74
# File 'lib/occi/collection.rb', line 72

def merge!(other_collection)
  merge other_collection, self
end

#to_headerObject


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

def to_header
  header = Hashie::Mash.new
  header['Category'] = self.categories.collect { |category| category.to_string_short }.join(',') if self.categories.any?
  raise "Only one resource allowed for rendering to text/occi" if self.resources.size > 1
  header = self.resources.first.to_header if self.resources.any?
  header['Link'] = self.links.collect { |link| link.to_string }.join(',') if self.links.any?
  header = self.action.to_header if self.action
  header
end

#to_textString


170
171
172
173
174
175
176
177
178
179
# File 'lib/occi/collection.rb', line 170

def to_text
  text = ""
  text << self.categories.collect { |category| category.to_text }.join("\n")
  text << "\n" if self.categories.any?
  raise "Only one resource allowed for rendering to text/plain" if self.resources.size > 1
  text << self.resources.collect { |resource| resource.to_text }.join("\n")
  text << self.links.collect { |link| link.to_text_link }.join("\n")
  text << self.action.to_text if self.action
  text
end