Skip to content

Commit c40faf4

Browse files
committed
Merge pull request #225 from kamilbielawski/comments_wrapping
Add pre/post wrapper for annotation block
2 parents cb331bb + a84af5e commit c40faf4

File tree

5 files changed

+29
-2
lines changed

5 files changed

+29
-2
lines changed

README.rdoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,10 @@ you can do so with a simple environment variable, instead of editing the
167167
Place the annotations at the top (before) or the bottom (after) of the routes.rb file
168168
--ps, --position-in-serializer [before|top|after|bottom]
169169
Place the annotations at the top (before) or the bottom (after) of the serializer files
170+
--w, --wrapper STR Wrap annotation with the text passed as parameter.
171+
If --w option is used, the same text will be used as opening and closing
172+
--wo, --wrapper-open STR Annotation wrapper opening.
173+
--wc, --wrapper-close STR Annotation wrapper closing
170174
-r, --routes Annotate routes.rb with the output of 'rake routes'
171175
-v, --version Show the current version of this gem
172176
-m, --show-migration Include the migration version number in the annotation

bin/annotate

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,19 @@ OptionParser.new do |opts|
7878
has_set_position['position_in_serializer'] = true
7979
end
8080

81+
opts.on('--w', '--wrapper STR', 'Wrap annotation with the text passed as parameter.',
82+
'If --w option is used, the same text will be used as opening and closing') do |p|
83+
ENV['wrapper'] = p
84+
end
85+
86+
opts.on('--wo', '--wrapper-open STR', 'Annotation wrapper opening.') do |p|
87+
ENV['wrapper_open'] = p
88+
end
89+
90+
opts.on('--wc', '--wrapper-close STR', 'Annotation wrapper closing') do |p|
91+
ENV['wrapper_close'] = p
92+
end
93+
8194
opts.on('-r', '--routes',
8295
"Annotate routes.rb with the output of 'rake routes'") do
8396
target = {

lib/annotate/annotate_models.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,15 +253,18 @@ def annotate_one_file(file_name, info_block, position, options={})
253253
new_content = old_content.sub(PATTERN, "\n" + info_block)
254254
end
255255

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

262265
new_content = %w(after bottom).include?(options[position].to_s) ?
263-
(encoding_header + (old_content.rstrip + "\n\n" + info_block)) :
264-
(encoding_header + info_block + "\n" + old_content)
266+
(encoding_header + (old_content.rstrip + "\n\n" + wrapped_info_block)) :
267+
(encoding_header + wrapped_info_block + "\n" + old_content)
265268
end
266269

267270
File.open(file_name, "wb") { |f| f.puts new_content }

lib/tasks/annotate_models.rake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ task :annotate_models => :environment do
3131
options[:sort] = Annotate.true?(ENV['sort'])
3232
options[:force] = Annotate.true?(ENV['force'])
3333
options[:trace] = Annotate.true?(ENV['trace'])
34+
options[:wrapper_open] = Annotate.fallback(ENV['wrapper_open'], ENV['wrapper'])
35+
options[:wrapper_close] = Annotate.fallback(ENV['wrapper_close'], ENV['wrapper'])
3436
AnnotateModels.do_annotations(options)
3537
end
3638

spec/annotate/annotate_models_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,11 @@ def encoding_comments_list_each
455455
expect(File.read(@model_file_name)).to eq("#{@file_content}\n#{@schema_info}")
456456
end
457457

458+
it 'should wrap annotation if wrapper is specified' do
459+
annotate_one_file :wrapper_open => 'START', :wrapper_close => 'END'
460+
expect(File.read(@model_file_name)).to eq("# START\n#{@schema_info}\n# END\n#{@file_content}")
461+
end
462+
458463
describe "with existing annotation => :before" do
459464
before do
460465
annotate_one_file :position => :before

0 commit comments

Comments
 (0)