class Puppet::Pops::Types::TypeFormatter

String


Creates a string representation of a type.

@api public

Constants

COMMA_SEP
HASH_ENTRY_OP
NAME_SEGMENT_SEPARATOR
STARTS_WITH_ASCII_CAPITAL

Public Class Methods

new() click to toggle source
   # File lib/puppet/pops/types/type_formatter.rb
24 def initialize
25   @string_visitor = Visitor.new(nil, 'string',0,0)
26 end
string(t) click to toggle source

Produces a String representation of the given type. @param t [PAnyType] the type to produce a string form @return [String] the type in string form

@api public

   # File lib/puppet/pops/types/type_formatter.rb
20 def self.string(t)
21   singleton.string(t)
22 end

Public Instance Methods

alias_expanded_string(t) click to toggle source

Produces a string representing the type where type aliases have been expanded @api public

    # File lib/puppet/pops/types/type_formatter.rb
122 def alias_expanded_string(t)
123   @expanded = true
124   begin
125     string(t)
126   ensure
127     @expanded = false
128   end
129 end
append_callable_params(t) click to toggle source
    # File lib/puppet/pops/types/type_formatter.rb
295 def append_callable_params(t)
296   # translate to string, and skip Unit types
297   append_strings(t.param_types.types.reject {|t2| t2.class == PUnitType }, true)
298 
299   if t.param_types.types.empty?
300     append_strings([0, 0], true)
301   else
302     append_elements(range_array_part(t.param_types.size_type), true)
303   end
304 
305   # Add block T last (after min, max) if present)
306   #
307   append_strings([t.block_type], true) unless t.block_type.nil?
308   chomp_list
309 end
append_default() click to toggle source
    # File lib/puppet/pops/types/type_formatter.rb
100 def append_default
101   @bld << 'default'
102 end
append_indented_string(t, indent = 0, indent_width = 2, skip_initial_indent = false) click to toggle source

@api private

   # File lib/puppet/pops/types/type_formatter.rb
72 def append_indented_string(t, indent = 0, indent_width = 2, skip_initial_indent = false)
73   save_indent = @indent
74   save_indent_width = @indent_width
75   @indent = indent
76   @indent_width = indent_width
77   begin
78     (@indent * @indent_width).times { @bld << ' ' } unless skip_initial_indent
79     append_string(t)
80     @bld << "\n"
81   ensure
82     @indent = save_indent
83     @indent_width = save_indent_width
84   end
85 end
append_string(t) click to toggle source
    # File lib/puppet/pops/types/type_formatter.rb
104 def append_string(t)
105   if @ruby && t.is_a?(PAnyType)
106     @ruby = false
107     begin
108       @bld << @ref_ctor << '('
109       @string_visitor.visit_this_0(self, TypeFormatter.new.string(t))
110       @bld << ')'
111     ensure
112       @ruby = true
113     end
114   else
115     @string_visitor.visit_this_0(self, t)
116   end
117 end
capitalize_segments(qualified_name) click to toggle source

Capitalizes each segment in a name separated with the {NAME_SEPARATOR} conditionally. The name will not be subject to capitalization if it already starts with a capital letter. This to avoid that existing camel casing is lost.

@param qualified_name [String] the name to capitalize @return [String] the capitalized name

@api private

    # File lib/puppet/pops/types/type_formatter.rb
648 def capitalize_segments(qualified_name)
649   if !qualified_name.is_a?(String) || qualified_name =~ STARTS_WITH_ASCII_CAPITAL
650     qualified_name
651   else
652     segments = qualified_name.split(NAME_SEGMENT_SEPARATOR)
653     if segments.size == 1
654       qualified_name.capitalize
655     else
656       segments.each(&:capitalize!)
657       segments.join(NAME_SEGMENT_SEPARATOR)
658     end
659   end
660 end
debug_string(t) click to toggle source

Produces a debug string representing the type (possibly with more information that the regular string format) @api public

    # File lib/puppet/pops/types/type_formatter.rb
134 def debug_string(t)
135   @debug = true
136   begin
137     string(t)
138   ensure
139     @debug = false
140   end
141 end
expanded() click to toggle source
   # File lib/puppet/pops/types/type_formatter.rb
28 def expanded
29   tf = clone
30   tf.instance_variable_set(:@expanded, true)
31   tf
32 end
format_type_alias_type(t, expand) click to toggle source

