Class: AnnotateRoutes::HeaderGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/annotate/annotate_routes/header_generator.rb

Constant Summary collapse

PREFIX =
'== Route Map'.freeze
PREFIX_MD =
'## Route Map'.freeze
HEADER_ROW =
['Prefix', 'Verb', 'URI Pattern', 'Controller#Action'].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, routes_map) ⇒ HeaderGenerator

Returns a new instance of HeaderGenerator.



39
40
41
42
# File 'lib/annotate/annotate_routes/header_generator.rb', line 39

def initialize(options, routes_map)
  @options = options
  @routes_map = routes_map
end

Class Method Details

.generate(options = {}) ⇒ Object



10
11
12
# File 'lib/annotate/annotate_routes/header_generator.rb', line 10

def generate(options = {})
  new(options, routes_map(options)).generate
end

Instance Method Details

#generateObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/annotate/annotate_routes/header_generator.rb', line 44

def generate
  magic_comments_map, contents_without_magic_comments = Helpers.extract_magic_comments_from_array(routes_map)

  out = []

  magic_comments_map.each do |magic_comment|
    out << magic_comment
  end
  out << '' if magic_comments_map.any?

  out << comment(options[:wrapper_open]) if options[:wrapper_open]

  out << comment(markdown? ? PREFIX_MD : PREFIX) + timestamp_if_required
  out << comment
  return out if contents_without_magic_comments.size.zero?

  maxs = [HEADER_ROW.map(&:size)] + contents_without_magic_comments[1..-1].map { |line| line.split.map(&:size) }

  if markdown?
    max = maxs.map(&:max).compact.max

    out << comment(content(HEADER_ROW, maxs))
    out << comment(content(['-' * max, '-' * max, '-' * max, '-' * max], maxs))
  else
    out << comment(content(contents_without_magic_comments[0], maxs))
  end

  out += contents_without_magic_comments[1..-1].map { |line| comment(content(markdown? ? line.split(' ') : line, maxs)) }
  out << comment(options[:wrapper_close]) if options[:wrapper_close]

  out
end