Skip to content

Commit 056fd42

Browse files
committed
Merge pull request #302 from b264/2_3_ruby__support
Ruby 2.3 magic comments support
2 parents b2b1706 + 40060e3 commit 056fd42

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

lib/annotate/annotate_models.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,8 @@ def annotate_one_file(file_name, info_block, position, options={})
330330
old_columns = old_header && old_header.scan(column_pattern).sort
331331
new_columns = new_header && new_header.scan(column_pattern).sort
332332

333-
encoding = Regexp.new(/(^#\s*encoding:.*\n)|(^# coding:.*\n)|(^# -\*- coding:.*\n)|(^# -\*- encoding\s?:.*\n)/)
334-
encoding_header = old_content.match(encoding).to_s
333+
magic_comment_matcher= Regexp.new(/(^#\s*encoding:.*\n)|(^# coding:.*\n)|(^# -\*- coding:.*\n)|(^# -\*- encoding\s?:.*\n)|(^#\s*frozen_string_literal:.+\n)|(^# -\*- frozen_string_literal\s*:.+-\*-\n)/)
334+
magic_comments= old_content.scan(magic_comment_matcher).flatten.compact
335335

336336
if old_columns == new_columns && !options[:force]
337337
return false
@@ -349,12 +349,12 @@ def annotate_one_file(file_name, info_block, position, options={})
349349
# if there *was* no old schema info (no substitution happened) or :force was passed,
350350
# we simply need to insert it in correct position
351351
if new_content == old_content || options[:force]
352-
old_content.sub!(encoding, '')
352+
old_content.sub!(magic_comment_matcher, '')
353353
old_content.sub!(PATTERN, '')
354354

355355
new_content = %w(after bottom).include?(options[position].to_s) ?
356-
(encoding_header + (old_content.rstrip + "\n\n" + wrapped_info_block)) :
357-
(encoding_header + wrapped_info_block + "\n" + old_content)
356+
(magic_comments.join + (old_content.rstrip + "\n\n" + wrapped_info_block)) :
357+
(magic_comments.join + wrapped_info_block + "\n" + old_content)
358358
end
359359

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

spec/annotate/annotate_models_spec.rb

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -502,14 +502,20 @@ def annotate_one_file options = {}
502502
Annotate::PATH_OPTIONS.each { |key| ENV[key.to_s] = '' }
503503
end
504504

505-
def encoding_comments_list_each
505+
def magic_comments_list_each
506506
[
507507
'# encoding: UTF-8',
508508
'# coding: UTF-8',
509509
'# -*- coding: UTF-8 -*-',
510510
'#encoding: utf-8',
511-
'# -*- encoding : utf-8 -*-'
512-
].each{|encoding_comment| yield encoding_comment }
511+
'# encoding: utf-8',
512+
'# -*- encoding : utf-8 -*-',
513+
"# encoding: utf-8\n# frozen_string_literal: true",
514+
"# frozen_string_literal: true\n# encoding: utf-8",
515+
'# frozen_string_literal: true',
516+
'#frozen_string_literal: false',
517+
'# -*- frozen_string_literal : true -*-',
518+
].each{|magic_comment| yield magic_comment }
513519
end
514520

515521
it "should put annotation before class if :position == 'before'" do
@@ -626,17 +632,22 @@ class Foo::User < ActiveRecord::Base
626632
expect(File.read(model_file_name)).to eq("#{schema_info}\n#{file_content}")
627633
end
628634

629-
it "should not touch encoding comments" do
630-
encoding_comments_list_each do |encoding_comment|
635+
it "should not touch magic comments" do
636+
magic_comments_list_each do |magic_comment|
631637
write_model "user.rb", <<-EOS
632-
#{encoding_comment}
638+
#{magic_comment}
633639
class User < ActiveRecord::Base
634640
end
635641
EOS
636642

637643
annotate_one_file :position => :before
638644

639-
expect(File.open(@model_file_name, &:readline)).to eq("#{encoding_comment}\n")
645+
lines= magic_comment.split("\n")
646+
File.open @model_file_name do |file|
647+
lines.count.times do |index|
648+
expect(file.readline).to eq "#{lines[index]}\n"
649+
end
650+
end
640651
end
641652
end
642653

0 commit comments

Comments
 (0)