Class: I27r::YamlDocument

Inherits:
Object
  • Object
show all
Defined in:
lib/generators/i18n/translation/lib/yaml.rb

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (YamlDocument) initialize(yaml = '')

A new instance of YamlDocument



58
59
60
# File 'lib/generators/i18n/translation/lib/yaml.rb', line 58

def initialize(yaml = '')
  @lines = yaml.split("\n").map {|s| Line.new s}
end

Instance Attribute Details

- (Object) root

Returns the value of attribute root



56
57
58
# File 'lib/generators/i18n/translation/lib/yaml.rb', line 56

def root
  @root
end

Class Method Details

+ (Object) load_yml_file(yml_path)



62
63
64
65
66
67
68
# File 'lib/generators/i18n/translation/lib/yaml.rb', line 62

def self.load_yml_file(yml_path)
  if File.exists? yml_path
    self.new File.read(yml_path)
  else
    self.new
  end
end

Instance Method Details

- (Object) [](*path)



70
71
72
# File 'lib/generators/i18n/translation/lib/yaml.rb', line 70

def [](*path)
  find_line_by_path(path.flatten).try :value
end

- (Object) []=(*args)



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# 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

- (Object) find_line_by_path(path, line_num = -1,, add_new = false)



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# 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

- (Object) to_s(add_blank_line = false)



126
127
128
129
130
131
132
133
134
135
# 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.ends_with?("\n\n")
      previous_indent = line.indent
      ret << line.to_s << "\n"
    end
  end
end