class Occi::Core::Entity

Attributes

actions[RW]
attributes[RW]
id[RW]
kind[R]
mixins[RW]
model[RW]

Public Class Methods

attribute_properties() click to toggle source
# File lib/occi4/core/entity.rb, line 48
def self.attribute_properties
  attributes = Occi::Core::Attributes.new self.attributes
  attributes.merge! Occi::Core::Attributes.new(self.superclass.attribute_properties) if self < Occi::Core::Entity
  attributes
end
new(*args) click to toggle source

@param [Array] args list of arguments @return [Object] new instance of this class

# File lib/occi4/core/entity.rb, line 31
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
new(kind = self.kind, mixins=[], attributes={}, actions=[], location=nil) click to toggle source

@param [String] kind @param [String] mixins @param [Occi::Core::Attributes] attributes @param [Occi::Core::Actions] actions @return [Occi::Core::Entity]

# File lib/occi4/core/entity.rb, line 65
def initialize(kind = self.kind, mixins=[], attributes={}, actions=[], location=nil)
  @kind = self.class.kind.clone
  @mixins = Occi::Core::Mixins.new mixins
  @mixins.entity = self

  attributes = self.class.attribute_properties if attributes.blank?
  if attributes.kind_of? Occi::Core::Attributes
    @attributes = attributes.convert
  else
    @attributes = Occi::Core::Attributes.new attributes
  end
  @attributes['occi.core.id'] ||= UUIDTools::UUID.random_create.to_s

  @actions = Occi::Core::Actions.new actions
  @location = location ? URI.parse(location).path : nil
end
type_identifier() click to toggle source

@return [String]

# File lib/occi4/core/entity.rb, line 25
def self.type_identifier
  self.kind.type_identifier
end

Public Instance Methods

actions=(actions) click to toggle source

@param [Occi::Core::Actions] actions

# File lib/occi4/core/entity.rb, line 105
def actions=(actions)
  @actions = Occi::Core::Actions.new actions
end
as_json(options={}) click to toggle source

@param [Hash] options @return [Hashie::Mash] json representation

# File lib/occi4/core/entity.rb, line 195
def as_json(options={})
  entity = Hashie::Mash.new
  entity.kind = @kind.to_s if @kind
  entity.mixins = @mixins.join(' ').split(' ') if @mixins.any?

  action_strings = @actions.collect { |action| action.to_s if action.to_s }.compact
  entity.actions = action_strings unless action_strings.empty?

  entity.attributes = @attributes.as_json if @attributes.as_json.any?
  entity.id = id.to_s if id

  entity
end
attribute_properties() click to toggle source
# File lib/occi4/core/entity.rb, line 54
def attribute_properties
  attributes = self.class.attribute_properties
  @mixins.collect {|mixin| attributes.merge! Occi::Core::Attributes.new(mixin.attributes)}
  attributes
end
attributes=(attributes) click to toggle source

@param [Occi::Core::Attributes] attributes

# File lib/occi4/core/entity.rb, line 100
def attributes=(attributes)
  @attributes = Occi::Core::Attributes.new attributes
end
check(set_defaults = false) click to toggle source

check attributes against their definitions and set defaults @param [true,false] set default values for all empty attributes

# File lib/occi4/core/entity.rb, line 172
def check(set_defaults = false)
  raise ArgumentError, 'No model has been assigned to this entity' unless @model

  kind = @model.get_by_id(@kind.to_s, true)
  raise Occi::Errors::KindNotDefinedError,
        "Kind not found for entity #{self.class.name}[#{self.to_s.inspect}]!" unless kind

  definitions = Occi::Core::Attributes.new
  definitions.merge! kind.attributes

  @mixins.each do |mxn|
    mixin = @model.get_by_id(mxn.to_s)
    raise Occi::Errors::CategoryNotDefinedError,
          "Mixin #{mxn.to_s.inspect} not declared in the model!" unless mixin && mixin.kind_of?(Occi::Core::Mixin)

    definitions.merge!(mixin.attributes) if mixin.attributes
  end if @mixins

  @attributes.check!(definitions, set_defaults)
end
empty?() click to toggle source

@return [Bool] Indicating whether this entity is “empty”, i.e. required attributes are blank

# File lib/occi4/core/entity.rb, line 252
def empty?
  kind.blank? || attributes['occi.core.id'].blank?