@api private

    # File lib/puppet/pops/types/type_formatter.rb
550 def format_type_alias_type(t, expand)
551   if @type_set.nil?
552     @bld << t.name
553     if expand && !Loader::StaticLoader::BUILTIN_ALIASES.include?(t.name)
554       @bld << ' = '
555       append_string(t.resolved_type)
556     end
557   else
558     if expand && @type_set.defines_type?(t)
559       append_string(t.resolved_type)
560     else
561       @bld << @type_set.name_for(t, t.name)
562     end
563   end
564 end
indented(indent = 0, indent_width = 2) click to toggle source
   # File lib/puppet/pops/types/type_formatter.rb
34 def indented(indent = 0, indent_width = 2)
35   tf = clone
36   tf.instance_variable_set(:@indent, indent)
37   tf.instance_variable_set(:@indent_width, indent_width)
38   tf
39 end
indented_string(t, indent = 0, indent_width = 2) click to toggle source

Produces an string containing newline characters and indentation that represents the given type or literal t.

@param t [Object] the type or literal to produce a string for @param indent [Integer] the current indentation level @param indent_width [Integer] the number of spaces to use for one indentation

@api public

   # File lib/puppet/pops/types/type_formatter.rb
65 def indented_string(t, indent = 0, indent_width = 2)
66   @bld = ''
67   append_indented_string(t, indent, indent_width)
68   @bld
69 end
ruby(ref_ctor) click to toggle source
   # File lib/puppet/pops/types/type_formatter.rb
41 def ruby(ref_ctor)
42   tf = clone
43   tf.instance_variable_set(:@ruby, true)
44   tf.instance_variable_set(:@ref_ctor, ref_ctor)
45   tf
46 end
ruby_string(ref_ctor, indent, t) click to toggle source

@api private

   # File lib/puppet/pops/types/type_formatter.rb
88 def ruby_string(ref_ctor, indent, t)
89   @ruby = true
90   @ref_ctor = ref_ctor
91   begin
92     indented_string(t, indent)
93   ensure
94     @ruby = nil
95     @ref_ctor = nil
96   end
97 end
string(t) click to toggle source

Produces a string representing the type @api public

   # File lib/puppet/pops/types/type_formatter.rb
51 def string(t)
52   @bld = ''
53   append_string(t)
54   @bld
55 end
string_Array(t) click to toggle source

@api private

    # File lib/puppet/pops/types/type_formatter.rb
572 def string_Array(t)
573   append_array('') do
574     if @indent && !is_short_array?(t)
575       @indent += 1
576       t.each { |elem| newline; append_string(elem); @bld << COMMA_SEP }
577       chomp_list
578       @indent -= 1
579       newline
580     else
581       append_strings(t)
582     end
583   end
584 end
string_FalseClass(t) click to toggle source

@api private

    # File lib/puppet/pops/types/type_formatter.rb
587 def string_FalseClass(t)   ; @bld << 'false'       ; end
string_Hash(t) click to toggle source

@api private

    # File lib/puppet/pops/types/type_formatter.rb
590 def string_Hash(t)
591   append_hash(t)
592 end
string_Module(t) click to toggle source

@api private

    # File lib/puppet/pops/types/type_formatter.rb
595 def string_Module(t)
596   append_string(TypeCalculator.singleton.type(t))
597 end
string_NilClass(t) click to toggle source

@api private

    # File lib/puppet/pops/types/type_formatter.rb
600 def string_NilClass(t)     ; @bld << (@ruby ? 'nil' : 'undef') ; end
string_Numeric(t) click to toggle source

@api private

    # File lib/puppet/pops/types/type_formatter.rb
603 def string_Numeric(t)      ; @bld << t.to_s    ; end
string_Object(t) click to toggle source
    # File lib/puppet/pops/types/type_formatter.rb
341 def string_Object(t)
342   type = TypeCalculator.infer(t)
343   if type.is_a?(PObjectTypeExtension)
344     type = type.base_type
345   end
346   if type.is_a?(PObjectType)
347     init_hash = type.extract_init_hash(t)
348     @bld << type.name << '('
349     if @indent
350       append_indented_string(init_hash, @indent, @indent_width, true)
351       @bld.chomp!
352     else
353       append_string(init_hash)
354     end
355     @bld << ')'
356   else
357     @bld << 'Instance of '
358     append_string(type)
359   end
360 end
string_PAnnotatedMember(m) click to toggle source

