Class: Puppet::Pops::Types::StringConverter::Format Private
- 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
-
#alt ⇒ Object
(also: #alt?)
readonly
private
Boolean, alternate form (varies in meaning).
-
#container_string_formats ⇒ Object
private
Map of type to format for elements contained in an object this format applies to.
-
#delimiters ⇒ Object
readonly
private
Delimiters for containers, a “left” char representing the pair <[{(.
-
#format ⇒ Object
readonly
private
One char symbol denoting the format.
-
#left ⇒ Object
readonly
private
Boolean, left adjust in given width or not.
- #orig_fmt ⇒ Object readonly private
-
#plus ⇒ Object
readonly
private
Symbol, :space, :plus, :ignore.
-
#prec ⇒ Object
readonly
private
Nil or Integer precisions.
-
#separator ⇒ Object
private
Separator string inserted between elements in a container.
-
#separator2 ⇒ Object
private
Separator string inserted between sub elements in a container.
-
#width ⇒ Object
readonly
private
Nil or Integer with width of field > 0.
-
#zero_pad ⇒ Object
readonly
private
Boolean left_pad with zero instead of space.
Class Method Summary collapse
-
.merge(lower, higher) ⇒ Object
private
Merges two formats where the ‘higher` format overrides the `lower`.
-
.merge_string_formats(lower, higher) ⇒ Object
private
Merges a type => format association and returns a new merged and sorted association.
-
.sort_formats(format_map) ⇒ Object
private
Sorts format based on generality of types - most specific types before general.
-
.type_rank(t) ⇒ Object
private
Ranks type on specificity where it matters lower number means more specific.
Instance Method Summary collapse
-
#delimiter_pair(default = StringConverter::DEFAULT_ARRAY_DELIMITERS) ⇒ Object
private
Returns an array with a delimiter pair derived from the format.
-
#initialize(fmt) ⇒ Format
constructor
private
A new instance of Format.
-
#merge(other) ⇒ Object
private
Merges one format into this and returns a new ‘Format`.
- #to_s ⇒ Object private
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.
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 133 134 135 136 137 138 139 140 141 |
# File 'lib/puppet/pops/types/string_converter.rb', line 102 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 = if flags.include?(' ') :space else flags.include?('+') ? :plus : :ignore end @zero_pad = flags.include?('0') @delimiters = nil DELIMITERS.each do |d| next unless flags.include?(d) unless @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
#alt ⇒ Object (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)
61 62 63 |
# File 'lib/puppet/pops/types/string_converter.rb', line 61 def alt @alt end |
#container_string_formats ⇒ 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.
Map of type to format for elements contained in an object this format applies to
80 81 82 |
# File 'lib/puppet/pops/types/string_converter.rb', line 80 def container_string_formats @container_string_formats end |
#delimiters ⇒ Object (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 <[{(
77 78 79 |
# File 'lib/puppet/pops/types/string_converter.rb', line 77 def delimiters @delimiters end |
#format ⇒ Object (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
69 70 71 |
# File 'lib/puppet/pops/types/string_converter.rb', line 69 def format @format end |
#left ⇒ Object (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
73 74 75 |
# File 'lib/puppet/pops/types/string_converter.rb', line 73 def left @left end |
#orig_fmt ⇒ Object (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.
88 89 90 |
# File 'lib/puppet/pops/types/string_converter.rb', line 88 def orig_fmt @orig_fmt end |
#plus ⇒ Object (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
71 72 73 |
# File 'lib/puppet/pops/types/string_converter.rb', line 71 def plus @plus end |
#prec ⇒ Object (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
67 68 69 |
# File 'lib/puppet/pops/types/string_converter.rb', line 67 def prec @prec end |
#separator ⇒ 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.
Separator string inserted between elements in a container
83 84 85 |
# File 'lib/puppet/pops/types/string_converter.rb', line 83 def separator @separator end |
#separator2 ⇒ 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.
Separator string inserted between sub elements in a container
86 87 88 |
# File 'lib/puppet/pops/types/string_converter.rb', line 86 def separator2 @separator2 end |
#width ⇒ Object (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
65 66 67 |
# File 'lib/puppet/pops/types/string_converter.rb', line 65 def width @width end |
#zero_pad ⇒ Object (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
75 76 77 |
# File 'lib/puppet/pops/types/string_converter.rb', line 75 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`
160 161 162 163 164 165 166 |
# File 'lib/puppet/pops/types/string_converter.rb', line 160 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.
173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/puppet/pops/types/string_converter.rb', line 173 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
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/puppet/pops/types/string_converter.rb', line 190 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 format_map.to_h 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
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/puppet/pops/types/string_converter.rb', line 216 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
243 244 245 |
# File 'lib/puppet/pops/types/string_converter.rb', line 243 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.
147 148 149 150 151 152 153 |
# File 'lib/puppet/pops/types/string_converter.rb', line 147 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_s ⇒ 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.
247 248 249 |
# File 'lib/puppet/pops/types/string_converter.rb', line 247 def to_s "%#{@flags}#{@width}.#{@prec}#{@format}" end |