Sha256: d27d3da55c28031587732b6bf83e1c0e9daee7f61791c0cad59dd5f37d97c832

Contents?: true

Size: 1.71 KB

Versions: 3

Compression:

Stored size: 1.71 KB

Contents

require 'i18n/tasks/relative_keys'
module I18n::Tasks::Scanners
  class BaseScanner
    include ::I18n::Tasks::RelativeKeys
    attr_reader :config

    def initialize(config)
      @config = config.dup.with_indifferent_access.tap do |conf|
        conf[:paths]   = %w(app/) if conf[:paths].blank?
        conf[:include] = Array(conf[:include]) if conf[:include].present?
        conf[:exclude] = Array(conf[:exclude])
      end
    end

    # @return [Array] found key usages, absolutized and unique
    def keys
      @keys ||= traverse_files { |path| scan_file(path) }.flatten.uniq
    end

    # @return [String] keys used in file (unimplemented)
    def scan_file(path)
      raise 'Unimplemented'
    end

    # Run given block for every relevant file, according to config
    # @return [Array] Results of block calls
    def traverse_files
      result = []
      Find.find(*config[:paths]) do |path|
        next if File.directory?(path)
        next if config[:include] and !config[:include].any? { |glob| File.fnmatch(glob, path) }
        next if config[:exclude].any? { |glob| File.fnmatch(glob, path) }
        result << yield(path)
      end
      result
    end

    protected

    def extract_key_from_match(match, path)
      key = strip_literal(match[0])
      key = absolutize_key(key, path) if path && key.start_with?('.')
      key
    end

    # remove the leading colon and unwrap quotes from the key match
    def strip_literal(literal)
      key = literal
      key.slice!(0) if ':' == key[0]
      key = key[1..-2] if %w(' ").include?(key[0])
      key
    end

    VALID_KEY_RE = /^[\w.\#{}]+$/

    def valid_key?(key)
      key =~ VALID_KEY_RE
    end

    def relative_roots
      config[:relative_roots]
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
i18n-tasks-0.2.19 lib/i18n/tasks/scanners/base_scanner.rb
i18n-tasks-0.2.18 lib/i18n/tasks/scanners/base_scanner.rb
i18n-tasks-0.2.17 lib/i18n/tasks/scanners/base_scanner.rb