class PSON::Pure::Generator::State

This class is used to create State instances, that are use to hold data while generating a PSON text from a Ruby data structure.

Attributes

array_nl[RW]

This string is put at the end of a line that holds a PSON array.

indent[RW]

This string is used to indent levels in the PSON text.

max_nesting[RW]

This integer returns the maximum level of data structure nesting in the generated PSON, max_nesting = 0 if no maximum is checked.

object_nl[RW]

This string is put at the end of a line that holds a PSON object (or Hash).

space[RW]

This string is used to insert a space between the tokens in a PSON string.

space_before[RW]

This string is used to insert a space before the ':' in PSON objects.

Public Class Methods

from_state(opts) click to toggle source

Creates a State object from opts, which ought to be Hash to create a new State instance configured by opts, something else to create an unconfigured instance. If opts is a State object, it is just returned.

   # File lib/puppet/external/pson/pure/generator.rb
67 def self.from_state(opts)
68   case opts
69   when self
70     opts
71   when Hash
72     new(opts)
73   else
74     new
75   end
76 end
new(opts = {}) click to toggle source

Instantiates a new State object, configured by opts.

opts can have the following keys:

  • indent: a string used to indent levels (default: ''),

  • space: a string that is put after, a : or , delimiter (default: ''),

  • space_before: a string that is put before a : pair delimiter (default: ''),

  • object_nl: a string that is put at the end of a PSON object (default: ''),

  • array_nl: a string that is put at the end of a PSON array (default: ''),

  • check_circular: true if checking for circular data structures should be done (the default), false otherwise.

  • check_circular: true if checking for circular data structures should be done, false (the default) otherwise.

  • allow_nan: true if NaN, Infinity, and -Infinity should be generated, otherwise an exception is thrown, if these values are encountered. This options defaults to false.

    # File lib/puppet/external/pson/pure/generator.rb
 94 def initialize(opts = {})
 95   @seen = {}
 96   @indent         = ''
 97   @space          = ''
 98   @space_before   = ''
 99   @object_nl      = ''
100   @array_nl       = ''
101   @check_circular = true
102   @allow_nan      = false
103   configure opts
104 end

Public Instance Methods

allow_nan?() click to toggle source

Returns true if NaN, Infinity, and -Infinity should be considered as valid PSON and output.

    # File lib/puppet/external/pson/pure/generator.rb
142 def allow_nan?
143   @allow_nan
144 end
check_circular?() click to toggle source

Returns true, if circular data structures should be checked, otherwise returns false.

    # File lib/puppet/external/pson/pure/generator.rb
136 def check_circular?
137   @check_circular
138 end
configure(opts) click to toggle source

Configure this State instance with the Hash opts, and return itself.

    # File lib/puppet/external/pson/pure/generator.rb
165 def configure(opts)
166   @indent         = opts[:indent] if opts.key?(:indent)
167   @space          = opts[:space] if opts.key?(:space)
168   @space_before   = opts[:space_before] if opts.key?(:space_before)
169   @object_nl      = opts[:object_nl] if opts.key?(:object_nl)
170   @array_nl       = opts[:array_nl] if opts.key?(:array_nl)
171   @check_circular = !!opts[:check_circular] if opts.key?(:check_circular)
172   @allow_nan      = !!opts[:allow_nan] if opts.key?(:allow_nan)
173   if !opts.key?(:max_nesting) # defaults to 19
174     @max_nesting = 19
175   elsif opts[:max_nesting]
176     @max_nesting = opts[:max_nesting]
177   else
178     @max_nesting = 0
179   end
180   self
181 end
forget(object) click to toggle source

Forget object for this generating run.

    # File lib/puppet/external/pson/pure/generator.rb
159 def forget(object)
160   @seen.delete object.__id__
161 end
remember(object) click to toggle source

Remember object, to find out if it was already encountered (if a cyclic data structure is if a cyclic data structure is rendered).

    # File lib/puppet/external/pson/pure/generator.rb
154 def remember(object)
155   @seen[object.__id__] = true
156 end
seen?(object) click to toggle source

Returns true, if object was already seen during this generating run.

    # File lib/puppet/external/pson/pure/generator.rb
148 def seen?(object)
149   @seen.key?(object.__id__)
150 end
to_h() click to toggle source

Returns the configuration instance variables as a hash, that can be passed to the configure method.

    # File lib/puppet/external/pson/pure/generator.rb
185 def to_h
186   result = {}
187   for iv in %w{indent space space_before object_nl array_nl check_circular allow_nan max_nesting}
188     result[iv.intern] = instance_variable_get("@#{iv}")
189   end
190   result
191 end