Sha256: ae09c5645fc8257024a86b9dde687f1be2ef15fd78023f021854b146024ca21f

Contents?: true

Size: 1.35 KB

Versions: 11

Compression:

Stored size: 1.35 KB

Contents

module Aigu
  class EmberExporter < Exporter
  protected

    def build_output
      @output = {}

      pattern = File.join(@input_directory, "#{@locale}/translations.js")
      Dir[pattern].each do |file|
        filepath = file.gsub(/\A#{@input_directory}\//, '')

        if ignored_filepath?(filepath)
          puts "Ignoring #{filepath}"
        else
          puts "Processing #{filepath}"

          content = File.read(file).gsub(/^export default (.*);$/m, '\1')
          content = JSON.parse(content)
          content = flattenize_hash(content)

          @output.merge! content
        end
      end
    end

    def write_json_file
      file_path = @output_file
      puts "Generating #{file_path}"
      FileUtils.mkdir_p(File.dirname(file_path))

      File.open(file_path, 'w+') do |file|
        file << @output.to_json
      end
    end

    def flattenize_hash(hash, base_key = '')
      if hash.is_a?(Hash)
        hash.reduce({}) do |memo, (key, value)|
          new_base_key = [base_key, key].join('.').gsub(/\|\.+/, '|')
          memo.merge! flattenize_hash(value, new_base_key)
        end
      else
        { base_key.gsub(/^\./, '') => hash }
      end
    end

    def ignored_filepath?(filepath)
      @ignore && @ignore.any? do |pattern|
        File.fnmatch(pattern, filepath, File::FNM_PATHNAME | File::FNM_DOTMATCH)
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
aigu-1.2 lib/aigu/ember_exporter.rb
aigu-1.1.1 lib/aigu/ember_exporter.rb
aigu-1.1 lib/aigu/ember_exporter.rb
aigu-1.0.3 lib/aigu/ember_exporter.rb
aigu-1.0.2 lib/aigu/ember_exporter.rb
aigu-1.0.1 lib/aigu/ember_exporter.rb
aigu-1.0 lib/aigu/ember_exporter.rb
aigu-0.7 lib/aigu/ember_exporter.rb
aigu-0.6.1 lib/aigu/ember_exporter.rb
aigu-0.6 lib/aigu/ember_exporter.rb
aigu-0.5.1 lib/aigu/ember_exporter.rb