Class: FindT::FileScanner

Inherits:
Object
  • Object
show all
Defined in:
lib/find_t/file_scanner.rb

Constant Summary collapse

YAML_HASH_PATTERN =
/\A([ ]*)(["']|)(\w+)\2:\s*(.*)\s*\z/

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ FileScanner

Returns a new instance of FileScanner.



6
7
8
# File 'lib/find_t/file_scanner.rb', line 6

def initialize(file)
  @file = file
end

Instance Method Details

#find(scopes) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/find_t/file_scanner.rb', line 10

def find(scopes)
  @scopes = scopes
  @size   = @scopes.size - 1

  @current_level  = 0
  @current_locale = nil
  @founds         = []

  File.open(@file) do |f|
    f.each_line.with_index do |line, n|
      scan_line line, n
    end
  end

  @founds
end

#scan_line(line, n) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/find_t/file_scanner.rb', line 27

def scan_line(line, n)
  return unless line =~ YAML_HASH_PATTERN

  indent, scope, text = $1, $3, $4
  level = indent.size >> 1

  if level == 0 && text == ''
    self.locale = scope
    return
  end

  if level == @current_level
    if scope == current_scope
      if level == @size
        @founds << {
          locale: @current_locale,
          file:   @file,
          line:   n + 1,
          text:   text,
        }
      elsif '' == text
        @current_level = level + 1
      end
    end
  elsif level < @current_level
    @current_level = level
  end
end