Sha256: 3eb938590580132428f7471da86c3b206af216ea3458d533f244ccc6ceb0817f

Contents?: true

Size: 1.18 KB

Versions: 1

Compression:

Stored size: 1.18 KB

Contents

module Aigu
  class EmberImporter < Importer
    def process!
      puts "Generating JavaScript file in `#{@output_directory}` based on Accent-generated `#{@input_file}` file"
      puts '---'

      parse_json
      build_blob
      write_javascript_file

      puts '---'
      puts 'Done'
    end

  protected

    def parse_json
      json = File.read(@input_file)
      @object = JSON.parse(json)
    end

    def write_javascript_file
      file_path = File.join(@output_directory, "#{@locale}/translations.js")
      puts "Generating #{file_path}"
      FileUtils.mkdir_p(File.dirname(file_path))

      javascript_object = JSON.pretty_generate(@blob)
      content = "export default #{javascript_object};\n"

      File.open(file_path, 'w+') do |file|
        file << content
      end
    end

    def build_blob
      @blob = Hash.recursive

      @object.each_pair do |key, value|
        parts = key.split('.')
        hash = @blob[parts.first]

        parts.each_with_index do |part, index|
          next if index.zero?

          if index + 1 < parts.length
            hash = hash[part]
          else
            hash[part] = value
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
aigu-0.5 lib/aigu/ember_importer.rb