class Occi::Core::Category

Attributes

attributes[RW]
model[RW]
scheme[RW]
term[RW]
title[RW]

Public Class Methods

new(scheme='http://schemas.ogf.org/occi/core click to toggle source

@param scheme [String] The categorisation scheme. @param term [String] Unique identifier of the Category instance within the categorisation scheme. @param title [String] The display name of an instance. @param attributes [Occi::Core::Attributes,Hash] Set of Attribute instances.

# File lib/occi/core/category.rb, line 14
def initialize(scheme='http://schemas.ogf.org/occi/core#',
    term='category',
    title=nil,
    attributes=nil)
  raise ArgumentError, 'Scheme and term cannot be empty' if scheme.blank? || term.blank?
  attributes ||= Occi::Core::Attributes.new

  scheme << '#' unless scheme.end_with? '#'
  @scheme = scheme
  @term = term
  @title = title

  case attributes
    when Occi::Core::Attributes
      @attributes = Occi::Core::Attributes.new(attributes)
    else
      @attributes = Occi::Core::Attributes.parse_properties attributes
  end
end
valid_term?(term) click to toggle source

@param term [String] Term to check. @return [Boolean] Indicating whether term consists exclusively of valid characters.

# File lib/occi/core/category.rb, line 105
def self.valid_term?(term)
  term =~ /^[a-z][a-z0-9_-]*$/
end

Public Instance Methods

as_json(options={}) click to toggle source

@param options [Hash] @return [Hashie::Mash] JSON representation of Category.

# File lib/occi/core/category.rb, line 41
def as_json(options={})
  category = Hashie::Mash.new
  category.scheme = self.scheme
  category.term = self.term
  category.title = self.title if self.title
  category.attributes = self.attributes.any? ? self.attributes.as_json : Occi::Core::Attributes.new.as_json
  category
end
check(check_attributes = false, check_title = false) click to toggle source

Checks this category against the model @param check_attributes [Boolean] attribute definitions must match @param check_title [Boolean] titles must match @return [Boolean]

# File lib/occi/core/category.rb, line 91
def check(check_attributes = false, check_title = false)
  raise ArgumentError, 'No model has been assigned to this category' unless @model

  cat = @model.get_by_id(type_identifier, true)
  raise Occi::Errors::CategoryNotDefinedError,
        "Category #{self.class.name}[#{type_identifier.inspect}] not found in the model!" unless cat

  # TODO: impl. check_attributes and check_title for strict matching

  true
end
empty?() click to toggle source

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

# File lib/occi/core/category.rb, line 83
def empty?
  term.blank? || scheme.blank?
end
location() click to toggle source

@return [NilClass] Returns nil as Category itself does not have a location.

# File lib/occi/core/category.rb, line 73
def location
  nil # not implemented
end
to_header() click to toggle source

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

# File lib/occi/core/category.rb, line 68
def to_header
  {:Category => self.to_string}
end
to_s() click to toggle source

@return [String] Type Identififier of the Category.

# File lib/occi/core/category.rb, line 78
def to_s
  self.type_identifier
end
to_string() click to toggle source

@return [String] Full text representation of the Category.

# File lib/occi/core/category.rb, line 56
def to_string
  string = self.to_string_short
  string << ";title=#{self.title.inspect}" if self.title
  string
end
to_string_short() click to toggle source

@return [String] Short text representation of the Category.

# File lib/occi/core/category.rb, line 51
def to_string_short
  "#{self.term};scheme=#{self.scheme.inspect};class=#{self.class.name.demodulize.downcase.inspect}"
end
to_text() click to toggle source

@return [String] Text representation of the Category.

# File lib/occi/core/category.rb, line 63
def to_text
  "Category: #{self.to_string}"
end
type_identifier() click to toggle source

@return [String] Type identifier of the Category.

# File lib/occi/core/category.rb, line 35
def type_identifier
  "#{self.scheme}#{self.term}"
end