Class: Occi::Collection
- Inherits:
-
Object
- Object
- Occi::Collection
- Defined in:
- lib/occi/collection.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#action ⇒ Object
Returns the value of attribute action.
-
#actions ⇒ Object
Returns the value of attribute actions.
-
#kinds ⇒ Object
Returns the value of attribute kinds.
-
#links ⇒ Object
Returns the value of attribute links.
-
#mixins ⇒ Object
Returns the value of attribute mixins.
-
#model ⇒ Object
Returns the value of attribute model.
-
#resources ⇒ Object
Returns the value of attribute resources.
Instance Method Summary collapse
- #==(category) ⇒ Object
-
#as_json(options = {}) ⇒ Hashie::Mash
Json representation.
-
#categories ⇒ Occi::Core::Categories
Categories combined list of all kinds, mixins and actions.
- #check ⇒ Object
-
#empty? ⇒ true, false
True if collection is empty, false otherwise.
-
#entities ⇒ Occi::Core::Entities
Entities combined list of all resources and links.
-
#get_by_id(id) ⇒ Occi::Core::Category
Returns the category corresponding to a given id.
-
#get_by_location(location) ⇒ Occi::Core::Category
Returns the category corresponding to a given location.
-
#get_related_to(category) ⇒ Occi::Core::Collection
Returns a collection with all categories related to the specified category.
-
#initialize(collection = {}, model = Occi::Model.new) ⇒ Collection
constructor
Initialize a new OCCI Collection by initializing all supplied OCCI objects.
- #inspect ⇒ Object
- #intersect(other_collection, collection = self.clone) ⇒ Occi::Collection
- #intersect!(other_collection) ⇒ Occi::Collection
- #merge(other_collection, collection = self.clone) ⇒ Occi::Collection
- #merge!(other_collection) ⇒ Occi::Collection
- #to_header ⇒ Object
-
#to_text ⇒ String
Text representation.
Constructor Details
#initialize(collection = {}, model = Occi::Model.new) ⇒ Collection
Initialize a new OCCI Collection by initializing all supplied OCCI objects
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/occi/collection.rb', line 9 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., kind.actions) } @mixins.merge collection.mixins.to_a.collect { |mixin| Occi::Core::Mixin.new(mixin.scheme, mixin.term, mixin.title, mixin.attributes, mixin., 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::Action_instance.new(collection.action, collection.attributes) if collection.action end |
Instance Attribute Details
#action ⇒ Object
Returns the value of attribute action.
4 5 6 |
# File 'lib/occi/collection.rb', line 4 def action @action end |
#actions ⇒ Object
Returns the value of attribute actions.
4 5 6 |
# File 'lib/occi/collection.rb', line 4 def actions @actions end |
#kinds ⇒ Object
Returns the value of attribute kinds.
4 5 6 |
# File 'lib/occi/collection.rb', line 4 def kinds @kinds end |
#links ⇒ Object
Returns the value of attribute links.
4 5 6 |
# File 'lib/occi/collection.rb', line 4 def links @links end |
#mixins ⇒ Object
Returns the value of attribute mixins.
4 5 6 |
# File 'lib/occi/collection.rb', line 4 def mixins @mixins end |
#model ⇒ Object
Returns the value of attribute model.
4 5 6 |
# File 'lib/occi/collection.rb', line 4 def model @model end |
#resources ⇒ Object
Returns the value of attribute resources.
4 5 6 |
# File 'lib/occi/collection.rb', line 4 def resources @resources end |
Instance Method Details
#==(category) ⇒ Object
28 29 30 |
# File 'lib/occi/collection.rb', line 28 def ==(category) not intersect(category).empty? end |
#as_json(options = {}) ⇒ Hashie::Mash
Returns json representation.
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/occi/collection.rb', line 137 def as_json( = {}) 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 |
#categories ⇒ Occi::Core::Categories
Returns categories combined list of all kinds, mixins and actions.
33 34 35 |
# File 'lib/occi/collection.rb', line 33 def categories Occi::Core::Categories.new(@kinds + @mixins + @actions) end |
#check ⇒ Object
53 54 55 56 57 58 |
# File 'lib/occi/collection.rb', line 53 def check @resources.check @links.check # TODO: check action instance format, should check be applicable? #@action.check end |
#empty? ⇒ true, false
Returns true if collection is empty, false otherwise.
121 122 123 |
# File 'lib/occi/collection.rb', line 121 def empty? @kinds.empty? && @mixins.empty? && @actions.empty? && @resources.empty? && @links.empty? && @action.nil? end |
#entities ⇒ Occi::Core::Entities
Returns entities combined list of all resources and links.
38 39 40 |
# File 'lib/occi/collection.rb', line 38 def entities Occi::Core::Entities.new(@resources + @links) end |
#get_by_id(id) ⇒ Occi::Core::Category
Returns the category corresponding to a given id
106 107 108 109 110 |
# File 'lib/occi/collection.rb', line 106 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
116 117 118 |
# File 'lib/occi/collection.rb', line 116 def get_by_location(location) self.categories.select { |category| category.location == location }.first end |
#get_related_to(category) ⇒ Occi::Core::Collection
Returns a collection with all categories related to the specified category
129 130 131 132 133 134 |
# File 'lib/occi/collection.rb', line 129 def (category) collection = self.class.new collection.kinds = @kinds.(category) collection.mixins = @mixins.(category) collection end |
#inspect ⇒ Object
181 182 183 |
# File 'lib/occi/collection.rb', line 181 def inspect JSON.pretty_generate(JSON.parse(to_json)) end |
#intersect(other_collection, collection = self.clone) ⇒ Occi::Collection
88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/occi/collection.rb', line 88 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
81 82 83 |
# File 'lib/occi/collection.rb', line 81 def intersect!(other_collection) intersect other_collection, self end |
#merge(other_collection, collection = self.clone) ⇒ Occi::Collection
69 70 71 72 73 74 75 76 77 |
# File 'lib/occi/collection.rb', line 69 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
62 63 64 |
# File 'lib/occi/collection.rb', line 62 def merge!(other_collection) merge other_collection, self end |
#to_header ⇒ Object
171 172 173 174 175 176 177 178 179 |
# File 'lib/occi/collection.rb', line 171 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_text ⇒ String
Returns text representation.
160 161 162 163 164 165 166 167 168 169 |
# File 'lib/occi/collection.rb', line 160 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 |