Module: Sinatra::Croon::Parser

Defined in:
lib/sinatra/croon.rb

Class Method Summary collapse

Class Method Details

.parse_comments(comments) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/sinatra/croon.rb', line 90

def self.parse_comments(comments)
  key = :description

  comments.inject({}) do |parsed, comment|
    case comment.strip
      when /\A#\s*@param\s+(.+?)\s+(.+)/ then
        parsed[:params] ||= []
        required = $1[0..0] == '<'
        name = $1[1..-2]
        parsed[:params] << { :name => name, :description => $2, :required => required }
      when /\A#\s*@(\w+)\s*(.*)\Z/ then
        key = $1.to_sym
        parsed[key] ||= []
        parsed[key]  << $2 if $2
      when /\A#(.+)\Z/ then
        parsed[key] ||= []
        parsed[key]  << $1
    end
    parsed
  end.inject({}) do |flattened, (k, v)|
    case v.first
      when String then
        flattened[k] = strip_left(v.reject { |l| l.strip == "" }.join("\n"))
      else
        flattened[k] = v
    end
    flattened
  end
end

.parse_route_documentation(filename, line) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/sinatra/croon.rb', line 69

def self.parse_route_documentation(filename, line)
  all_lines   = File.read(filename).split("\n").reverse
  index_start = all_lines.length - line + 1
  lines       = []
  started     = false

  all_lines[index_start..-1].each do |line|
    case line.strip[0..0]
      when '#' then
        started = true
        lines << line.strip
      when '' then
        break(2) if started
      else
        break(2)
    end
  end

  parse_comments(lines.reverse)
end

.strip_left(code) ⇒ Object



120
121
122
123
124
125
126
127
# File 'lib/sinatra/croon.rb', line 120

def self.strip_left(code)
  first_line = code.split("\n").first
  return code unless first_line
  num_spaces = first_line.match(/\A */)[0].length
  code.split("\n").map do |line|
    line[num_spaces..-1]
  end.join("\n")
end