Class: Occi::Core::Entity

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

Direct Known Subclasses

Link, Resource

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Parameters:



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

.actionsObject (readonly)

Returns the value of attribute actions.



9
10
11
# File 'lib/occi/core/entity.rb', line 9

def actions
  @actions
end

.kindObject

Returns the value of attribute kind.



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

def kind
  @kind
end

.mixinsObject (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

#actionsObject

Returns the value of attribute actions.



5
6
7
# File 'lib/occi/core/entity.rb', line 5

def actions
  @actions
end

#attributesObject

Returns the value of attribute attributes.



5
6
7
# File 'lib/occi/core/entity.rb', line 5

def attributes
  @attributes
end

#idUUIDTools::UUID

Returns id of the entity.

Returns:

  • (UUIDTools::UUID)

    id of the entity



101
102
103
# File 'lib/occi/core/entity.rb', line 101

def id
  @id
end

#kindOcci::Core::Kind

Returns:



64
65
66
# File 'lib/occi/core/entity.rb', line 64

def kind
  @kind
end

#mixinsObject

Returns the value of attribute mixins.



5
6
7
# File 'lib/occi/core/entity.rb', line 5

def mixins
  @mixins
end

#modelObject

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.

Parameters:

Returns:



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.

Parameters:

  • args (Array)

    list of arguments

Returns:

  • (Object)

    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
    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

Returns:

  • (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.

Parameters:

  • options (Hash) (defaults to: { })

Returns:

  • (Hashie::Mash)

    json representation



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

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

Parameters:

  • model (Occi::Model)

    representation of the Occi model to check the attributes against



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

#inspectString

Returns json representation.

Returns:

  • (String)

    json representation



235
236
237
# File 'lib/occi/core/entity.rb', line 235

def inspect
  JSON.pretty_generate(JSON.parse(to_json))
end

#locationString

Returns location of the entity.

Returns:

  • (String)

    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

#titleString

Returns title attribute of entity.

Returns:

  • (String)

    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

Parameters:

  • title (String)


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

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

#to_headerHash

Returns hash containing the HTTP headers of the text/occi rendering.

Returns:

  • (Hash)

    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_sString

Returns string representation of entity is its location.

Returns:

  • (String)

    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_textString

Returns text representation.

Returns:

  • (String)

    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