Commit c40faf4a by Cuong Tran

Merge pull request #225 from kamilbielawski/comments_wrapping

Add pre/post wrapper for annotation block
parents cb331bbe a84af5e8
...@@ -167,6 +167,10 @@ you can do so with a simple environment variable, instead of editing the ...@@ -167,6 +167,10 @@ you can do so with a simple environment variable, instead of editing the
Place the annotations at the top (before) or the bottom (after) of the routes.rb file Place the annotations at the top (before) or the bottom (after) of the routes.rb file
--ps, --position-in-serializer [before|top|after|bottom] --ps, --position-in-serializer [before|top|after|bottom]
Place the annotations at the top (before) or the bottom (after) of the serializer files Place the annotations at the top (before) or the bottom (after) of the serializer files
--w, --wrapper STR Wrap annotation with the text passed as parameter.
If --w option is used, the same text will be used as opening and closing
--wo, --wrapper-open STR Annotation wrapper opening.
--wc, --wrapper-close STR Annotation wrapper closing
-r, --routes Annotate routes.rb with the output of 'rake routes' -r, --routes Annotate routes.rb with the output of 'rake routes'
-v, --version Show the current version of this gem -v, --version Show the current version of this gem
-m, --show-migration Include the migration version number in the annotation -m, --show-migration Include the migration version number in the annotation
......
...@@ -78,6 +78,19 @@ OptionParser.new do |opts| ...@@ -78,6 +78,19 @@ OptionParser.new do |opts|
has_set_position['position_in_serializer'] = true has_set_position['position_in_serializer'] = true
end end
opts.on('--w', '--wrapper STR', 'Wrap annotation with the text passed as parameter.',
'If --w option is used, the same text will be used as opening and closing') do |p|
ENV['wrapper'] = p
end
opts.on('--wo', '--wrapper-open STR', 'Annotation wrapper opening.') do |p|
ENV['wrapper_open'] = p
end
opts.on('--wc', '--wrapper-close STR', 'Annotation wrapper closing') do |p|
ENV['wrapper_close'] = p
end
opts.on('-r', '--routes', opts.on('-r', '--routes',
"Annotate routes.rb with the output of 'rake routes'") do "Annotate routes.rb with the output of 'rake routes'") do
target = { target = {
......
...@@ -253,6 +253,9 @@ module AnnotateModels ...@@ -253,6 +253,9 @@ module AnnotateModels
new_content = old_content.sub(PATTERN, "\n" + info_block) new_content = old_content.sub(PATTERN, "\n" + info_block)
end 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, # if there *was* no old schema info (no substitution happened) or :force was passed,
# we simply need to insert it in correct position # we simply need to insert it in correct position
if new_content == old_content || options[:force] if new_content == old_content || options[:force]
...@@ -260,8 +263,8 @@ module AnnotateModels ...@@ -260,8 +263,8 @@ module AnnotateModels
old_content.sub!(PATTERN, '') old_content.sub!(PATTERN, '')
new_content = %w(after bottom).include?(options[position].to_s) ? new_content = %w(after bottom).include?(options[position].to_s) ?
(encoding_header + (old_content.rstrip + "\n\n" + info_block)) : (encoding_header + (old_content.rstrip + "\n\n" + wrapped_info_block)) :
(encoding_header + info_block + "\n" + old_content) (encoding_header + wrapped_info_block + "\n" + old_content)
end end
File.open(file_name, "wb") { |f| f.puts new_content } File.open(file_name, "wb") { |f| f.puts new_content }
......
...@@ -31,6 +31,8 @@ task :annotate_models => :environment do ...@@ -31,6 +31,8 @@ task :annotate_models => :environment do
options[:sort] = Annotate.true?(ENV['sort']) options[:sort] = Annotate.true?(ENV['sort'])
options[:force] = Annotate.true?(ENV['force']) options[:force] = Annotate.true?(ENV['force'])
options[:trace] = Annotate.true?(ENV['trace']) options[:trace] = Annotate.true?(ENV['trace'])
options[:wrapper_open] = Annotate.fallback(ENV['wrapper_open'], ENV['wrapper'])
options[:wrapper_close] = Annotate.fallback(ENV['wrapper_close'], ENV['wrapper'])
AnnotateModels.do_annotations(options) AnnotateModels.do_annotations(options)
end end
......
...@@ -455,6 +455,11 @@ end ...@@ -455,6 +455,11 @@ end
expect(File.read(@model_file_name)).to eq("#{@file_content}\n#{@schema_info}") expect(File.read(@model_file_name)).to eq("#{@file_content}\n#{@schema_info}")
end end
it 'should wrap annotation if wrapper is specified' do
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 describe "with existing annotation => :before" do
before do before do
annotate_one_file :position => :before annotate_one_file :position => :before
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment