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 =
[".coffee", ".coffee.erb", ".es6", ".es6.erb", ".js", ".js.erb", ".jsx"].freeze
METHOD_NAMES =
%w[t controller_t helper_t].freeze
VALID_BEGINNING =
'(^|\s+|\(|\{|\[|<%=\s*|I18n\.)'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ FileInspector

Returns a new instance of FileInspector.



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

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.



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

def file_path
  @file_path
end

#root_pathObject (readonly)

Returns the value of attribute root_path.



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

def root_path
  @root_path
end

Instance Method Details

#basenameObject



42
43
44
# File 'lib/awesome_translations/erb_inspector/file_inspector.rb', line 42

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

#changed?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/awesome_translations/erb_inspector/file_inspector.rb', line 46

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

#full_pathObject



38
39
40
# File 'lib/awesome_translations/erb_inspector/file_inspector.rb', line 38

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

#translationsObject



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

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

      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