Class: Dry::Types::Printer Private
- Inherits:
-
Object
- Object
- Dry::Types::Printer
- Defined in:
- lib/dry/types/printer.rb,
lib/dry/types/extensions/maybe.rb,
lib/dry/types/printer/composition.rb
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.
Defined Under Namespace
Classes: Composition
Constant Summary collapse
- MAPPING =
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.
{ Nominal => :visit_nominal, Constructor => :visit_constructor, [ Constrained, Constrained::Coercible ] => :visit_constrained, Types::Hash => :visit_hash, Schema => :visit_schema, Schema::Key => :visit_key, Map => :visit_map, Array => :visit_array, Array::Member => :visit_array_member, Lax => :visit_lax, Enum => :visit_enum, [Default, Default::Callable] => :visit_default, [ Sum, Sum::Constrained, Intersection, Intersection::Constrained, Implication, Implication::Constrained ] => :visit_composition, Any.class => :visit_any }.flat_map { |k, v| Array(k).map { |kk| [kk, v] } }.to_h
Instance Method Summary collapse
- #call(type) ⇒ Object private
-
#initialize ⇒ Printer
constructor
private
A new instance of Printer.
- #visit(type) ⇒ Object private
- #visit_any(_) {|"Any"| ... } ⇒ Object private
- #visit_array(type) ⇒ Object private
- #visit_array_member(array) ⇒ Object private
- #visit_callable(callable) ⇒ Object private
- #visit_composition(composition) ⇒ Object private
- #visit_constrained(constrained) ⇒ Object private
- #visit_constructor(constructor) ⇒ Object private
- #visit_default(default) ⇒ Object private
- #visit_enum(enum) ⇒ Object private
- #visit_hash(hash) ⇒ Object private
- #visit_key(key) ⇒ Object private
- #visit_lax(lax) ⇒ Object private
- #visit_map(map) ⇒ Object private
- #visit_maybe(maybe) ⇒ Object private
- #visit_nominal(type) ⇒ Object private
-
#visit_options(options, meta = EMPTY_HASH) ⇒ Object
private
rubocop:disable Metrics/PerceivedComplexity.
- #visit_schema(schema) ⇒ Object private
Constructor Details
#initialize ⇒ Printer
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 Printer.
38 39 40 41 |
# File 'lib/dry/types/printer.rb', line 38 def initialize @composition_printers = {} freeze end |
Instance Method Details
#call(type) ⇒ 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.
43 44 45 46 47 |
# File 'lib/dry/types/printer.rb', line 43 def call(type) output = "".dup visit(type) { |str| output << str } "#<Dry::Types[#{output}]>" end |
#visit(type) ⇒ 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.
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/dry/types/printer.rb', line 49 def visit(type, &) print_with = MAPPING.fetch(type.class) do if type.class < Constructor :visit_constructor elsif type.is_a?(Type) return yield type.inspect else raise ArgumentError, "Do not know how to print #{type.class}" end end send(print_with, type, &) end |
#visit_any(_) {|"Any"| ... } ⇒ 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.
62 |
# File 'lib/dry/types/printer.rb', line 62 def visit_any(_) = yield "Any" |
#visit_array(type) ⇒ 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.
64 65 66 67 68 |
# File 'lib/dry/types/printer.rb', line 64 def visit_array(type) (EMPTY_HASH, type.) do |opts| yield "Array#{opts}" end end |
#visit_array_member(array) ⇒ 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.
70 71 72 73 74 75 76 |
# File 'lib/dry/types/printer.rb', line 70 def visit_array_member(array) visit(array.member) do |type| (EMPTY_HASH, array.) do |opts| yield "Array<#{type}#{opts}>" end end end |
#visit_callable(callable) ⇒ 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.
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/dry/types/printer.rb', line 152 def visit_callable(callable) fn = callable.is_a?(String) ? FnContainer[callable] : callable case fn when ::Method yield "#{fn.receiver}.#{fn.name}" when ::Proc path, line = fn.source_location if line&.zero? yield ".#{path}" elsif path yield "#{path.sub("#{Dir.pwd}/", EMPTY_STRING)}:#{line}" else match = fn.to_s.match(/\A#<Proc:0x\h+\(&:(?<name>\w+)\)(:? \(lambda\))?>\z/) # rubocop:disable Lint/MixedRegexpCaptureTypes if match yield ".#{match[:name]}" elsif fn.lambda? yield "(lambda)" else yield "(proc)" end end else call = fn.method(:call) if call.owner == fn.class yield "#{fn.class}#call" else yield "#{fn}.call" end end end |
#visit_composition(composition) ⇒ 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.
102 103 104 105 106 |
# File 'lib/dry/types/printer.rb', line 102 def visit_composition(composition, &) klass = composition.class @composition_printers[klass] = Composition.new(self, klass) @composition_printers[klass].visit(composition, &) end |
#visit_constrained(constrained) ⇒ 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.
91 92 93 94 95 96 97 98 99 100 |
# File 'lib/dry/types/printer.rb', line 91 def visit_constrained(constrained) visit(constrained.type) do |type| = constrained..dup rule = .delete(:rule) () do |_opts| yield "Constrained<#{type} rule=[#{rule}]>" end end end |
#visit_constructor(constructor) ⇒ 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.
78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/dry/types/printer.rb', line 78 def visit_constructor(constructor) visit(constructor.type) do |type| visit_callable(constructor.fn.fn) do |fn| = constructor..dup .delete(:fn) () do |opts| yield "Constructor<#{type} fn=#{fn}#{opts}>" end end end end |
#visit_default(default) ⇒ 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.
126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/dry/types/printer.rb', line 126 def visit_default(default) visit(default.type) do |type| (default.) do |opts| if default.is_a?(Default::Callable) visit_callable(default.value) do |fn| yield "Default<#{type} value_fn=#{fn}#{opts}>" end else yield "Default<#{type} value=#{default.value.inspect}#{opts}>" end end end end |
#visit_enum(enum) ⇒ 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.
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/dry/types/printer.rb', line 108 def visit_enum(enum) visit(enum.type) do |type| = enum..dup mapping = .delete(:mapping) () do |opts| if mapping == enum.inverted_mapping yield "Enum(#{enum.joined_values})<#{type}#{opts}>" else mapping_str = mapping.map { |key, value| "#{key.inspect}=>#{value.inspect}" }.join(", ") yield "Enum<#{type} mapping={#{mapping_str}}#{opts}>" end end end end |
#visit_hash(hash) ⇒ 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.
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/dry/types/printer.rb', line 250 def visit_hash(hash) = hash..dup type_fn_str = "" if (type_fn = .delete(:type_transform_fn)) visit_callable(type_fn) do |fn| type_fn_str = "type_fn=#{fn}" end end (, hash.) do |opts| if opts.empty? && type_fn_str.empty? yield "Hash" else yield "Hash<#{type_fn_str}#{opts}>" end end end |
#visit_key(key) ⇒ 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.
240 241 242 243 244 245 246 247 248 |
# File 'lib/dry/types/printer.rb', line 240 def visit_key(key) visit(key.type) do |type| if key.required? yield "#{key.name}: #{type}" else yield "#{key.name}?: #{type}" end end end |
#visit_lax(lax) ⇒ 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.
146 147 148 149 150 |
# File 'lib/dry/types/printer.rb', line 146 def visit_lax(lax) visit(lax.type) do |type| yield "Lax<#{type}>" end end |
#visit_map(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.
226 227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/dry/types/printer.rb', line 226 def visit_map(map) visit(map.key_type) do |key| visit(map.value_type) do |value| = map..dup .delete(:key_type) .delete(:value_type) () do |_opts| yield "Map<#{key} => #{value}>" end end end end |
#visit_maybe(maybe) ⇒ 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.
111 112 113 114 115 |
# File 'lib/dry/types/extensions/maybe.rb', line 111 def visit_maybe(maybe) visit(maybe.type) do |type| yield "Maybe<#{type}>" end end |
#visit_nominal(type) ⇒ 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.
140 141 142 143 144 |
# File 'lib/dry/types/printer.rb', line 140 def visit_nominal(type) (type., type.) do |opts| yield "Nominal<#{type.primitive}#{opts}>" end end |
#visit_options(options, meta = EMPTY_HASH) ⇒ 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.
rubocop:disable Metrics/PerceivedComplexity
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
# File 'lib/dry/types/printer.rb', line 269 def (, = EMPTY_HASH) # rubocop:disable Metrics/PerceivedComplexity if .empty? && .empty? yield "" else opts = .empty? ? "" : " options=#{.inspect}" if .empty? yield opts else values = .map do |key, value| case key when Symbol "#{key}: #{value.inspect}" else "#{key.inspect}=>#{value.inspect}" end end yield "#{opts} meta={#{values.join(", ")}}" end end end |
#visit_schema(schema) ⇒ 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.
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/dry/types/printer.rb', line 187 def visit_schema(schema) = schema..dup size = schema.count key_fn_str = "" type_fn_str = "" strict_str = "" strict_str = "strict " if .delete(:strict) if (key_fn = .delete(:key_transform_fn)) visit_callable(key_fn) do |fn| key_fn_str = "key_fn=#{fn} " end end if (type_fn = .delete(:type_transform_fn)) visit_callable(type_fn) do |fn| type_fn_str = "type_fn=#{fn} " end end keys = .delete(:keys) (, schema.) do |opts| opts = "#{opts[1..]} " unless opts.empty? schema_parameters = "#{key_fn_str}#{type_fn_str}#{strict_str}#{opts}" header = "Schema<#{schema_parameters}keys={" if size.zero? yield "#{header}}>" else yield header.dup << keys.map { |key| visit(key) { |type| type } }.join(" ") << "}>" end end end |