Class: Occi::Core::Entity
- Inherits:
-
Object
- Object
- Occi::Core::Entity
- Defined in:
- lib/occi/core/entity.rb
Class Attribute Summary collapse
-
.actions ⇒ Object
readonly
Returns the value of attribute actions.
-
.kind ⇒ Object
Returns the value of attribute kind.
-
.mixins ⇒ Object
readonly
Returns the value of attribute mixins.
Instance Attribute Summary collapse
-
#actions ⇒ Object
Returns the value of attribute actions.
-
#attributes ⇒ Object
Returns the value of attribute attributes.
-
#id ⇒ UUIDTools::UUID
Id of the entity.
- #kind ⇒ Occi::Core::Kind
-
#mixins ⇒ Object
Returns the value of attribute mixins.
-
#model ⇒ Object
Returns the value of attribute model.
Class Method Summary collapse
-
.check(attributes, definitions, set_defaults = false) ⇒ Occi::Core::Attributes
Attributes with their defaults set.
-
.new(*args) ⇒ Object
New instance of this class.
- .type_identifier ⇒ String
Instance Method Summary collapse
-
#as_json(options = { }) ⇒ Hashie::Mash
Json representation.
-
#check ⇒ Object
check attributes against their definitions and set defaults.
- #initialize(kind = self.kind, mixins = [], attributes = { }, actions = []) ⇒ Occi::Core::Entity constructor
-
#inspect ⇒ String
Json representation.
-
#location ⇒ String
Location of the entity.
-
#title ⇒ String
Title attribute of entity.
-
#title=(title) ⇒ Object
set title attribute for entity.
-
#to_header ⇒ Hash
Hash containing the HTTP headers of the text/occi rendering.
-
#to_s ⇒ String
String representation of entity is its location.
-
#to_text ⇒ String
Text representation.
Constructor Details
#initialize(kind = self.kind, mixins = [], attributes = { }, actions = []) ⇒ Occi::Core::Entity
56 57 58 59 60 61 |
# File 'lib/occi/core/entity.rb', line 56 def initialize(kind = self.kind, mixins=[], attributes={ }, actions=[]) @kind = self.class.kind.clone @mixins = Occi::Core::Mixins.new mixins @attributes = Occi::Core::Attributes.new attributes @actions = Occi::Core::Actions.new actions end |
Class Attribute Details
.actions ⇒ Object (readonly)
Returns the value of attribute actions.
9 10 11 |
# File 'lib/occi/core/entity.rb', line 9 def actions @actions end |
.kind ⇒ Object
Returns the value of attribute kind.
8 9 10 |
# File 'lib/occi/core/entity.rb', line 8 def kind @kind end |
.mixins ⇒ Object (readonly)
Returns the value of attribute mixins.
9 10 11 |
# File 'lib/occi/core/entity.rb', line 9 def mixins @mixins end |
Instance Attribute Details
#actions ⇒ Object
Returns the value of attribute actions.
5 6 7 |
# File 'lib/occi/core/entity.rb', line 5 def actions @actions end |
#attributes ⇒ Object
Returns the value of attribute attributes.
5 6 7 |
# File 'lib/occi/core/entity.rb', line 5 def attributes @attributes end |
#id ⇒ UUIDTools::UUID
Returns id of the entity.
101 102 103 |
# File 'lib/occi/core/entity.rb', line 101 def id @id end |
#mixins ⇒ Object
Returns the value of attribute mixins.
5 6 7 |
# File 'lib/occi/core/entity.rb', line 5 def mixins @mixins end |
#model ⇒ Object
Returns the value of attribute model.
5 6 7 |
# File 'lib/occi/core/entity.rb', line 5 def model @model end |
Class Method Details
.check(attributes, definitions, set_defaults = false) ⇒ Occi::Core::Attributes
Returns attributes with their defaults set.
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/occi/core/entity.rb', line 152 def self.check(attributes, definitions, set_defaults=false) attributes = Occi::Core::Attributes.new(attributes) definitions.each_key do |key| if definitions[key].kind_of? Occi::Core::Attributes attributes[key] = check(attributes[key], definitions[key]) else properties = definitions[key] value = attributes[key] value ||= properties.default if set_defaults or properties.required raise "required attribute #{key} not found" if value.nil? && properties.required next if value.nil? and not properties.required case properties.type when 'number' raise "attribute #{key} value #{value} from class #{value.class.name} does not match attribute property type #{properties.type}" unless value.kind_of?(Numeric) when 'boolean' raise "attribute #{key} value #{value} from class #{value.class.name} does not match attribute property type #{properties.type}" unless !!value == value when 'string' raise "attribute #{key} with value #{value} from class #{value.class.name} does not match attribute property type #{properties.type}" unless value.kind_of?(String) else raise "property type #{properties.type} is not one of the allowed types number, boolean or string" end Occi::Log.warn "attribute #{key} with value #{value} does not match pattern #{properties.pattern}" if value.to_s.scan(Regexp.new(properties.pattern)).empty? if properties.pattern attributes[key] = value end end attributes.delete_if { |_, v| v.empty? } # remove empty attributes attributes end |
.new(*args) ⇒ Object
Returns new instance of this class.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/occi/core/entity.rb', line 34 def self.new(*args) if args.size > 0 type_identifier = args[0].to_s = [self.kind] else type_identifier = self.kind.type_identifier = nil end scheme, term = type_identifier.split '#' klass = Occi::Core::Kind.get_class scheme, term, object = klass.allocate object.send :initialize, *args object end |
.type_identifier ⇒ String
28 29 30 |
# File 'lib/occi/core/entity.rb', line 28 def self.type_identifier self.kind.type_identifier end |
Instance Method Details
#as_json(options = { }) ⇒ Hashie::Mash
Returns json representation.
183 184 185 186 187 188 189 190 191 |
# File 'lib/occi/core/entity.rb', line 183 def as_json(={ }) entity = Hashie::Mash.new entity.kind = @kind.to_s if @kind entity.mixins = @mixins.join(' ').split(' ') if @mixins.any? entity.actions = @actions if @actions.any? entity.attributes = @attributes if @attributes.any? entity.id = id if id entity end |
#check ⇒ Object
check attributes against their definitions and set defaults
135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/occi/core/entity.rb', line 135 def check raise "No model associated" unless @model definitions = Occi::Core::Attributes.new definitions.merge!(@model.get_by_id(@kind.to_s).attributes) @mixins.each do |mixin_id| mixin = @model.get_by_id(mixin_id) next if mixin.nil? definitions.merge!(mixin.attributes) if mixin.attributes end if @mixins @attributes = Entity.check(@attributes, definitions) if definitions end |
#inspect ⇒ String
Returns json representation.
235 236 237 |
# File 'lib/occi/core/entity.rb', line 235 def inspect JSON.pretty_generate(JSON.parse(to_json)) end |
#location ⇒ String
Returns location of the entity.
129 130 131 |
# File 'lib/occi/core/entity.rb', line 129 def location kind.location + id.gsub('urn:uuid:', '') if id end |
#title ⇒ String
Returns title attribute of entity.
113 114 115 |
# File 'lib/occi/core/entity.rb', line 113 def title @attributes.occi.core.title if @attributes.occi.core if @attributes.occi end |
#title=(title) ⇒ Object
set title attribute for entity
108 109 110 |
# File 'lib/occi/core/entity.rb', line 108 def title=(title) @attributes.occi!.core!.title = title end |
#to_header ⇒ Hash
Returns hash containing the HTTP headers of the text/occi rendering.
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/occi/core/entity.rb', line 213 def to_header header = Hashie::Mash.new header['Category'] = self.kind.term + ';scheme=' + self.kind.scheme.inspect + ';class="kind"' @mixins.each do |mixin| scheme, term = mixin.to_s.split('#') scheme << '#' header['Category'] += ',' + term + ';scheme=' + scheme.inspect + ';class="mixin"' end attributes = [] @attributes.combine.each_pair do |name, value| attributes << name + '=' + value.inspect end header['X-OCCI-Attribute'] = attributes.join(',') if attributes.any? links = [] @actions.each do |action| _, term = action.split('#') links << self.location + '?action=' + term + '>;rel=' + action.inspect end header end |
#to_s ⇒ String
Returns string representation of entity is its location.
240 241 242 |
# File 'lib/occi/core/entity.rb', line 240 def to_s self.location end |
#to_text ⇒ String
Returns text representation.
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/occi/core/entity.rb', line 194 def to_text text = 'Category: ' + self.kind.term + ';scheme=' + self.kind.scheme.inspect + ';class="kind"' + "\n" @mixins.each do |mixin| scheme, term = mixin.to_s.split('#') scheme << '#' text << 'Category: ' + term + ';scheme=' + scheme.inspect + ';class="mixin"' + "\n" end @attributes.combine.each_pair do |name, value| value = value.inspect text << 'X-OCCI-Attribute: ' + name + '=' + value + "\n" end @actions.each do |action| _, term = action.split('#') text << 'Link: <' + self.location + '?action=' + term + '>;rel=' + action.inspect + "\n" end text end |