@api private

    # File lib/puppet/pops/types/type_formatter.rb
457 def string_PAnnotatedMember(m)
458   hash = m._pcore_init_hash
459   if hash.size == 1
460     string(m.type)
461   else
462     string(hash)
463   end
464 end
string_PAnyType(_) click to toggle source

@api private

    # File lib/puppet/pops/types/type_formatter.rb
144 def string_PAnyType(_)     ; @bld << 'Any'     ; end
string_PArrayType(t) click to toggle source

@api private

    # File lib/puppet/pops/types/type_formatter.rb
400 def string_PArrayType(t)
401   if t.has_empty_range?
402     append_array('Array') { append_strings([0, 0]) }
403   else
404     append_array('Array', t == PArrayType::DEFAULT) do
405       append_strings([t.element_type], true)
406       append_elements(range_array_part(t.size_type), true)
407       chomp_list
408     end
409   end
410 end
string_PBinaryType(_) click to toggle source

@api private

    # File lib/puppet/pops/types/type_formatter.rb
167 def string_PBinaryType(_)  ; @bld << 'Binary' ; end
string_PBooleanType(t) click to toggle source

@api private

    # File lib/puppet/pops/types/type_formatter.rb
153 def string_PBooleanType(t)
154   append_array('Boolean', t.value.nil?) { append_string(t.value) }
155 end
string_PCallableType(t) click to toggle source

@api private

    # File lib/puppet/pops/types/type_formatter.rb
279 def string_PCallableType(t)
280   if t.return_type.nil?
281     append_array('Callable', t.param_types.nil?) { append_callable_params(t) }
282   else
283     if t.param_types.nil?
284       append_array('Callable', false) { append_strings([[], t.return_type], false) }
285     else
286       append_array('Callable', false) do
287         append_array('', false) { append_callable_params(t) }
288         @bld << COMMA_SEP
289         append_string(t.return_type)
290       end
291     end
292   end
293 end
string_PCatalogEntryType(_) click to toggle source

@api private

    # File lib/puppet/pops/types/type_formatter.rb
426 def string_PCatalogEntryType(_)
427   @bld << 'CatalogEntry'
428 end
string_PClassType(t) click to toggle source

@api private

    # File lib/puppet/pops/types/type_formatter.rb
431 def string_PClassType(t)
432   append_array('Class', t.class_name.nil?) { append_elements([t.class_name]) }
433 end
string_PCollectionType(t) click to toggle source

@api private

    # File lib/puppet/pops/types/type_formatter.rb
336 def string_PCollectionType(t)
337   range = range_array_part(t.size_type)
338   append_array('Collection', range.empty? ) { append_elements(range) }
339 end
string_PDefaultType(_) click to toggle source

@api private

    # File lib/puppet/pops/types/type_formatter.rb
150 def string_PDefaultType(_) ; @bld << 'Default' ; end
string_PEnumType(t) click to toggle source

@api private

    # File lib/puppet/pops/types/type_formatter.rb
218 def string_PEnumType(t)
219   append_array('Enum', t.values.empty?) do
220     append_strings(t.values)
221     if t.case_insensitive?
222       @bld << COMMA_SEP
223       append_string(true)
224     end
225   end
226 end
string_PFloatType(t) click to toggle source

@api private

    # File lib/puppet/pops/types/type_formatter.rb
195 def string_PFloatType(t)
196   append_array('Float', t.unbounded? ) { append_elements(range_array_part(t)) }
197 end
string_PHashType(t) click to toggle source

@api private

    # File lib/puppet/pops/types/type_formatter.rb
413 def string_PHashType(t)
414   if t.has_empty_range?
415     append_array('Hash') { append_strings([0, 0]) }
416   else
417     append_array('Hash', t == PHashType::DEFAULT) do
418       append_strings([t.key_type, t.value_type], true)
419       append_elements(range_array_part(t.size_type), true)
420       chomp_list
421     end
422   end
423 end
string_PInitType(t) click to toggle source

@api private

    # File lib/puppet/pops/types/type_formatter.rb
180 def string_PInitType(t)
181   append_array('Init', t.type.nil?)  { append_strings([t.type, *t.init_args]) }
182 end
string_PIntegerType(t) click to toggle source

