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
30 def initialize(catalog, pretty: true, exclude_virtual: true)
31   @catalog = catalog
32   @pretty = pretty
33   @exclude_virtual = exclude_virtual
34 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
40 def encode
41   possibly_filtered_catalog.to_json(:pretty => pretty)
42 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
50 def encode_resource(type, title)
51   # Ensure that both type and title are given since the underlying API will do mysterious things
52   # if 'title' is nil. (Other assertions are made by the catalog when looking up the resource).
53   #
54   # TRANSLATORS 'type' and 'title' are internal parameter names - do not translate
55   raise ArgumentError, _("Both type and title must be given") if type.nil? or title.nil?
56   r = possibly_filtered_catalog.resource(type, title)
57   return nil if r.nil?
58   r.to_data_hash.to_json(:pretty => pretty)
59 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
66 def possibly_filtered_catalog
67   @filtered ||= (exclude_virtual ? catalog.filter { |r| r.virtual? } : catalog)
68 end