Class: Occi::Core::Renderers::Json::Collection

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

Overview

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

Author:

Constant Summary

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

#ai_no_ent?Boolean

:nodoc:

Returns:


54
55
56
# File 'lib/occi/core/renderers/json/collection.rb', line 54

def ai_no_ent?
  object_action_instances.any? && object_entities.empty?
end

#ent_no_ai?Boolean

:nodoc:

Returns:


49
50
51
# File 'lib/occi/core/renderers/json/collection.rb', line 49

def ent_no_ai?
  object_entities.any? && object_action_instances.empty?
end

#render_action_instances_hashObject

:nodoc:


41
42
43
44
45
46
# File 'lib/occi/core/renderers/json/collection.rb', line 41

def render_action_instances_hash
  if object_action_instances.count > 1
    raise Occi::Core::Errors::RenderingError, 'Cannot render multiple action instances to JSON'
  end
  ActionInstance.new(object_action_instances.first, options).render_hash
end

#render_entities_hashObject

:nodoc:


29
30
31
32
33
34
35
36
37
38
# File 'lib/occi/core/renderers/json/collection.rb', line 29

def render_entities_hash
  hsh = {}
  if object_resources.any?
    hsh[:resources] = object_resources.collect { |r| Resource.new(r, options).render_hash }
  end
  if object_links.any?
    hsh[:links] = object_links.collect { |r| Link.new(r, options).render_hash }
  end
  hsh
end

#render_hashObject

:nodoc:


16
17
18
19
20
21
22
23
24
25
26
# File 'lib/occi/core/renderers/json/collection.rb', line 16

def render_hash
  return {} if object_empty? || object.only_categories?

  if ent_no_ai?
    render_entities_hash
  elsif ai_no_ent?
    render_action_instances_hash
  else
    raise Occi::Core::Errors::RenderingError, 'Cannot render mixed collection to JSON'
  end
end