Class: Occi::Core::Category
- Inherits:
-
Object
- Object
- Occi::Core::Category
- Defined in:
- lib/occi/core/category.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
Returns the value of attribute attributes.
-
#model ⇒ Object
Returns the value of attribute model.
-
#scheme ⇒ Object
Returns the value of attribute scheme.
-
#term ⇒ Object
Returns the value of attribute term.
-
#title ⇒ Object
Returns the value of attribute title.
Class Method Summary collapse
- .categories ⇒ Object
-
.get_class(scheme, term, related = ['http://schemas.ogf.org/occi/core#entity']) ⇒ Class
Ruby class with scheme as namespace, term as name and related kind as super class.
Instance Method Summary collapse
-
#as_json(options = {}) ⇒ Hashie::Mash
Json representation.
-
#initialize(scheme = 'http://schemas.ogf.org/occi/core#', term = 'category', title = nil, attributes = Occi::Core::Attributes.new) ⇒ Category
constructor
A new instance of Category.
-
#inspect ⇒ String
Json representation.
-
#location ⇒ NilClass
Category itself does not have a location.
-
#related_to?(category) ⇒ true, false
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.
-
#to_header ⇒ Hash
Hash containing the HTTP headers of the text/occi rendering.
- #to_s ⇒ Object
-
#to_string ⇒ String
Full text representation of the category.
-
#to_string_short ⇒ String
Short text representation of the category.
-
#to_text ⇒ String
Text representation.
-
#type_identifier ⇒ String
Type identifier of the category.
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.
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
#attributes ⇒ Object
Returns the value of attribute attributes.
5 6 7 |
# File 'lib/occi/core/category.rb', line 5 def attributes @attributes end |
#model ⇒ Object
Returns the value of attribute model.
5 6 7 |
# File 'lib/occi/core/category.rb', line 5 def model @model end |
#scheme ⇒ Object
Returns the value of attribute scheme.
5 6 7 |
# File 'lib/occi/core/category.rb', line 5 def scheme @scheme end |
#term ⇒ Object
Returns the value of attribute term.
5 6 7 |
# File 'lib/occi/core/category.rb', line 5 def term @term end |
#title ⇒ Object
Returns the value of attribute title.
5 6 7 |
# File 'lib/occi/core/category.rb', line 5 def title @title end |
Class Method Details
.categories ⇒ Object
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.
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, =['http://schemas.ogf.org/occi/core#entity']) = .to_a.flatten scheme += '#' unless scheme.end_with? '#' if .first.to_s == 'http://schemas.ogf.org/occi/core#entity' or .first.nil? parent = Occi::Core::Entity elsif .first.kind_of? Occi::Core::Kind parent = .first.entity_type elsif .first.kind_of? Occi::Core::Mixin parent = .first.class else , = .first.to_s.split '#' parent = self.get_class , 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, {}, unless parent.ancestors.include? Occi::Core::Category end klass end |
Instance Method Details
#as_json(options = {}) ⇒ Hashie::Mash
Returns json representation.
102 103 104 105 106 107 108 109 |
# File 'lib/occi/core/category.rb', line 102 def as_json(={}) 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 |
#inspect ⇒ String
Returns json representation.
134 135 136 |
# File 'lib/occi/core/category.rb', line 134 def inspect JSON.pretty_generate(JSON.parse(to_json)) end |
#location ⇒ NilClass
Returns category itself does not have a location.
139 140 141 |
# File 'lib/occi/core/category.rb', line 139 def location nil # not implemented end |
#related_to?(category) ⇒ true, false
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
90 91 92 93 94 95 96 97 98 |
# File 'lib/occi/core/category.rb', line 90 def (category) if @related self..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_header ⇒ Hash
Returns 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_s ⇒ Object
143 144 145 |
# File 'lib/occi/core/category.rb', line 143 def to_s self.type_identifier end |
#to_string ⇒ String
Returns 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_short ⇒ String
Returns 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_text ⇒ String
Returns text representation.
124 125 126 |
# File 'lib/occi/core/category.rb', line 124 def to_text 'Category: ' + self.to_string end |
#type_identifier ⇒ String
Returns Type identifier of the category.
79 80 81 |
# File 'lib/occi/core/category.rb', line 79 def type_identifier @scheme + @term end |