Class: PSON::Pure::Generator::State
- Defined in:
- lib/vendor/puppet/external/pson/pure/generator.rb
Overview
This class is used to create State instances, that are use to hold data while generating a PSON text from a a Ruby data structure.
Instance Attribute Summary collapse
-
#array_nl ⇒ Object
This string is put at the end of a line that holds a PSON array.
-
#indent ⇒ Object
This string is used to indent levels in the PSON text.
-
#max_nesting ⇒ Object
This integer returns the maximum level of data structure nesting in the generated PSON, max_nesting = 0 if no maximum is checked.
-
#object_nl ⇒ Object
This string is put at the end of a line that holds a PSON object (or Hash).
-
#space ⇒ Object
This string is used to insert a space between the tokens in a PSON string.
-
#space_before ⇒ Object
This string is used to insert a space before the ‘:’ in PSON objects.
Class Method Summary collapse
-
.from_state(opts) ⇒ Object
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.
Instance Method Summary collapse
-
#allow_nan? ⇒ Boolean
Returns true if NaN, Infinity, and -Infinity should be considered as valid PSON and output.
-
#check_circular? ⇒ Boolean
Returns true, if circular data structures should be checked, otherwise returns false.
-
#check_max_nesting(depth) ⇒ Object
:nodoc:.
-
#configure(opts) ⇒ Object
Configure this State instance with the Hash opts, and return itself.
-
#forget(object) ⇒ Object
Forget object for this generating run.
-
#initialize(opts = {}) ⇒ State
constructor
Instantiates a new State object, configured by opts.
-
#remember(object) ⇒ Object
Remember object, to find out if it was already encountered (if a cyclic data structure is if a cyclic data structure is rendered).
-
#seen?(object) ⇒ Boolean
Returns true, if object was already seen during this generating run.
-
#to_h ⇒ Object
Returns the configuration instance variables as a hash, that can be passed to the configure method.
Constructor Details
#initialize(opts = {}) ⇒ State
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.
94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/vendor/puppet/external/pson/pure/generator.rb', line 94 def initialize(opts = {}) @seen = {} @indent = '' @space = '' @space_before = '' @object_nl = '' @array_nl = '' @check_circular = true @allow_nan = false configure opts end |
Instance Attribute Details
#array_nl ⇒ Object
This string is put at the end of a line that holds a PSON array.
121 122 123 |
# File 'lib/vendor/puppet/external/pson/pure/generator.rb', line 121 def array_nl @array_nl end |
#indent ⇒ Object
This string is used to indent levels in the PSON text.
107 108 109 |
# File 'lib/vendor/puppet/external/pson/pure/generator.rb', line 107 def indent @indent end |
#max_nesting ⇒ Object
This integer returns the maximum level of data structure nesting in the generated PSON, max_nesting = 0 if no maximum is checked.
125 126 127 |
# File 'lib/vendor/puppet/external/pson/pure/generator.rb', line 125 def max_nesting @max_nesting end |
#object_nl ⇒ Object
This string is put at the end of a line that holds a PSON object (or Hash).
118 119 120 |
# File 'lib/vendor/puppet/external/pson/pure/generator.rb', line 118 def object_nl @object_nl end |
#space ⇒ Object
This string is used to insert a space between the tokens in a PSON string.
111 112 113 |
# File 'lib/vendor/puppet/external/pson/pure/generator.rb', line 111 def space @space end |
#space_before ⇒ Object
This string is used to insert a space before the ‘:’ in PSON objects.
114 115 116 |
# File 'lib/vendor/puppet/external/pson/pure/generator.rb', line 114 def space_before @space_before end |
Class Method Details
.from_state(opts) ⇒ Object
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.
67 68 69 70 71 72 73 74 75 76 |
# File 'lib/vendor/puppet/external/pson/pure/generator.rb', line 67 def self.from_state(opts) case opts when self opts when Hash new(opts) else new end end |
Instance Method Details
#allow_nan? ⇒ Boolean
Returns true if NaN, Infinity, and -Infinity should be considered as valid PSON and output.
142 143 144 |
# File 'lib/vendor/puppet/external/pson/pure/generator.rb', line 142 def allow_nan? @allow_nan end |
#check_circular? ⇒ Boolean
Returns true, if circular data structures should be checked, otherwise returns false.
136 137 138 |
# File 'lib/vendor/puppet/external/pson/pure/generator.rb', line 136 def check_circular? @check_circular end |
#check_max_nesting(depth) ⇒ Object
:nodoc:
127 128 129 130 131 132 |
# File 'lib/vendor/puppet/external/pson/pure/generator.rb', line 127 def check_max_nesting(depth) # :nodoc: return if @max_nesting.zero? current_nesting = depth + 1 current_nesting > @max_nesting and raise NestingError, "nesting of #{current_nesting} is too deep" end |
#configure(opts) ⇒ Object
Configure this State instance with the Hash opts, and return itself.
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/vendor/puppet/external/pson/pure/generator.rb', line 165 def configure(opts) @indent = opts[:indent] if opts.key?(:indent) @space = opts[:space] if opts.key?(:space) @space_before = opts[:space_before] if opts.key?(:space_before) @object_nl = opts[:object_nl] if opts.key?(:object_nl) @array_nl = opts[:array_nl] if opts.key?(:array_nl) @check_circular = !!opts[:check_circular] if opts.key?(:check_circular) @allow_nan = !!opts[:allow_nan] if opts.key?(:allow_nan) if !opts.key?(:max_nesting) # defaults to 19 @max_nesting = 19 elsif opts[:max_nesting] @max_nesting = opts[:max_nesting] else @max_nesting = 0 end self end |
#forget(object) ⇒ Object
Forget object for this generating run.
159 160 161 |
# File 'lib/vendor/puppet/external/pson/pure/generator.rb', line 159 def forget(object) @seen.delete object.__id__ end |
#remember(object) ⇒ Object
Remember object, to find out if it was already encountered (if a cyclic data structure is if a cyclic data structure is rendered).
154 155 156 |
# File 'lib/vendor/puppet/external/pson/pure/generator.rb', line 154 def remember(object) @seen[object.__id__] = true end |
#seen?(object) ⇒ Boolean
Returns true, if object was already seen during this generating run.
148 149 150 |
# File 'lib/vendor/puppet/external/pson/pure/generator.rb', line 148 def seen?(object) @seen.key?(object.__id__) end |
#to_h ⇒ Object
Returns the configuration instance variables as a hash, that can be passed to the configure method.
185 186 187 188 189 190 191 |
# File 'lib/vendor/puppet/external/pson/pure/generator.rb', line 185 def to_h result = {} for iv in %w{indent space space_before object_nl array_nl check_circular allow_nan max_nesting} result[iv.intern] = instance_variable_get("@#{iv}") end result end |