Skip to content

Commit

Permalink
Wrap annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilbielawski committed Jan 13, 2015
1 parent e5bedc0 commit a84af5e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
7 changes: 5 additions & 2 deletions lib/annotate/annotate_models.rb
Expand Up @@ -253,15 +253,18 @@ def annotate_one_file(file_name, info_block, position, options={})
new_content = old_content.sub(PATTERN, "\n" + info_block)
end

wrapper_open = options[:wrapper_open] ? "# #{options[:wrapper_open]}\n" : ""
wrapper_close = options[:wrapper_close] ? "\n# #{options[:wrapper_close]}" : ""
wrapped_info_block = "#{wrapper_open}#{info_block}#{wrapper_close}"
# if there *was* no old schema info (no substitution happened) or :force was passed,
# we simply need to insert it in correct position
if new_content == old_content || options[:force]
old_content.sub!(encoding, '')
old_content.sub!(PATTERN, '')

new_content = %w(after bottom).include?(options[position].to_s) ?
(encoding_header + (old_content.rstrip + "\n\n" + info_block)) :
(encoding_header + info_block + "\n" + old_content)
(encoding_header + (old_content.rstrip + "\n\n" + wrapped_info_block)) :
(encoding_header + wrapped_info_block + "\n" + old_content)
end

File.open(file_name, "wb") { |f| f.puts new_content }
Expand Down
4 changes: 2 additions & 2 deletions spec/annotate/annotate_models_spec.rb
Expand Up @@ -456,8 +456,8 @@ def encoding_comments_list_each
end

it 'should wrap annotation if wrapper is specified' do
annotate_one_file :wrapper_beg => 'START', :wrapper_end => 'END'
expect(File.read(@model_file_name)).to eq("START\n#{@schema_info}\nEND\n#{@file_content}")
annotate_one_file :wrapper_open => 'START', :wrapper_close => 'END'
expect(File.read(@model_file_name)).to eq("# START\n#{@schema_info}\n# END\n#{@file_content}")
end

describe "with existing annotation => :before" do
Expand Down

0 comments on commit a84af5e

Please sign in to comment.