@api private

    # File lib/puppet/pops/types/type_formatter.rb
170 def string_PIntegerType(t)
171   append_array('Integer', t.unbounded?) { append_elements(range_array_part(t)) }
172 end
string_PIterableType(t) click to toggle source

@api private

    # File lib/puppet/pops/types/type_formatter.rb
185 def string_PIterableType(t)
186   append_array('Iterable', t.element_type.nil?)  { append_string(t.element_type) }
187 end
string_PIteratorType(t) click to toggle source

@api private

    # File lib/puppet/pops/types/type_formatter.rb
190 def string_PIteratorType(t)
191   append_array('Iterator', t.element_type.nil?) { append_string(t.element_type) }
192 end
string_PNotUndefType(t) click to toggle source

@api private

    # File lib/puppet/pops/types/type_formatter.rb
445 def string_PNotUndefType(t)
446   contained_type = t.type
447   append_array('NotUndef', contained_type.nil? || contained_type.class == PAnyType) do
448     if contained_type.is_a?(PStringType) && !contained_type.value.nil?
449       append_string(contained_type.value)
450     else
451       append_string(contained_type)
452     end
453   end
454 end
string_PNumericType(_) click to toggle source

@api private

    # File lib/puppet/pops/types/type_formatter.rb
164 def string_PNumericType(_) ; @bld << 'Numeric' ; end
string_PObjectType(t) click to toggle source

@api private

    # File lib/puppet/pops/types/type_formatter.rb
502 def string_PObjectType(t)
503   if @expanded
504     append_object_hash(t._pcore_init_hash(@type_set.nil? || !@type_set.defines_type?(t)))
505   else
506     @bld << (@type_set ? @type_set.name_for(t, t.label) : t.label)
507   end
508 end
string_PObjectTypeExtension(t) click to toggle source
    # File lib/puppet/pops/types/type_formatter.rb
510 def string_PObjectTypeExtension(t)
511   append_array(@type_set ? @type_set.name_for(t, t.name) : t.name, false) do
512     ips = t.init_parameters
513     if ips.is_a?(Array)
514       append_strings(ips)
515     else
516       append_string(ips)
517     end
518   end
519 end
string_POptionalType(t) click to toggle source

@api private

    # File lib/puppet/pops/types/type_formatter.rb
527 def string_POptionalType(t)
528   optional_type = t.optional_type
529   append_array('Optional', optional_type.nil?) do
530     if optional_type.is_a?(PStringType) && !optional_type.value.nil?
531       append_string(optional_type.value)
532     else
533       append_string(optional_type)
534     end
535   end
536 end
string_PPatternType(t) click to toggle source

@api private

    # File lib/puppet/pops/types/type_formatter.rb
330 def string_PPatternType(t)
331   append_array('Pattern', t.patterns.empty?) { append_strings(t.patterns.map(&:regexp)) }
332 end
string_PRegexpType(t) click to toggle source

@api private

    # File lib/puppet/pops/types/type_formatter.rb
200 def string_PRegexpType(t)
201   append_array('Regexp', t.pattern.nil?) { append_string(t.regexp) }
202 end
string_PResourceType(t) click to toggle source

@api private

    # File lib/puppet/pops/types/type_formatter.rb
436 def string_PResourceType(t)
437   if t.type_name
438     append_array(capitalize_segments(t.type_name), t.title.nil?) { append_string(t.title) }
439   else
440     @bld << 'Resource'
441   end
442 end
string_PRuntimeType(t) click to toggle source

@api private

    # File lib/puppet/pops/types/type_formatter.rb
395 def string_PRuntimeType(t)
396   append_array('Runtime', t.runtime.nil? && t.name_or_pattern.nil?) { append_strings([t.runtime, t.name_or_pattern]) }
397 end
string_PScalarDataType(_) click to toggle source

@api private

    # File lib/puppet/pops/types/type_formatter.rb
161 def string_PScalarDataType(_)  ; @bld << 'ScalarData'  ; end
string_PScalarType(_) click to toggle source

@api private

    # File lib/puppet/pops/types/type_formatter.rb
158 def string_PScalarType(_)  ; @bld << 'Scalar'  ; end
string_PSemVerRangeType(t) click to toggle source

@api private

    # File lib/puppet/pops/types/type_formatter.rb
