Sha256: 34f18790cfdd380ce44e388fe93c686f0f197afe41e38935d8e7c82da26f796e

Contents?: true

Size: 1.46 KB

Versions: 19

Compression:

Stored size: 1.46 KB

Contents

# Used to find translations in the Rails app by inspecting .erb- and .haml-files.
class AwesomeTranslations::ErbInspector
  autoload :FileInspector, "#{File.dirname(__FILE__)}/erb_inspector/file_inspector"
  autoload :TranslationInspector, "#{File.dirname(__FILE__)}/erb_inspector/translation_inspector"

  def initialize(args = {})
    @args = args
    @args[:exts] ||= [".erb", ".haml", ".rb"]

    if @args[:dirs]
      @dirs = @args[:dirs]
    else
      @dirs = AwesomeTranslations.config.paths_to_translate
    end
  end

  # Yields all relevant .erb- and .haml-files.
  def files
    Enumerator.new do |yielder|
      @dirs.each do |dir|
        scan_dir("", dir, yielder)
      end
    end
  end

  def file(root_path, file_path)
    return AwesomeTranslations::ErbInspector::FileInspector.new(
      file_path: file_path,
      root_path: root_path
    )
  end

private

  def scan_dir(path, root_path, yielder)
    Dir.foreach("#{root_path}/#{path}") do |file|
      next if file == "." || file == ".."
      file_path = "#{path}"
      file_path << "/" unless file_path.empty?
      file_path << file

      full_path = "#{root_path}/#{file_path}"
      ext = File.extname(file_path)

      if File.directory?(full_path)
        scan_dir(file_path, root_path, yielder)
      elsif @args[:exts].include?(ext)
        yielder << AwesomeTranslations::ErbInspector::FileInspector.new(
          file_path: file_path,
          root_path: root_path
        )
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
awesome_translations-0.0.24 lib/awesome_translations/erb_inspector.rb
awesome_translations-0.0.23 lib/awesome_translations/erb_inspector.rb
awesome_translations-0.0.22 lib/awesome_translations/erb_inspector.rb
awesome_translations-0.0.21 lib/awesome_translations/erb_inspector.rb
awesome_translations-0.0.20 lib/awesome_translations/erb_inspector.rb
awesome_translations-0.0.19 lib/awesome_translations/erb_inspector.rb
awesome_translations-0.0.18 lib/awesome_translations/erb_inspector.rb
awesome_translations-0.0.17 lib/awesome_translations/erb_inspector.rb
awesome_translations-0.0.16 lib/awesome_translations/erb_inspector.rb
awesome_translations-0.0.15 lib/awesome_translations/erb_inspector.rb
awesome_translations-0.0.14 lib/awesome_translations/erb_inspector.rb
awesome_translations-0.0.13 lib/awesome_translations/erb_inspector.rb
awesome_translations-0.0.12 lib/awesome_translations/erb_inspector.rb
awesome_translations-0.0.10 lib/awesome_translations/erb_inspector.rb
awesome_translations-0.0.9 lib/awesome_translations/erb_inspector.rb
awesome_translations-0.0.8 lib/awesome_translations/erb_inspector.rb
awesome_translations-0.0.7 lib/awesome_translations/erb_inspector.rb
awesome_translations-0.0.6 lib/awesome_translations/erb_inspector.rb
awesome_translations-0.0.5 lib/awesome_translations/erb_inspector.rb