Class: Wayfarer::CLI::RoutePrinter Private
- Inherits:
-
Object
- Object
- Wayfarer::CLI::RoutePrinter
- Defined in:
- lib/wayfarer/cli/route_printer.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.
Turns a routing tree into a Hash and prints it.
Used by the route CLI subcommand.
Constant Summary collapse
- BATCH =
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.
"tmp"
Class Method Summary collapse
-
.print(route, url, format:) ⇒ Object
private
Prints a routing tree.
Instance Method Summary collapse
-
#call(route, result, path_finder) ⇒ Object
private
Callback method called by
path_finderwith the result of matching the route. -
#initialize(route, url, serializer) ⇒ RoutePrinter
constructor
private
A new instance of RoutePrinter.
-
#print ⇒ Object
private
Processes the routing trees and prints the serialized output.
- #serializers ⇒ Hash<Symbol, Proc> private
Constructor Details
#initialize(route, url, serializer) ⇒ RoutePrinter
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 RoutePrinter.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/wayfarer/cli/route_printer.rb', line 32 def initialize(route, url, serializer) @route = route @serializer = serializer @nodes = {} @root_hash = nil task = Wayfarer::Task.new(url, BATCH) task[:uri] = Addressable::URI.parse(url) @path_finder = Wayfarer::Routing::PathFinder.new( task, stop_when_found: false, &method(:call) ) end |
Class Method Details
.print(route, url, format:) ⇒ 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.
Prints a routing tree.
25 26 27 |
# File 'lib/wayfarer/cli/route_printer.rb', line 25 def self.print(route, url, format:) new(route, url, serializers.fetch(format.to_sym)).print end |
Instance Method Details
#call(route, result, path_finder) ⇒ 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.
Callback method called by path_finder with the result of matching
the route.
63 64 65 66 67 68 69 70 |
# File 'lib/wayfarer/cli/route_printer.rb', line 63 def call(route, result, path_finder) node = (nodes[route] ||= attributes(route, result, path_finder)) parent = route.parent return @root_hash ||= node unless parent nodes.dig(parent, route_type(parent), :children).append(node) end |
#print ⇒ 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.
Processes the routing trees and prints the serialized output.
49 50 51 52 53 54 55 |
# File 'lib/wayfarer/cli/route_printer.rb', line 49 def print route.accept(path_finder) hash = routing_result(path_finder).merge(root_hash) puts serializer.call(hash) end |
#serializers ⇒ Hash<Symbol, Proc>
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.
11 12 13 14 15 16 |
# File 'lib/wayfarer/cli/route_printer.rb', line 11 class_attribute :serializers, default: { yaml: ->(hash) { YAML.dump(hash.deep_stringify_keys) }, json: ->(hash) { JSON.pretty_generate(hash) }, ruby: ->(hash) { pp(hash) } }, instance_accessor: false, instance_predicate: false |