class Puppet::Pal::JsonCatalogEncoder

Attributes

catalog[R]

The internal catalog being build - what this class wraps with a public API.

exclude_virtual[R]

Should unrealized virtual resources be included in the result or not.

pretty[R]

Is the resulting Json pretty printed or not.

Public Class Methods

new(catalog, pretty: true, exclude_virtual: true) click to toggle source

Do not instantiate this class directly! Use the `Pal::CatalogCompiler#with_json_encoding` method instead.

@param catalog [Puppet::Resource::Catalog] the internal catalog that this class wraps @param pretty [Boolean] (true), if the resulting JSON should be pretty printed or not @param exclude_virtual [Boolean] (true), if the resulting catalog should contain unrealzed virtual resources or not

@api private

   # File lib/puppet/pal/json_catalog_encoder.rb
29 def initialize(catalog, pretty: true, exclude_virtual: true)
30   @catalog = catalog
31   @pretty = pretty
32   @exclude_virtual = exclude_virtual
33 end

Public Instance Methods

encode() click to toggle source

Encodes the entire catalog as a rich-data Json catalog. @return String The catalog in Json format using rich data format @api public

   # File lib/puppet/pal/json_catalog_encoder.rb
39 def encode
40   possibly_filtered_catalog.to_json(:pretty => pretty)
41 end
encode_resource(type, title) click to toggle source

Returns one particular resource as a Json string, or returns nil if resource was not found. @param type [String] the name of the puppet type (case independent) @param title [String] the title of the wanted resource @return [String] the resulting Json text @api public

   # File lib/puppet/pal/json_catalog_encoder.rb
49 def encode_resource(type, title)
50   # Ensure that both type and title are given since the underlying API will do mysterious things
51   # if 'title' is nil. (Other assertions are made by the catalog when looking up the resource).
52   #
53   # TRANSLATORS 'type' and 'title' are internal parameter names - do not translate
54   raise ArgumentError, _("Both type and title must be given") if type.nil? or title.nil?
55   r = possibly_filtered_catalog.resource(type, title)
56   return nil if r.nil?
57   r.to_data_hash.to_json(:pretty => pretty)
58 end

Private Instance Methods

possibly_filtered_catalog() click to toggle source

Applies a filter for virtual resources and returns filtered catalog or the catalog itself if filtering was not needed. The result is cached. @api private

   # File lib/puppet/pal/json_catalog_encoder.rb
65 def possibly_filtered_catalog
66   @filtered ||= (exclude_virtual ? catalog.filter { |r| r.virtual? } : catalog)
67 end