class AnnotateYaml::FileHandler
Attributes
file_name[R]
result[R]
Public Class Methods
new(file_name)
click to toggle source
# File lib/annotate_yaml/annotate_yaml.rb, line 35 def initialize(file_name) @file_name = file_name @result = "" end
Public Instance Methods
call()
click to toggle source
# File lib/annotate_yaml/annotate_yaml.rb, line 40 def call create_annotated_text rewrite_file end
Private Instance Methods
adapted_file_content()
click to toggle source
# File lib/annotate_yaml/annotate_yaml.rb, line 87 def adapted_file_content File.open(file_name).read.gsub(/\r\n?/, "\n") end
annotate_line(line)
click to toggle source
# File lib/annotate_yaml/annotate_yaml.rb, line 53 def annotate_line(line) begin if line_hash = YAML::load(line) _, line_value = line_hash.first if line_value.nil? result << line else key, value = current_hash.first if line_value == key result << annotation_for_line(line, value) else result << line end end else result << line end # YAML::load will generate an exception when references are used inside the yaml file. Exemple: errors: &errors rescue Psych::BadAlias result << line end end
annotation_for_line(line, value)
click to toggle source
# File lib/annotate_yaml/annotate_yaml.rb, line 77 def annotation_for_line(line, value) (line.end_with?(" \# #{value}\n") ? line : line.gsub("\n", '') + " \# #{value}\n") end
array_of_hashes_of_values_with_keys()
click to toggle source
# File lib/annotate_yaml/annotate_yaml.rb, line 97 def array_of_hashes_of_values_with_keys @array_of_hashes_of_values_with_keys ||= YamlHashExplorer.new(hash_content).result end
create_annotated_text()
click to toggle source
# File lib/annotate_yaml/annotate_yaml.rb, line 47 def create_annotated_text adapted_file_content.each_line do |line| annotate_line(line) end end
current_hash()
click to toggle source
# File lib/annotate_yaml/annotate_yaml.rb, line 101 def current_hash array_of_hashes_of_values_with_keys.shift end
hash_content()
click to toggle source
# File lib/annotate_yaml/annotate_yaml.rb, line 91 def hash_content # Remove the references inside the yaml file otherwise the hash will be duplicated at each reference, and we don't want that. # Exemple of reference: <<: *errors YAML.load(File.open(file_name).read.gsub(/<<: \*.*/, '')) end
rewrite_file()
click to toggle source
# File lib/annotate_yaml/annotate_yaml.rb, line 81 def rewrite_file File.open(file_name, "w+") do |f| f.write(result) end end