Class: Wayfarer::CLI::RoutePrinter

Inherits:
Thor::Shell::Color
  • Object
show all
Defined in:
lib/wayfarer/cli/route_printer.rb

Constant Summary collapse

INDENT =
"   "
REGULAR_SEGMENT =
""
JUNCTION_SEGMENT =
"├──"
CORNER_SEGMENT =
"└──"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#outputObject (readonly)

Returns the value of attribute output.



6
7
8
# File 'lib/wayfarer/cli/route_printer.rb', line 6

def output
  @output
end

#path_finderObject (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

#urlObject (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



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 highlight_options(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)),
   highlight_options(route, options(route)),
   highlight_options(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 options(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

#visit(route) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/wayfarer/cli/route_printer.rb', line 25

def visit(route)
  route.accept(path_finder) unless route.parent
  return true if route.is_a?(Wayfarer::Routing::RootRoute)

  puts [segments(route), label(route)].join("")[3..]
  true
end