Module: ActiveRecord::Annotate::Writer

Defined in:
lib/active_record/annotate/writer.rb

Class Method Summary collapse

Class Method Details

.assemble(annotation, path) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/active_record/annotate/writer.rb', line 17

def assemble(annotation, path)
  lines = File.read(path).split("\n")
  
  if lines.first =~ /^\s*#.*coding/
    # encoding: utf-8 encountered on the first line
    encoding_line = lines.shift
  end
  
  while lines.first.starts_with?('#') || lines.first.blank?
    # throw out comments and empty lines in the beginning of the file (old annotation)
    lines.shift
  end
  
  lines.unshift(*annotation, nil)
  lines.unshift(encoding_line) unless encoding_line.nil?
  
  lines.join("\n")
end

.write(annotation, path) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/active_record/annotate/writer.rb', line 5

def write(annotation, path)
  new_file_content = assemble(annotation, path)
  
  temp_path = "#{path}.annotated"
  File.open(temp_path, 'w') do |temp_file|
    temp_file.puts(new_file_content)
  end
  
  File.delete(path)
  File.rename(temp_path, path)
end