class I27r::YamlDocument
Attributes
root[RW]
Public Class Methods
load_yml_file(yml_path)
click to toggle source
# File lib/generators/i18n_translation/lib/yaml.rb, line 62 def self.load_yml_file(yml_path) if File.exist? yml_path self.new File.read(yml_path) else self.new end end
new(yaml = '')
click to toggle source
# File lib/generators/i18n_translation/lib/yaml.rb, line 58 def initialize(yaml = '') @lines = yaml.split("\n").map {|s| Line.new s} end
Public Instance Methods
[](*path)
click to toggle source
# File lib/generators/i18n_translation/lib/yaml.rb, line 70 def [](*path) find_line_by_path(path.flatten).try :value end
[]=(*args)
click to toggle source
# File lib/generators/i18n_translation/lib/yaml.rb, line 110 def []=(*args) value = args.pop path = args.flatten # return if value && (value == self[path]) line = find_line_by_path path.dup if line if line.generated? line.value = value end else line = find_line_by_path path, -1, true line.value = value end end
find_line_by_path(path, line_num = -1, add_new = false)
click to toggle source
# File lib/generators/i18n_translation/lib/yaml.rb, line 74 def find_line_by_path(path, line_num = -1, add_new = false) key = path.shift indent = line_num == -1 ? '' : @lines[line_num].scan(/^ */).first + ' ' @lines[(line_num + 1)..-1].each do |line| line_num += 1 next unless line.yaml? if (line.indent == indent) && (line.key == key) if path.empty? return line else return find_line_by_path path, line_num, add_new end elsif line.indent < indent if add_new new_line = Line.new("#{indent}#{key}:", :generated => true) if @lines[line_num - 1].try(:text) == '' @lines.insert line_num - 1, new_line else @lines.insert line_num, new_line end return new_line if path.empty? return find_line_by_path path, line_num, add_new else return end end end if add_new new_line = Line.new("#{indent}#{key}:", :generated => true) @lines.insert @lines.count, new_line return new_line if path.empty? return find_line_by_path path, @lines.count - 1, add_new end end
to_s(add_blank_line = false)
click to toggle source
# File lib/generators/i18n_translation/lib/yaml.rb, line 126 def to_s(add_blank_line = false) previous_indent = '' ''.tap do |ret| @lines.each do |line| ret << "\n" if add_blank_line && (line.indent < previous_indent) && !line.to_s.blank? && !ret.end_with?("\n\n") previous_indent = line.indent ret << line.to_s << "\n" end end end