Class: AnnotateRoutes::HeaderGenerator

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

Constant Summary collapse

PREFIX =
'== Route Map'
PREFIX_MD =
'## Route Map'
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.



41
42
43
44
# File 'lib/annotate/annotate_routes/header_generator.rb', line 41

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

Class Method Details

.generate(options = {}) ⇒ Object



12
13
14
# File 'lib/annotate/annotate_routes/header_generator.rb', line 12

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

Instance Method Details

#generateObject



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
76
77
78
79
# File 'lib/annotate/annotate_routes/header_generator.rb', line 46

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..].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..].map do |line|
    comment(content(markdown? ? line.split(' ') : line, maxs))
  end
  out << comment(options[:wrapper_close]) if options[:wrapper_close]

  out
end