Class: AwesomeTranslations::ErbInspector::FileInspector

Inherits:
Object
  • Object
show all
Defined in:
lib/awesome_translations/erb_inspector/file_inspector.rb

Constant Summary collapse

JS_FILE_EXTS =
[".cjs", ".coffee", ".coffee.erb", ".es6", ".es6.erb", ".js", ".js.erb", ".jsx", ".mjs"].freeze
METHOD_NAMES =
%w[t controller_t helper_t].freeze
VALID_BEGINNING =
'(^|\s+|\(|\{|\[|<%=\s*|I18n\.)'.freeze
VALID_BEGINNING_JS =
'(^|\s+|\(|\[|\{|\.)'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ FileInspector

Returns a new instance of FileInspector.



9
10
11
12
13
14
# File 'lib/awesome_translations/erb_inspector/file_inspector.rb', line 9

def initialize(args)
  @args = args
  @root_path = args[:root_path]
  @file_path = args[:file_path]
  @method_names ||= ["t"]
end

Instance Attribute Details

#file_pathObject (readonly)

Returns the value of attribute file_path.



7
8
9
# File 'lib/awesome_translations/erb_inspector/file_inspector.rb', line 7

def file_path
  @file_path
end

#root_pathObject (readonly)

Returns the value of attribute root_path.



7
8
9
# File 'lib/awesome_translations/erb_inspector/file_inspector.rb', line 7

def root_path
  @root_path
end

Instance Method Details

#basenameObject



44
45
46
# File 'lib/awesome_translations/erb_inspector/file_inspector.rb', line 44

def basename
  File.basename(@file_path).match(/\A(.+?)\./)[1]
end

#changed?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/awesome_translations/erb_inspector/file_inspector.rb', line 48

def changed?
  @args.fetch(:changed)
end

#full_pathObject



40
41
42
# File 'lib/awesome_translations/erb_inspector/file_inspector.rb', line 40

def full_path
  "#{@root_path}/#{@file_path}"
end

#translationsObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/awesome_translations/erb_inspector/file_inspector.rb', line 16

def translations
  Enumerator.new do |yielder|
    File.open(full_path, "r") do |fp|
      extname = File.extname(full_path)
      translations_found = []
      line_no = 0
      @namespace = nil

      fp.each_line do |line|
        line_no += 1
        line = force_utf8(line)

        if extname == ".liquid"
          parse_content_liquid(line_no, line, translations_found, yielder)
        elsif JS_FILE_EXTS.include?(extname)
          parse_content_js(line_no, line, translations_found, yielder)
        else
          parse_content(line_no, line, translations_found, yielder)
        end
      end
    end
  end
end