Class: Occi::Core::Entity

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

Direct Known Subclasses

Link, Resource

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers::Inspect

#inspect

Constructor Details

#initialize(kind = self.kind, mixins = [], attributes = {}, actions = [], location = nil) ⇒ Occi::Core::Entity


51
52
53
54
55
56
57
# File 'lib/occi/core/entity.rb', line 51

def initialize(kind = self.kind, mixins=[], attributes={}, actions=[], location=nil)
  @kind = self.class.kind.clone
  @mixins = Occi::Core::Mixins.new mixins
  @attributes = Occi::Core::Attributes.new self.kind.attributes
  @actions = Occi::Core::Actions.new actions
  @location = location
end

Instance Attribute Details

#actionsObject

Returns the value of attribute actions


7
8
9
# File 'lib/occi/core/entity.rb', line 7

def actions
  @actions
end

#attributesObject

Returns the value of attribute attributes


7
8
9
# File 'lib/occi/core/entity.rb', line 7

def attributes
  @attributes
end

#idUUIDTools::UUID


97
98
99
# File 'lib/occi/core/entity.rb', line 97

def id
  @id
end

#kindOcci::Core::Kind


60
61
62
# File 'lib/occi/core/entity.rb', line 60

def kind
  @kind
end

#locationString


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

def location
  @location
end

#mixinsObject

Returns the value of attribute mixins


7
8
9
# File 'lib/occi/core/entity.rb', line 7

def mixins
  @mixins
end

#modelObject

Returns the value of attribute model


7
8
9
# File 'lib/occi/core/entity.rb', line 7

def model
  @model
end

Class Method Details

.check(attributes, definitions, set_defaults = false) ⇒ Occi::Core::Attributes


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
180
181
# File 'lib/occi/core/entity.rb', line 154

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


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/occi/core/entity.rb', line 29

def self.new(*args)
  if args.size > 0
    type_identifier = args[0].to_s
    related = [self.kind]
  else
    type_identifier = self.kind.type_identifier
    related = nil
  end
  scheme, term = type_identifier.split '#'

  klass = Occi::Core::Kind.get_class scheme, term, related

  object = klass.allocate
  object.send :initialize, *args
  object
end

.type_identifierString


23
24
25
# File 'lib/occi/core/entity.rb', line 23

def self.type_identifier
  self.kind.type_identifier
end

Instance Method Details

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


185
186
187
188
189
190
191
192
193
# File 'lib/occi/core/entity.rb', line 185

def as_json(options={})
  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

#checkObject

check attributes against their definitions and set defaults


138
139
140
141
142
143
144
145
146
147
148
# File 'lib/occi/core/entity.rb', line 138

def check
  raise "No model associated" unless @model
  definitions = 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

#titleString


109
110
111
# File 'lib/occi/core/entity.rb', line 109

def title
  @attributes.occi.core.title if @attributes.occi.core if @attributes.occi
end

#title=(title) ⇒ Object

set title attribute for entity


104
105
106
# File 'lib/occi/core/entity.rb', line 104

def title=(title)
  @attributes.occi!.core!.title = title
end

#to_headerHash


215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/occi/core/entity.rb', line 215

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_sString


237
238
239
# File 'lib/occi/core/entity.rb', line 237

def to_s
  self.location
end

#to_textString


196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/occi/core/entity.rb', line 196

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