Class: Occi::Core::Category

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

Direct Known Subclasses

Action, Kind, Mixin

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scheme = 'http://schemas.ogf.org/occi/core#', term = 'category', title = nil, attributes = Occi::Core::Attributes.new) ⇒ Category

Returns a new instance of Category.

Parameters:

  • scheme (String) (defaults to: 'http://schemas.ogf.org/occi/core#')
  • term (String) (defaults to: 'category')
  • title (String) (defaults to: nil)
  • attributes (Hash) (defaults to: Occi::Core::Attributes.new)


15
16
17
18
19
20
21
22
23
# File 'lib/occi/core/category.rb', line 15

def initialize(scheme='http://schemas.ogf.org/occi/core#',
    term='category',
    title=nil,
    attributes=Occi::Core::Attributes.new)
  @scheme = scheme
  @term = term
  @title = title
  @attributes = Occi::Core::AttributeProperties.parse attributes
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



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

def attributes
  @attributes
end

#modelObject

Returns the value of attribute model.



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

def model
  @model
end

#schemeObject

Returns the value of attribute scheme.



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

def scheme
  @scheme
end

#termObject

Returns the value of attribute term.



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

def term
  @term
end

#titleObject

Returns the value of attribute title.



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

def title
  @title
end

Class Method Details

.categoriesObject



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

def self.categories
  self.mixins + self.actions << self.kind
end

.get_class(scheme, term, related = ['http://schemas.ogf.org/occi/core#entity']) ⇒ Class

Returns ruby class with scheme as namespace, term as name and related kind as super class.

Parameters:

  • scheme (String)
  • term (String)
  • related (Array) (defaults to: ['http://schemas.ogf.org/occi/core#entity'])

Returns:

  • (Class)

    ruby class with scheme as namespace, term as name and related kind as super class



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/occi/core/category.rb', line 29

def self.get_class(scheme, term, related=['http://schemas.ogf.org/occi/core#entity'])
  related = related.to_a.flatten
  scheme += '#' unless scheme.end_with? '#'

  if related.first.to_s == 'http://schemas.ogf.org/occi/core#entity' or related.first.nil?
    parent = Occi::Core::Entity
  elsif related.first.kind_of? Occi::Core::Kind
    parent = related.first.entity_type
  elsif related.first.kind_of? Occi::Core::Mixin
    parent = related.first.class
  else
    related_scheme, related_term = related.first.to_s.split '#'
    parent = self.get_class related_scheme, related_term
  end

  uri = URI.parse(scheme)

  namespace = if uri.host == 'schemas.ogf.org'
                uri.path.reverse.chomp('/').reverse.split('/')
              else
                uri.host.split('.').reverse + uri.path.reverse.chomp('/').reverse.split('/')
              end

  namespace = namespace.inject(Object) do |mod, name|
    if mod.constants.collect { |sym| sym.to_s }.include? name.classify
      mod.const_get name.classify
    else
      mod.const_set name.classify, Module.new
    end
  end

  if namespace.const_defined? term.classify
    klass = namespace.const_get term.classify
    unless klass.ancestors.include? Occi::Core::Entity or klass.ancestors.include? Occi::Core::Category
      raise "OCCI Kind with type identifier #{scheme + term} could not be created as the corresponding class #{klass.to_s} already exists and is not derived from Occi::Core::Entity"
    end
  else
    klass = namespace.const_set term.classify, Class.new(parent)
    klass.kind = Occi::Core::Kind.new scheme, term, nil, {}, related unless parent.ancestors.include? Occi::Core::Category
  end

  klass
end

Instance Method Details

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

Returns json representation.

Parameters:

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

Returns:

  • (Hashie::Mash)

    json representation



102
103
104
105
106
107
108
109
# File 'lib/occi/core/category.rb', line 102

def as_json(options={})
  category = Hashie::Mash.new
  category.scheme = @scheme if @scheme
  category.term = @term if @term
  category.title = @title if @title
  category.attributes = @attributes if @attributes.any?
  category
end

#inspectString

Returns json representation.

Returns:

  • (String)

    json representation



134
135
136
# File 'lib/occi/core/category.rb', line 134

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

#locationNilClass

Returns category itself does not have a location.

Returns:

  • (NilClass)

    category itself does not have a location



139
140
141
# File 'lib/occi/core/category.rb', line 139

def location
  nil # not implemented
end

check if category is related to another category a category is related to another category if it is included in @related or if it is the category itself

Parameters:

  • category (String, Category)

    Related Category or its type identifier

Returns:

  • (true, false)

    true if category is related to category_id else false



90
91
92
93
94
95
96
97
98
# File 'lib/occi/core/category.rb', line 90

def related_to?(category)
  if @related
    self.related.each do |cat|
      return true if cat.to_s == category.to_s
    end
    return true if self.to_s == category.to_s
  end
  false
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



129
130
131
# File 'lib/occi/core/category.rb', line 129

def to_header
  {:Category => self.to_string}
end

#to_sObject



143
144
145
# File 'lib/occi/core/category.rb', line 143

def to_s
  self.type_identifier
end

#to_stringString

Returns full text representation of the category.

Returns:

  • (String)

    full text representation of the category



117
118
119
120
121
# File 'lib/occi/core/category.rb', line 117

def to_string
  string = self.to_string_short
  string << ';title=' + @title.inspect if @title
  string
end

#to_string_shortString

Returns short text representation of the category.

Returns:

  • (String)

    short text representation of the category



112
113
114
# File 'lib/occi/core/category.rb', line 112

def to_string_short
  @term + ';scheme=' + @scheme.inspect + ';class=' + self.class.name.demodulize.downcase.inspect
end

#to_textString

Returns text representation.

Returns:

  • (String)

    text representation



124
125
126
# File 'lib/occi/core/category.rb', line 124

def to_text
  'Category: ' + self.to_string
end

#type_identifierString

Returns Type identifier of the category.

Returns:

  • (String)

    Type identifier of the category



79
80
81
# File 'lib/occi/core/category.rb', line 79

def type_identifier
  @scheme + @term
end