Class: Occi::Core::Renderers::Json::Category

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

Overview

Implements routines required to render `Occi::Core::Category` and its subclasses to a JSON-based representation.

Author:

Constant Summary collapse

STRING =

Typecasting constants TODO: fix this damn typing mess (add at least MACAddr, IPAddr, URI, Float, and Integer)

'string'.freeze
NUMBER =
'number'.freeze
BOOLEAN =
'boolean'.freeze
ARRAY =
'array'.freeze
OBJECT =
'object'.freeze
TYPECASTER_HASH =
{
  IPAddr => STRING, URI => STRING, String => STRING,
  Float => NUMBER, Numeric => NUMBER, Integer => NUMBER,
  Boolean => BOOLEAN, Array => ARRAY, Hash => OBJECT
}.freeze

Constants inherited from Base

Base::DELEGATED

Instance Attribute Summary

Attributes inherited from Base

#object, #options

Instance Method Summary collapse

Methods inherited from Base

#initialize, #render

Constructor Details

This class inherits a constructor from Occi::Core::Renderers::Json::Base

Instance Method Details

#add_attributes!(hash) ⇒ Object

:nodoc:


53
54
55
56
57
58
59
# File 'lib/occi/core/renderers/json/category.rb', line 53

def add_attributes!(hash)
  return if object_attributes.blank?
  hash[:attributes] = {}
  object_attributes.each_pair do |key, value|
    hash[:attributes][key] = definition_to_hash(value)
  end
end

#add_basics!(hash) ⇒ Object

:nodoc:


36
37
38
39
40
41
# File 'lib/occi/core/renderers/json/category.rb', line 36

def add_basics!(hash)
  hash[:term] = object.term
  hash[:scheme] = object.schema
  hash[:location] = object.location.to_s if object_respond_to?(:location)
  hash[:title] = object.title if object.title
end

#add_extended!(hash) ⇒ Object

:nodoc:


44
45
46
47
48
49
50
# File 'lib/occi/core/renderers/json/category.rb', line 44

def add_extended!(hash)
  hash[:parent] = object_parent.to_s if object_respond_to?(:parent) && object_parent
  %i[actions depends applies].each do |symbol|
    next unless object_respond_to?(symbol) && !object_send(symbol).blank?
    hash[symbol] = object_send(symbol).collect(&:to_s)
  end
end

#definition_to_hash(attr_defn) ⇒ Hash


63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/occi/core/renderers/json/category.rb', line 63

def definition_to_hash(attr_defn)
  hattr = {
    mutable: attr_defn.mutable?,
    required: attr_defn.required?,
    type: typecast(attr_defn.type)
  }

  hattr[:pattern] = attr_defn.pattern unless attr_defn.pattern.nil?
  hattr[:default] = attr_defn.default unless attr_defn.default.nil?
  hattr[:description] = attr_defn.description if attr_defn.description

  hattr
end

#render_hashObject

:nodoc:


27
28
29
30
31
32
33
# File 'lib/occi/core/renderers/json/category.rb', line 27

def render_hash
  hash = {}
  add_basics! hash
  add_extended! hash
  add_attributes! hash
  hash
end

#typecast(type) ⇒ Object

:nodoc:


78
79
80
# File 'lib/occi/core/renderers/json/category.rb', line 78

def typecast(type)
  TYPECASTER_HASH[type] || raise("#{self.class} -> Cannot typecast #{type.inspect} to a known OCCI type")
end