Class: ConverterService

Inherits:
Object
  • Object
show all
Defined in:
app/services/ConverterService.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json_file = {}) ⇒ ConverterService

Returns a new instance of ConverterService.



5
6
7
8
# File 'app/services/ConverterService.rb', line 5

def initialize(json_file={})
  @locales_path = get_file_path
  @json_file = json_file
end

Instance Attribute Details

#json_fileObject (readonly)

Returns the value of attribute json_file.



2
3
4
# File 'app/services/ConverterService.rb', line 2

def json_file
  @json_file
end

#locales_pathObject (readonly)

Returns the value of attribute locales_path.



3
4
5
# File 'app/services/ConverterService.rb', line 3

def locales_path
  @locales_path
end

Instance Method Details

#get_file_pathObject



10
11
12
13
14
15
16
# File 'app/services/ConverterService.rb', line 10

def get_file_path
  if Kernel.const_defined? 'Rails'
    File.join(Rails.root, 'config/locales')
  else
    raise 'I need Rails to run!'
  end
end

#iterate_locale_dirObject

returns full json of all ymls



36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/services/ConverterService.rb', line 36

def iterate_locale_dir # returns full json of all ymls
  Dir.foreach(@locales_path) do |filename|

    next if filename == '.' or filename == '..' or filename.include? "devise"

    parsed_yml = parse_yml_to_json("#{@locales_path}/#{filename}")
    key = parsed_yml.keys.first

    @json_file[key] = parsed_yml[key]
  end
  @json_file = @json_file.to_json
end

#parse_json_to_ymlObject

method for I M P O R T I N G



19
20
21
22
23
24
25
26
27
28
# File 'app/services/ConverterService.rb', line 19

def parse_json_to_yml # creates individual ymls for each locale
  locale_arr = []
  @json_file.keys.each { |key| locale_arr << {key => @json_file[key]} }

  locale_arr.each do |locale|
    y_file = File.open("#{@locales_path}/#{locale.keys.first}.yml", 'w')
    y_file.write(YAML.dump(locale))
    y_file.close
  end
end

#parse_yml_to_json(yml_file) ⇒ Object

methods for E X P O R T I N G



32
33
34
# File 'app/services/ConverterService.rb', line 32

def parse_yml_to_json(yml_file)
  j_file = YAML.load_file(File.open("#{yml_file}", 'r'))
end