end
id=(id) click to toggle source

set id for entity @param [UUIDTools::UUID] id

# File lib/occi4/core/entity.rb, line 111
def id=(id)
  @attributes.occi!.core!.id = id
  @id = id
end
kind=(kind) click to toggle source

@param [Occi::Core::Kind,String] kind @return [Occi::Core::Kind]

# File lib/occi4/core/entity.rb, line 84
def kind=(kind)
  if kind.kind_of? String
    scheme, term = kind.split '#'
    kind = Occi::Core::Kind.get_class scheme, term
  end
  @kind = kind
end
location() click to toggle source

@return [String] location of the entity

# File lib/occi4/core/entity.rb, line 154
def location
  return @location.clone if @location
  return if id.blank? || kind.location.blank?

  # guess the location from kind and ID
  # check for kind locations already included in IDs
  tmp_id = id.gsub('urn:uuid:', '')
  @location = if tmp_id.start_with?(kind.location)
    # ID by itself is enough
    tmp_id
  else
    # concat kind location and ID, remove duplicated slashes
    "#{kind.location}#{tmp_id}".gsub(/\/+/, '/')
  end
end
location=(location) click to toggle source

set location attribute of entity @param [String] location

# File lib/occi4/core/entity.rb, line 149
def location=(location)
  @location = location ? URI.parse(location).path : nil
end
mixins=(mixins) click to toggle source

@param [Array] mixins

# File lib/occi4/core/entity.rb, line 93
def mixins=(mixins)
  @mixins = Occi::Core::Mixins.new mixins
  @mixins.entity = self
  @mixins
end
model=(model) click to toggle source

@param [Occi::Model] model @return [Occi::Model]

# File lib/occi4/core/entity.rb, line 135
def model=(model)
  @model = model

  @kind = (model.get_by_id(@kind.type_identifier) || @kind)
  @kind.entities << self

  @mixins.model = model
  @mixins.each { |mixin| mixin.entities << self }

  @actions.model = model
end
title() click to toggle source

@return [String] title attribute of entity

# File lib/occi4/core/entity.rb, line 129
def title
  @attributes.occi_.core_.title
end
title=(title) click to toggle source

set title attribute for entity @param [String] title

# File lib/occi4/core/entity.rb, line 124
def title=(title)
  @attributes.occi!.core!.title = title
end
to_header() click to toggle source

@return [Hash] hash containing the HTTP headers of the text/occi rendering

# File lib/occi4/core/entity.rb, line 226
def to_header
  header = Hashie::Mash.new
  header['Category'] = "#{self.kind.term};scheme=#{self.kind.scheme.inspect};class=\"kind\";location=#{self.kind.location.inspect};title=#{self.kind.title.inspect}"

  @mixins.each do |mixin|
    scheme, term = mixin.to_s.split('#')
    scheme << '#'
    header['Category'] << ",#{term};scheme=#{scheme.inspect};class=\"mixin\";location=#{mixin.location.inspect};title=#{mixin.title ? mixin.title.inspect : ''.inspect}"
  end

  attributes = @attributes.to_header
  header['X-OCCI-Attribute'] = attributes unless attributes.blank?

  links = []
  @actions.each { |action| links << "<#{self.location}?action=#{action.term}>;rel=#{action.to_s.inspect}" }
  header['Link'] = links.join(',') if links.any?

  header
end
to_s() click to toggle source

@return [String] string representation of entity is its location

# File lib/occi4/core/entity.rb, line 247
def to_s
  self.location
end
to_text() click to toggle source

@return [String] text representation

# File lib/occi4/core/entity.rb, line 210
def to_text
  text = "Category: #{self.kind.term};scheme=#{self.kind.scheme.inspect};class=\"kind\";location=#{self.kind.location.inspect};title=#{self.kind.title.inspect}"
  @mixins.each do |mixin|
    scheme, term = mixin.to_s.split('#')
    scheme << '#'
    text << "\nCategory: #{term};scheme=#{scheme.inspect};class=\"mixin\";location=#{mixin.location.inspect};title=#{mixin.title ? mixin.title.inspect : ''.inspect}"
  end

  text << @attributes.to_text

  @actions.each { |action| text << "\nLink: <#{self.location}?action=#{action.term}>;rel=#{action.to_s.inspect}" }

  text
end