Class: Puppet::Pops::Types::StringConverter::Format Private

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet/pops/types/string_converter.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Format represents one format specification that is textually represented by %<flags><width>.<precision><format> Format parses and makes the individual parts available when an instance is created.

Constant Summary collapse

FMT_PATTERN_STR =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

'^%([\s\[+#0{<(|-]*)([1-9][0-9]*)?(?:\.([0-9]+))?([a-zA-Z])$'
FMT_PATTERN =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Regexp.compile(FMT_PATTERN_STR)
DELIMITERS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

[ '[', '{', '(', '<', '|',]
DELIMITER_MAP =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

{
  '[' => ['[', ']'],
  '{' => ['{', '}'],
  '(' => ['(', ')'],
  '<' => ['<', '>'],
  '|' => ['|', '|'],
  :space => ['', '']
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fmt) ⇒ Format

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Format.



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/puppet/pops/types/string_converter.rb', line 100

def initialize(fmt)
  @orig_fmt = fmt
  match = FMT_PATTERN.match(fmt)
  unless match
    raise ArgumentError, "The format '#{fmt}' is not a valid format on the form '%<flags><width>.<prec><format>'"
  end

  @format = match[4]
  unless @format.is_a?(String) && @format.length == 1
    raise ArgumentError, "The format must be a one letter format specifier, got '#{@format}'"
  end
  @format = @format.to_sym
  flags  = match[1].split('') || []
  unless flags.uniq.size == flags.size
    raise ArgumentError, "The same flag can only be used once, got '#{fmt}'"
  end
  @left  = flags.include?('-')
  @alt   = flags.include?('#')
  @plus  = (flags.include?(' ') ? :space : (flags.include?('+') ? :plus : :ignore))
  @zero_pad = flags.include?('0')

  @delimiters = nil
  DELIMITERS.each do |d|
    next unless flags.include?(d)
      if !@delimiters.nil?
        raise ArgumentError, "Only one of the delimiters [ { ( < | can be given in the format flags, got '#{fmt}'"
      end
      @delimiters = d
  end

  @width = match[2] ? match[2].to_i : nil
  @prec  = match[3] ? match[3].to_i : nil
end

Instance Attribute Details

#altObject (readonly) Also known as: alt?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Boolean, alternate form (varies in meaning)



59
60
61
# File 'lib/puppet/pops/types/string_converter.rb', line 59

def alt
  @alt
end

#container_string_formatsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Map of type to format for elements contained in an object this format applies to



78
79
80
# File 'lib/puppet/pops/types/string_converter.rb', line 78

def container_string_formats
  @container_string_formats
end

#delimitersObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Delimiters for containers, a “left” char representing the pair <[{(



75
76
77
# File 'lib/puppet/pops/types/string_converter.rb', line 75

def delimiters
  @delimiters
end

#formatObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

One char symbol denoting the format



67
68
69
# File 'lib/puppet/pops/types/string_converter.rb', line 67

def format
  @format
end

#leftObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Boolean, left adjust in given width or not



71
72
73
# File 'lib/puppet/pops/types/string_converter.rb', line 71

def left
  @left
end

#orig_fmtObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



86
87
88
# File 'lib/puppet/pops/types/string_converter.rb', line 86

def orig_fmt
  @orig_fmt
end

#plusObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Symbol, :space, :plus, :ignore



69
70
71
# File 'lib/puppet/pops/types/string_converter.rb', line 69

def plus
  @plus
end

#precObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Nil or Integer precisions



65
66
67
# File 'lib/puppet/pops/types/string_converter.rb', line 65

def prec
  @prec
end

#separatorObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Separator string inserted between elements in a container



81
82
83
# File 'lib/puppet/pops/types/string_converter.rb', line 81

def separator
  @separator
end

#separator2Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Separator string inserted between sub elements in a container



84
85
86
# File 'lib/puppet/pops/types/string_converter.rb', line 84

def separator2
  @separator2
end

#widthObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Nil or Integer with width of field > 0



63
64
65
# File 'lib/puppet/pops/types/string_converter.rb', line 63

def width
  @width
end

#zero_padObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Boolean left_pad with zero instead of space



73
74
75
# File 'lib/puppet/pops/types/string_converter.rb', line 73

def zero_pad
  @zero_pad
end

Class Method Details

.merge(lower, higher) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Merges two formats where the ‘higher` format overrides the `lower`. Produces a new `Format`

Parameters:



151
152
153
154
155
156
# File 'lib/puppet/pops/types/string_converter.rb', line 151

def self.merge(lower, higher)
  unless lower && higher
    return lower || higher
  end
  lower.merge(higher)
end

.merge_string_formats(lower, higher) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Merges a type => format association and returns a new merged and sorted association.

Parameters:



163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/puppet/pops/types/string_converter.rb', line 163

def self.merge_string_formats(lower, higher)
  unless lower && higher
    return lower || higher
  end

  # drop all formats in lower than is more generic in higher. Lower must never
  # override higher
  lower = lower.reject { |lk, _| higher.keys.any? { |hk| hk != lk && hk.assignable?(lk) }}

  merged = (lower.keys + higher.keys).uniq.map do |k|
    [k, merge(lower[k], higher[k])]
  end
  sort_formats(merged)
end

.sort_formats(format_map) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Sorts format based on generality of types - most specific types before general



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/puppet/pops/types/string_converter.rb', line 180

def self.sort_formats(format_map)
  format_map = format_map.sort do |(a,_),(b,_)|
    ab = b.assignable?(a)
    ba = a.assignable?(b)
    if a == b
      0
    elsif ab && !ba
      -1
    elsif !ab && ba
      1
    else
      # arbitrary order if disjunct (based on name of type)
      rank_a = type_rank(a)
      rank_b = type_rank(b)
      if rank_a == 0 || rank_b == 0
        a.to_s <=> b.to_s
      else
        rank_a <=> rank_b
      end
    end
  end
  Hash[format_map]
end

.type_rank(t) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Ranks type on specificity where it matters lower number means more specific



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/puppet/pops/types/string_converter.rb', line 206

def self.type_rank(t)
  case t
  when PStructType
    1
  when PHashType
    2
  when PTupleType
    3
  when PArrayType
    4
  when PPatternType
    10
  when PEnumType
    11
  when PStringType
    12
  else
    0
  end
end

Instance Method Details

#delimiter_pair(default = StringConverter::DEFAULT_ARRAY_DELIMITERS) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns an array with a delimiter pair derived from the format. If format does not contain a delimiter specification the given default is returned

Parameters:

  • the (Array<String>)

    default delimiters



232
233
234
# File 'lib/puppet/pops/types/string_converter.rb', line 232

def delimiter_pair(default = StringConverter::DEFAULT_ARRAY_DELIMITERS)
  DELIMITER_MAP[ @delimiters || @plus ] || default
end

#merge(other) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Merges one format into this and returns a new ‘Format`. The `other` format overrides this.

Parameters:



138
139
140
141
142
143
144
# File 'lib/puppet/pops/types/string_converter.rb', line 138

def merge(other)
  result = Format.new(other.orig_fmt)
  result.separator = other.separator || separator
  result.separator2 = other.separator2 || separator2
  result.container_string_formats = Format.merge_string_formats(container_string_formats, other.container_string_formats)
  result
end

#to_sObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



236
237
238
# File 'lib/puppet/pops/types/string_converter.rb', line 236

def to_s
  "%#{@flags}#{@width}.#{@prec}#{@format}"
end