Class: Wayfarer::CLI::RoutePrinter
- Inherits:
-
Thor::Shell::Color
- Object
- Thor::Shell::Color
- Wayfarer::CLI::RoutePrinter
- Defined in:
- lib/wayfarer/cli/route_printer.rb
Constant Summary collapse
- INDENT =
" "
- REGULAR_SEGMENT =
"│ "
- JUNCTION_SEGMENT =
"├──"
- CORNER_SEGMENT =
"└──"
Instance Attribute Summary collapse
-
#output ⇒ Object
readonly
Returns the value of attribute output.
-
#path_finder ⇒ Object
readonly
Returns the value of attribute path_finder.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Class Method Summary collapse
Instance Method Summary collapse
- #highlight_matcher(route, string) ⇒ Object
- #highlight_options(route, string) ⇒ Object
-
#initialize(url) ⇒ RoutePrinter
constructor
A new instance of RoutePrinter.
- #label(route) ⇒ Object
- #matcher_label(route) ⇒ Object
- #options(route) ⇒ Object
- #params(route) ⇒ Object
- #parent_segment(parent) ⇒ Object
- #segment(route) ⇒ Object
- #segments(route) ⇒ Object
- #visit(route) ⇒ Object
Constructor Details
#initialize(url) ⇒ RoutePrinter
Returns a new instance of RoutePrinter.
19 20 21 22 23 |
# File 'lib/wayfarer/cli/route_printer.rb', line 19 def initialize(url) @url = url @path_finder = Wayfarer::Routing::PathFinder.new(url) super() end |
Instance Attribute Details
#output ⇒ Object (readonly)
Returns the value of attribute output.
6 7 8 |
# File 'lib/wayfarer/cli/route_printer.rb', line 6 def output @output end |
#path_finder ⇒ Object (readonly)
Returns the value of attribute path_finder.
6 7 8 |
# File 'lib/wayfarer/cli/route_printer.rb', line 6 def path_finder @path_finder end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
6 7 8 |
# File 'lib/wayfarer/cli/route_printer.rb', line 6 def url @url end |
Class Method Details
.print(route, url) ⇒ Object
15 16 17 |
# File 'lib/wayfarer/cli/route_printer.rb', line 15 def self.print(route, url) route.accept(new(url)) end |
Instance Method Details
#highlight_matcher(route, string) ⇒ Object
61 62 63 64 65 66 67 68 69 |
# File 'lib/wayfarer/cli/route_printer.rb', line 61 def highlight_matcher(route, string) if path_finder.path.include?(route) set_color(string, :green, :bold) elsif route.matcher.match(url) set_color(string, :green) else set_color(string, :red) end end |
#highlight_options(route, string) ⇒ Object
71 72 73 74 75 |
# File 'lib/wayfarer/cli/route_printer.rb', line 71 def (route, string) return string unless path_finder.path.include?(route) set_color(string, :green, :bold) end |
#label(route) ⇒ Object
55 56 57 58 59 |
# File 'lib/wayfarer/cli/route_printer.rb', line 55 def label(route) [highlight_matcher(route, matcher_label(route)), (route, (route)), (route, params(route))].compact.join(" ") end |
#matcher_label(route) ⇒ Object
77 78 79 80 81 |
# File 'lib/wayfarer/cli/route_printer.rb', line 77 def matcher_label(route) return "Target" if route.is_a?(Wayfarer::Routing::TargetRoute) route.matcher.class.name.demodulize.delete_suffix("Matcher") end |
#options(route) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/wayfarer/cli/route_printer.rb', line 83 def (route) return "" if route.is_a?(Wayfarer::Routing::RootRoute) case (matcher = route.matcher) when Wayfarer::Routing::HostMatcher then matcher.host when Wayfarer::Routing::PathMatcher then matcher.path when Wayfarer::Routing::QueryMatcher then matcher.fields when Wayfarer::Routing::CustomMatcher then "##{route.action}" when Wayfarer::Routing::SchemeMatcher then matcher.scheme when Wayfarer::Routing::SuffixMatcher then matcher.suffix end end |
#params(route) ⇒ Object
96 97 98 99 |
# File 'lib/wayfarer/cli/route_printer.rb', line 96 def params(route) params = route.matcher.params(url) "=> #{params.symbolize_keys}" if params.any? end |
#parent_segment(parent) ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/wayfarer/cli/route_printer.rb', line 39 def parent_segment(parent) if trailer?(parent) INDENT else REGULAR_SEGMENT end end |
#segment(route) ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/wayfarer/cli/route_printer.rb', line 47 def segment(route) if trailer?(route) CORNER_SEGMENT else JUNCTION_SEGMENT end end |
#segments(route) ⇒ Object
33 34 35 36 37 |
# File 'lib/wayfarer/cli/route_printer.rb', line 33 def segments(route) current = segment(route) parents = parents(route).map { |parent| parent_segment(parent) } [parents, current].join end |