Class: I18nFlow::Repository

Inherits:
Object
  • Object
show all
Defined in:
lib/i18n_flow/repository.rb

Instance Method Summary collapse

Constructor Details

#initialize(base_path:, glob_patterns:) ⇒ Repository

Returns a new instance of Repository.



6
7
8
9
10
11
12
# File 'lib/i18n_flow/repository.rb', line 6

def initialize(
  base_path:,
  glob_patterns:
)
  @base_path     = Pathname.new(base_path)
  @glob_patterns = glob_patterns.to_a
end

Instance Method Details

#asts_by_pathObject



19
20
21
22
23
24
25
26
27
28
# File 'lib/i18n_flow/repository.rb', line 19

def asts_by_path
  @asts ||= file_paths
    .map { |path|
      rel_path = Pathname.new(path).relative_path_from(@base_path).to_s
      parser = I18nFlow::Parser.new(File.read(path), file_path: rel_path)
      parser.parse!
      [rel_path, parser.root_proxy]
    }
    .to_h
end

#asts_by_scopeObject



30
31
32
33
34
35
36
37
38
# File 'lib/i18n_flow/repository.rb', line 30

def asts_by_scope
  @asts_by_scope ||= Hash.new { |h, k| h[k] = {} }
    .tap { |h|
      asts_by_path.each { |path, tree|
        locale, *scopes = I18nFlow::Util.filepath_to_scope(path)
        h[scopes.join('.')][locale] = tree
      }
    }
end

#file_pathsObject



14
15
16
17
# File 'lib/i18n_flow/repository.rb', line 14

def file_paths
  @file_paths ||= @glob_patterns
    .flat_map { |pattern| Dir.glob(@base_path.join(pattern)) }
end