239 def string_PSemVerRangeType(t)
240   @bld << 'SemVerRange'
241 end
string_PSemVerType(t) click to toggle source

@api private

    # File lib/puppet/pops/types/type_formatter.rb
234 def string_PSemVerType(t)
235   append_array('SemVer', t.ranges.empty?) { append_strings(t.ranges) }
236 end
string_PSensitiveType(t) click to toggle source

@api private

    # File lib/puppet/pops/types/type_formatter.rb
522 def string_PSensitiveType(t)
523   append_array('Sensitive', PAnyType::DEFAULT == t.type) { append_string(t.type) }
524 end
string_PStringType(t) click to toggle source

@api private

    # File lib/puppet/pops/types/type_formatter.rb
205 def string_PStringType(t)
206   range = range_array_part(t.size_type)
207   append_array('String', range.empty? && !(@debug && !t.value.nil?)) do
208     if @debug
209       append_elements(range, !t.value.nil?)
210       append_string(t.value) unless t.value.nil?
211     else
212       append_elements(range)
213     end
214   end
215 end
string_PStructType(t) click to toggle source

@api private

    # File lib/puppet/pops/types/type_formatter.rb
312 def string_PStructType(t)
313   append_array('Struct', t.elements.empty?) { append_hash(Hash[t.elements.map {|e| struct_element_pair(e) }]) }
314 end
string_PTimespanType(t) click to toggle source

@api private

    # File lib/puppet/pops/types/type_formatter.rb
257 def string_PTimespanType(t)
258   min = t.from
259   max = t.to
260   append_array('Timespan', min.nil? && max.nil?) do
261     min.nil? ? append_default : append_string(min)
262     unless max.nil? || max == min
263       @bld << COMMA_SEP
264       append_string(max)
265     end
266   end
267 end
string_PTimestampType(t) click to toggle source

@api private

    # File lib/puppet/pops/types/type_formatter.rb
244 def string_PTimestampType(t)
245   min = t.from
246   max = t.to
247   append_array('Timestamp', min.nil? && max.nil?) do
248     min.nil? ? append_default : append_string(min)
249     unless max.nil? || max == min
250       @bld << COMMA_SEP
251       append_string(max)
252     end
253   end
254 end
string_PTupleType(t) click to toggle source

@api private

    # File lib/puppet/pops/types/type_formatter.rb
270 def string_PTupleType(t)
271   append_array('Tuple', t.types.empty?) do
272     append_strings(t.types, true)
273     append_elements(range_array_part(t.size_type), true)
274     chomp_list
275   end
276 end
string_PTypeAliasType(t) click to toggle source

@api private

    # File lib/puppet/pops/types/type_formatter.rb
539 def string_PTypeAliasType(t)
540   expand = @expanded
541   if expand && t.self_recursion?
542     @guard ||= RecursionGuard.new
543     @guard.with_this(t) { |state| format_type_alias_type(t, (state & RecursionGuard::SELF_RECURSION_IN_THIS) == 0) }
544   else
545     format_type_alias_type(t, expand)
546   end
547 end
string_PTypeReferenceType(t) click to toggle source

@api private

    # File lib/puppet/pops/types/type_formatter.rb
567 def string_PTypeReferenceType(t)
568   append_array('TypeReference') { append_string(t.type_string) }
569 end
string_PTypeSetType(t) click to toggle source

@api private

    # File lib/puppet/pops/types/type_formatter.rb
474 def string_PTypeSetType(t)
475   append_array('TypeSet') do
476     append_hash(t._pcore_init_hash.each, proc { |k| @bld << symbolic_key(k) }) do |k,v|
477       case k
478       when KEY_TYPES
479         old_ts = @type_set
480         @type_set = t
481         begin
482           append_hash(v, proc { |tk| @bld << symbolic_key(tk) }) do |tk, tv|
483             if tv.is_a?(Hash)
484               append_object_hash(tv)
485             else
486               append_string(tv)
487             end
488           end
489         rescue
490           @type_set = old_ts
491         end
492       when KEY_REFERENCES
493         append_hash(v, proc { |tk| @bld << symbolic_key(tk) })
494       else
495         append_string(v)
496       end
497     end
498   end
499 end
string_PTypeType(t) click to toggle source

@api private

    # File lib/puppet/pops/types/type_formatter.rb
