Class: Vocab::Converter::Rails

Inherits:
Base
  • Object
show all
Defined in:
lib/vocab/converter/rails.rb

Constant Summary collapse

SEPARATOR_ESCAPE_CHAR =
"\001"

Class Method Summary collapse

Class Method Details

.convert_xml_to_yml(file = nil) ⇒ Object



7
8
9
10
11
12
13
14
# File 'lib/vocab/converter/rails.rb', line 7

def convert_xml_to_yml( file = nil )
  xml_root = 'hash'
  keys = xml_to_yml_keys( Hash.from_xml( File.read( file ) )[ xml_root ] )
  unwound_keys = unwind_keys( keys )
  out_dir = File.dirname( file )
  out_file = "#{out_dir}/#{File.basename( file, '.xml' ) + '.yml'}"
  File.open( out_file, 'w' ) {|f| f.puts( ( keys_to_yaml( unwound_keys ) ) ) }
end

.convert_yml_to_xml(file = nil) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/vocab/converter/rails.rb', line 65

def convert_yml_to_xml( file = nil )
  xml_root = 'hash'
  yml = YAML.load_file( file )
  wound_keys = wind_keys( yml )

  out_dir = File.dirname( file )
  out_file = "#{out_dir}/#{File.basename( file, '.yml' ) + '.xml'}"
  File.open( out_file, 'w' ) { |f| f.puts( ( keys_to_xml( wound_keys ) ) ) }
end

.escape_default_separator(key, separator = nil) ⇒ Object



79
80
81
# File 'lib/vocab/converter/rails.rb', line 79

def escape_default_separator( key, separator = nil )
  key.to_s.tr( separator || I18n.default_separator, SEPARATOR_ESCAPE_CHAR )
end

.keys_to_xml(keys) ⇒ Object



60
61
62
63
# File 'lib/vocab/converter/rails.rb', line 60

def keys_to_xml( keys )
  raise "to_xml is broken" unless keys.respond_to?( :to_xml )
  keys.to_xml
end

.keys_to_yaml(keys) ⇒ Object



55
56
57
58
# File 'lib/vocab/converter/rails.rb', line 55

def keys_to_yaml( keys )
  # Using ya2yaml, if available, for UTF8 support
  keys.respond_to?( :ya2yaml ) ? keys.ya2yaml( :escape_as_utf8 => true ) : keys.to_yaml
end

.unescape_default_separator(key, separator = nil) ⇒ Object



75
76
77
# File 'lib/vocab/converter/rails.rb', line 75

def unescape_default_separator( key, separator = nil ) 
  key.to_s.tr( SEPARATOR_ESCAPE_CHAR, separator || I18n.default_separator ).to_sym
end

.unwind_keys(hash, separator = ".") ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/vocab/converter/rails.rb', line 25

def unwind_keys( hash, separator = "." )
  result = {}
  hash.each do |key, value|
    keys = key.to_s.split( separator )
    curr = result
    curr = curr[ keys.shift ] ||= {} while keys.size > 1
        curr[ keys.shift ] = value
  end

  return result
end

.wind_keys(hash, separator = nil, subtree = false, prev_key = nil, result = {}, orig_hash = hash) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/vocab/converter/rails.rb', line 37

def wind_keys( hash, separator = nil, subtree = false, prev_key = nil, result = {}, orig_hash = hash )
  separator ||= I18n.default_separator

  hash.each_pair do |key, value|
    key = escape_default_separator( key, separator )
    curr_key = [ prev_key, key ].compact.join( separator ).to_sym

    if value.is_a?( Hash )
      result[ curr_key ] = value if subtree
      wind_keys( value, separator, subtree, curr_key, result, orig_hash )
    else
      result[ unescape_default_separator( curr_key ) ] = value
    end
  end

  return result
end

.xml_to_yml_keys(hash) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/vocab/converter/rails.rb', line 16

def xml_to_yml_keys( hash )
  hash.inject( {} ) { |result, ( key, value )|
    value = xml_to_yml_keys( value ) if value.is_a? Hash
    yml_key = key.gsub( "-", "_" )
    result[ yml_key ] = value
    result
  }
end