Class: AnnotateRb::ModelAnnotator::SingleFileAnnotator
- Inherits:
-
Object
- Object
- AnnotateRb::ModelAnnotator::SingleFileAnnotator
- Defined in:
- lib/annotate_rb/model_annotator/single_file_annotator.rb
Class Method Summary collapse
-
.call(file_name, annotation, annotation_position, options) ⇒ Object
Add a schema block to a file.
- .call_with_instructions(instruction) ⇒ Object
Class Method Details
.call(file_name, annotation, annotation_position, options) ⇒ Object
Add a schema block to a file. If the file already contains a schema info block (a comment starting with “== Schema Information”), check if it matches the block that is already there. If so, leave it be. If not, remove the old info block and write a new one.
Returns:
true or false depending on whether the file was modified.
Options (opts)
:force<Symbol>:: whether to update the file even if it doesn't seem to need it.
:position_in_*<Symbol>:: where to place the annotated section in fixture or model file,
:before, :top, :after or :bottom. Default is :before.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/annotate_rb/model_annotator/single_file_annotator.rb', line 24 def call(file_name, annotation, annotation_position, ) return false unless File.exist?(file_name) old_content = File.read(file_name) parser_klass = FileToParserMapper.map(file_name) begin parsed_file = FileParser::ParsedFile.new(old_content, annotation, parser_klass, ).parse rescue FileParser::AnnotationFinder::MalformedAnnotation => e warn "Unable to process #{file_name}: #{e.}" warn "\t" + e.backtrace.join("\n\t") if @options[:trace] return false end return false if parsed_file.has_skip_string? return false if !parsed_file.annotations_changed? && ![:force] abort "AnnotateRb error. #{file_name} needs to be updated, but annotaterb was run with `--frozen`." if [:frozen] updated_file_content = if !parsed_file.has_annotations? AnnotatedFile::Generator.new(old_content, annotation, annotation_position, parser_klass, parsed_file, ).generate elsif [:force] AnnotatedFile::Generator.new(old_content, annotation, annotation_position, parser_klass, parsed_file, ).generate else AnnotatedFile::Updater.new(old_content, annotation, annotation_position, parsed_file, ).update end File.open(file_name, "wb") { |f| f.puts updated_file_content } true end |
.call_with_instructions(instruction) ⇒ Object
7 8 9 |
# File 'lib/annotate_rb/model_annotator/single_file_annotator.rb', line 7 def call_with_instructions(instruction) call(instruction.file, instruction.annotation, instruction.position, instruction.) end |