From a84af5e87af45860c6a4dfaf99bcf57e0f385903 Mon Sep 17 00:00:00 2001 From: Kamil Bielawski Date: Tue, 13 Jan 2015 21:00:38 +0100 Subject: [PATCH] Wrap annotations --- lib/annotate/annotate_models.rb | 7 +++++-- spec/annotate/annotate_models_spec.rb | 4 ++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/annotate/annotate_models.rb b/lib/annotate/annotate_models.rb index 90f2553aa..2a5d8c71d 100644 --- a/lib/annotate/annotate_models.rb +++ b/lib/annotate/annotate_models.rb @@ -253,6 +253,9 @@ 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] @@ -260,8 +263,8 @@ def annotate_one_file(file_name, info_block, position, options={}) 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 } diff --git a/spec/annotate/annotate_models_spec.rb b/spec/annotate/annotate_models_spec.rb index b42efc8ad..27d228877 100644 --- a/spec/annotate/annotate_models_spec.rb +++ b/spec/annotate/annotate_models_spec.rb @@ -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