Class: Occi::Core::Category

Inherits:
Object
  • Object
show all
Includes:
Helpers::Inspect
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

Methods included from Helpers::Inspect

#inspect

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)

17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/occi/core/category.rb', line 17

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

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes


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

def attributes
  @attributes
end

#modelObject

Returns the value of attribute model


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

def model
  @model
end

#schemeObject

Returns the value of attribute scheme


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

def scheme
  @scheme
end

#termObject

Returns the value of attribute term


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

def term
  @term
end

#titleObject

Returns the value of attribute title


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

def title
  @title
end

Class Method Details

.categoriesObject


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

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


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
72
73
74
75
76
77
78
79
80
# File 'lib/occi/core/category.rb', line 37

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.capitalize
      mod.const_get name.capitalize
    else
      mod.const_set name.capitalize, Module.new
    end
  end

  class_name =  term.gsub('-', '_').capitalize
  if namespace.const_defined? class_name
    klass = namespace.const_get class_name
    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 class_name, 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


111
112
113
114
115
116
117
118
# File 'lib/occi/core/category.rb', line 111

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

#locationNilClass

Returns category itself does not have a location

Returns:

  • (NilClass)

    category itself does not have a location


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

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


99
100
101
102
103
104
105
106
107
# File 'lib/occi/core/category.rb', line 99

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


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

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

#to_sObject


147
148
149
# File 'lib/occi/core/category.rb', line 147

def to_s
  self.type_identifier
end

#to_stringString

Returns full text representation of the category

Returns:

  • (String)

    full text representation of the category


126
127
128
129
130
# File 'lib/occi/core/category.rb', line 126

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


121
122
123
# File 'lib/occi/core/category.rb', line 121

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


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

def to_text
  'Category: ' + self.to_string
end

#type_identifierString

Returns Type identifier of the category

Returns:

  • (String)

    Type identifier of the category


88
89
90
# File 'lib/occi/core/category.rb', line 88

def type_identifier
  @scheme + @term
end