Class: OpenNebula::DocumentPoolJSON

Inherits:
DocumentPool show all
Defined in:
OpenNebula/DocumentPoolJSON.rb

Constant Summary

TEMPLATE_TAG =
"BODY"

Constants inherited from DocumentPool

OpenNebula::DocumentPool::DOCUMENT_POOL_METHODS

Constants inherited from Pool

Pool::INFO_ALL, Pool::INFO_GROUP, Pool::INFO_MINE

Instance Method Summary (collapse)

Methods inherited from DocumentPool

#document_type, #info, #info_all, #info_group, #info_mine, #initialize

Methods inherited from Pool

#each, #to_str

Methods inherited from XMLPool

#each_element, #initialize

Methods inherited from XMLElement

#[], #add_element, #attr, build_xml, #delete_element, #each, #each_xpath, #element_xml, #has_elements?, #initialize, #initialize_xml, #name, #retrieve_elements, #template_like_str, #template_str, #template_xml, #text, #to_hash, #to_xml

Constructor Details

This class inherits a constructor from OpenNebula::DocumentPool

Instance Method Details

- (Object) factory(element_xml)



22
23
24
25
26
# File 'OpenNebula/DocumentPoolJSON.rb', line 22

def factory(element_xml)
    doc = OpenNebula::DocumentJSON.new(element_xml, @client)
    doc.load_body
    doc
end

- (String) to_json(pretty_generate = true)

Generates a json representing the object

Parameters:

  • pretty_generate (true, false) (defaults to: true)

Returns:

  • (String)

    json representing the object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'OpenNebula/DocumentPoolJSON.rb', line 33

def to_json(pretty_generate=true)
    hash = self.to_hash

    if hash['DOCUMENT_POOL'] && hash['DOCUMENT_POOL']['DOCUMENT']
        if !hash['DOCUMENT_POOL']['DOCUMENT'].instance_of?(Array)
            array = [hash['DOCUMENT_POOL']['DOCUMENT']]
            hash['DOCUMENT_POOL']['DOCUMENT'] = array.compact
        end

        hash['DOCUMENT_POOL']['DOCUMENT'].each { |doc|
            body = doc['TEMPLATE']["#{TEMPLATE_TAG}"]
            if body
                b_hash = JSON.parse(body)
                doc['TEMPLATE']["#{TEMPLATE_TAG}"] = b_hash
            end
        }
    end

    if pretty_generate
        JSON.pretty_generate hash
    else
        hash.to_json
    end
end