175 def string_PTypeType(t)
176   append_array('Type', t.type.nil?) { append_string(t.type) }
177 end
string_PURIType(t) click to toggle source

@api private

    # File lib/puppet/pops/types/type_formatter.rb
374 def string_PURIType(t)
375   append_array('URI', t.parameters.nil?) { append_string(t._pcore_init_hash['parameters']) }
376 end
string_PUndefType(_) click to toggle source

@api private

    # File lib/puppet/pops/types/type_formatter.rb
147 def string_PUndefType(_)   ; @bld << 'Undef'   ; end
string_PUnitType(_) click to toggle source

@api private

    # File lib/puppet/pops/types/type_formatter.rb
390 def string_PUnitType(_)
391   @bld << 'Unit'
392 end
string_PVariantType(t) click to toggle source

@api private

    # File lib/puppet/pops/types/type_formatter.rb
229 def string_PVariantType(t)
230   append_array('Variant', t.types.empty?) { append_strings(t.types) }
231 end
string_PuppetObject(t) click to toggle source
    # File lib/puppet/pops/types/type_formatter.rb
362 def string_PuppetObject(t)
363   @bld << t._pcore_type.name << '('
364   if @indent
365     append_indented_string(t._pcore_init_hash, @indent, @indent_width, true)
366     @bld.chomp!
367   else
368     append_string(t._pcore_init_hash)
369   end
370   @bld << ')'
371 end
string_Regexp(t) click to toggle source

@api private

    # File lib/puppet/pops/types/type_formatter.rb
606 def string_Regexp(t)       ; @bld << PRegexpType.regexp_to_s_with_delimiters(t); end
string_String(t) click to toggle source

@api private

    # File lib/puppet/pops/types/type_formatter.rb
609 def string_String(t)
610   # Use single qoute on strings that does not contain single quotes, control characters, or backslashes.
611   @bld << StringConverter.singleton.puppet_quote(t)
612 end
string_Symbol(t) click to toggle source

@api private

    # File lib/puppet/pops/types/type_formatter.rb
615 def string_Symbol(t)       ; @bld << t.to_s    ; end
string_Timespan(t) click to toggle source

@api private

    # File lib/puppet/pops/types/type_formatter.rb
627 def string_Timespan(t)    ; @bld << "'#{t}'"  ; end
string_Timestamp(t) click to toggle source

@api private

    # File lib/puppet/pops/types/type_formatter.rb
630 def string_Timestamp(t)    ; @bld << "'#{t}'"  ; end
string_TrueClass(t) click to toggle source

@api private

    # File lib/puppet/pops/types/type_formatter.rb
618 def string_TrueClass(t)    ; @bld << 'true'    ; end
string_URI(t) click to toggle source
    # File lib/puppet/pops/types/type_formatter.rb
378 def string_URI(t)
379   @bld << 'URI('
380   if @indent
381     append_indented_string(t.to_s, @indent, @indent_width, true)
382     @bld.chomp!
383   else
384     append_string(t.to_s)
385   end
386   @bld << ')'
387 end
string_Version(t) click to toggle source

@api private

    # File lib/puppet/pops/types/type_formatter.rb
621 def string_Version(t)      ; @bld << "'#{t}'"  ; end
string_VersionRange(t) click to toggle source

@api private

    # File lib/puppet/pops/types/type_formatter.rb
624 def string_VersionRange(t) ; @bld << "'#{t}'"  ; end
struct_element_pair(t) click to toggle source

@api private

    # File lib/puppet/pops/types/type_formatter.rb
317 def struct_element_pair(t)
318   k = t.key_type
319   value_optional = t.value_type.assignable?(PUndefType::DEFAULT)
320   if k.is_a?(POptionalType)
321     # Output as literal String
322     k = t.name if value_optional
323   else
324     k = value_optional ? PNotUndefType.new(k) : t.name
325   end
326   [k, t.value_type]
327 end
symbolic_key(key) click to toggle source

Used when printing names of well known keys in an Object type. Placed in a separate method to allow override. @api private

    # File lib/puppet/pops/types/type_formatter.rb
469 def symbolic_key(key)
470   @ruby ? "'#{key}'" : key
471 end
to_s() click to toggle source

Debugging to_s to reduce the amount of output

    # File lib/puppet/pops/types/type_formatter.rb
