Skip to content

Commit

Permalink
Merge pull request #225 from kamilbielawski/comments_wrapping
Browse files Browse the repository at this point in the history
Add pre/post wrapper for annotation block
  • Loading branch information
ctran committed Jan 13, 2015
2 parents cb331bb + a84af5e commit c40faf4
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.rdoc
Expand Up @@ -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
--ps, --position-in-serializer [before|top|after|bottom]
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'
-v, --version Show the current version of this gem
-m, --show-migration Include the migration version number in the annotation
Expand Down
13 changes: 13 additions & 0 deletions bin/annotate
Expand Up @@ -78,6 +78,19 @@ OptionParser.new do |opts|
has_set_position['position_in_serializer'] = true
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',
"Annotate routes.rb with the output of 'rake routes'") do
target = {
Expand Down
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
2 changes: 2 additions & 0 deletions lib/tasks/annotate_models.rake
Expand Up @@ -31,6 +31,8 @@ task :annotate_models => :environment do
options[:sort] = Annotate.true?(ENV['sort'])
options[:force] = Annotate.true?(ENV['force'])
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)
end

Expand Down
5 changes: 5 additions & 0 deletions spec/annotate/annotate_models_spec.rb
Expand Up @@ -455,6 +455,11 @@ def encoding_comments_list_each
expect(File.read(@model_file_name)).to eq("#{@file_content}\n#{@schema_info}")
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
before do
annotate_one_file :position => :before
Expand Down

0 comments on commit c40faf4

Please sign in to comment.