Class: Yamlstrings::Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/yamlstrings/converter.rb

Instance Method Summary collapse

Constructor Details

#initialize(paths = nil) ⇒ Converter

Returns a new instance of Converter.



5
6
7
8
9
10
11
# File 'lib/yamlstrings/converter.rb', line 5

def initialize(paths=nil)
  @keys = {}
  @paths = []
  for p in paths
    add_file(p)
  end if Array === paths
end

Instance Method Details

#add_file(path) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/yamlstrings/converter.rb', line 13

def add_file(path)
  keys = YAML.load_file(path)
  prefix = keys['_prefix']
  keys.delete('_prefix')
  keys = {prefix => keys} if prefix
  @keys.merge!(keys)
end

#to_file(path) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/yamlstrings/converter.rb', line 21

def to_file(path)
  flat = @keys.hflatten
  out = []

  open(path, 'w') do |out|
    flat.each_pair do |k, v|
      k = k.to_s.gsub(/\._/, '')
      v = v.gsub('"', '\"').gsub("\n", '\n')
      k = k.gsub('"', '').gsub("\n", '')
      out << %|"#{k}" = "#{v}";\n|
    end
  end
end