633 def to_s
634   '[a TypeFormatter]'
635 end

Private Instance Methods

append_array(start, empty = false) { || ... } click to toggle source
    # File lib/puppet/pops/types/type_formatter.rb
754 def append_array(start, empty = false)
755   @bld << start
756   unless empty
757     @bld << '['
758     yield
759     @bld << ']'
760   end
761 end
append_elements(array, to_be_continued = false) click to toggle source
    # File lib/puppet/pops/types/type_formatter.rb
727 def append_elements(array, to_be_continued = false)
728   case array.size
729   when 0
730   when 1
731     @bld << array[0]
732     @bld << COMMA_SEP if to_be_continued
733   else
734     array.each { |elem| @bld << elem << COMMA_SEP }
735     chomp_list unless to_be_continued
736   end
737 end
append_hash(hash, key_proc = nil) { |k, v| ... } click to toggle source
    # File lib/puppet/pops/types/type_formatter.rb
763 def append_hash(hash, key_proc = nil)
764   @bld << '{'
765   @indent += 1 if @indent
766   hash.each do |k, v|
767     newline if @indent
768     if key_proc.nil?
769       append_string(k)
770     else
771       key_proc.call(k)
772     end
773     @bld << HASH_ENTRY_OP
774     if block_given?
775       yield(k, v)
776     else
777       append_string(v)
778     end
779     @bld << COMMA_SEP
780   end
781   chomp_list
782   if @indent
783     @indent -= 1
784     newline
785   end
786   @bld << '}'
787 end
append_object_hash(hash) click to toggle source
    # File lib/puppet/pops/types/type_formatter.rb
693 def append_object_hash(hash)
694   begin
695     @expanded = false
696     append_array('Object') do
697       append_hash(hash, proc { |k| @bld << symbolic_key(k) }) do |k,v|
698         case k
699         when KEY_ATTRIBUTES, KEY_FUNCTIONS
700           # Types might need to be output as type references
701           append_hash(v) do |_, fv|
702             if fv.is_a?(Hash)
703               append_hash(fv, proc { |fak| @bld << symbolic_key(fak) }) do |fak,fav|
704                 case fak
705                 when KEY_KIND
706                   @bld << fav
707                 else
708                   append_string(fav)
709                 end
710               end
711             else
712               append_string(fv)
713             end
714           end
715         when KEY_EQUALITY
716           append_array('') { append_strings(v) } if v.is_a?(Array)
717         else
718           append_string(v)
719         end
720       end
721     end
722   ensure
723     @expanded = true
724   end
725 end
append_strings(array, to_be_continued = false) click to toggle source
    # File lib/puppet/pops/types/type_formatter.rb
739 def append_strings(array, to_be_continued = false)
740   case array.size
741   when 0
742   when 1
743     append_string(array[0])
744     @bld << COMMA_SEP if to_be_continued
745   else
746     array.each do |elem|
747       append_string(elem)
748       @bld << COMMA_SEP
749     end
750     chomp_list unless to_be_continued
751   end
752 end
chomp_list() click to toggle source
    # File lib/puppet/pops/types/type_formatter.rb
795 def chomp_list
796   @bld.chomp!(COMMA_SEP)
797 end
is_short_array?(t) click to toggle source
    # File lib/puppet/pops/types/type_formatter.rb
668 def is_short_array?(t)
669   t.empty? || 100 - @indent * @indent_width > t.inject(0) do |sum, elem|
670     case elem
671     when true, false, nil, Numeric, Symbol
672       sum + elem.inspect.length()
673     when String
674       sum + 2 + elem.length
675     when Hash, Array
676       sum + (elem.empty? ? 2 : 1000)
677     else
678       sum + 1000
679     end
680   end
681 end
newline() click to toggle source
    # File lib/puppet/pops/types/type_formatter.rb
789 def newline
790   @bld.rstrip!
791   @bld << "\n"
792   (@indent * @indent_width).times { @bld << ' ' }
793 end
range_array_part(t) click to toggle source
    # File lib/puppet/pops/types/type_formatter.rb
683 def range_array_part(t)
684   if t.nil? || t.unbounded?
685     EMPTY_ARRAY
686   else
687     result = [t.from.nil? ? 'default' : t.from.to_s]
688     result << t.to.to_s unless t.to.nil?
689     result
690   end
691 end