Sha256: 5e83e3aea31f19a319ceb0efdbaf4b05a0cbce604d539af614085ef5b36f4511

Contents?: true

Size: 2 KB

Versions: 10

Compression:

Stored size: 2 KB

Contents

# Used to find translations in the Rails app by inspecting .erb- and .haml-files.
class AwesomeTranslations::ErbInspector
  AutoAutoloader.autoload_sub_classes(self, __FILE__)

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

    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)
    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.clone
      file_path << "/" unless file_path.empty?
      file_path << file

      file_path_with_path = "#{path}/#{file_path}"

      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)
        scanned_file = AwesomeTranslations::CacheDatabaseGenerator::ScannedFile.find_or_initialize_by(file_path: file_path_with_path)
        file_size = File.size(full_path)
        last_changed_at = File.mtime(full_path)

        changed = false
        changed = true unless file_size == scanned_file.file_size
        changed = true unless last_changed_at.to_s == scanned_file.last_changed_at.to_s

        if changed
          scanned_file.assign_attributes(
            file_size: file_size,
            last_changed_at: last_changed_at
          )
          scanned_file.save!
        end

        yielder << AwesomeTranslations::ErbInspector::FileInspector.new(
          file_path: file_path,
          root_path: root_path,
          changed: changed
        )
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
awesome_translations-0.0.36 lib/awesome_translations/erb_inspector.rb
awesome_translations-0.0.35 lib/awesome_translations/erb_inspector.rb
awesome_translations-0.0.34 lib/awesome_translations/erb_inspector.rb
awesome_translations-0.0.33 lib/awesome_translations/erb_inspector.rb
awesome_translations-0.0.32 lib/awesome_translations/erb_inspector.rb
awesome_translations-0.0.31 lib/awesome_translations/erb_inspector.rb
awesome_translations-0.0.30 lib/awesome_translations/erb_inspector.rb
awesome_translations-0.0.29 lib/awesome_translations/erb_inspector.rb
awesome_translations-0.0.28 lib/awesome_translations/erb_inspector.rb
awesome_translations-0.0.27 lib/awesome_translations/erb_inspector.rb