Class: I18nUno::Tree

Inherits:
Object
  • Object
show all
Defined in:
lib/i18n_uno/tree.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(locale) ⇒ Tree

Returns a new instance of Tree.



7
8
9
10
11
# File 'lib/i18n_uno/tree.rb', line 7

def initialize(locale)
  @locale = locale
  @data   = []       # [{ file: I18nUno::File, delta: I18nUno::DiffDelta }]
  collect_files
end

Instance Attribute Details

#localeObject (readonly)

Returns the value of attribute locale.



5
6
7
# File 'lib/i18n_uno/tree.rb', line 5

def locale
  @locale
end

Instance Method Details

#add_delta!(file, diff_delta) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/i18n_uno/tree.rb', line 48

def add_delta!(file, diff_delta)
  @data.each do |entry|
    if entry[:file] == file
      entry[:delta] = diff_delta
      break
    end
  end
end

#any?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/i18n_uno/tree.rb', line 61

def any?
  @data.any?
end

#clean_not_processed_filesObject



88
89
90
91
92
93
94
95
# File 'lib/i18n_uno/tree.rb', line 88

def clean_not_processed_files
  @data.each do |entry|
    if (entry[:new_file] && !entry[:processed])
      ::File.delete(entry[:file].file_path)
    end
  end

end

#each(&block) ⇒ Object



69
70
71
# File 'lib/i18n_uno/tree.rb', line 69

def each(&block)
  files.each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/i18n_uno/tree.rb', line 65

def empty?
  @data.empty?
end

#extract_entries_with_deltaObject



42
43
44
45
46
# File 'lib/i18n_uno/tree.rb', line 42

def extract_entries_with_delta
  @data.select do |entry|
    entry[:delta].present? && entry[:delta].any_changes?
  end
end

#filesObject



57
58
59
# File 'lib/i18n_uno/tree.rb', line 57

def files
  @data.map { |entry| entry[:file] }
end

#find_file(target_file) ⇒ Object



73
74
75
# File 'lib/i18n_uno/tree.rb', line 73

def find_file(target_file)
  files.find { |file| file.file_path.match(/#{target_file.file_identifier}/) }
end

#find_or_create_file(sot_file) ⇒ Object



77
78
79
80
81
82
83
84
85
86
# File 'lib/i18n_uno/tree.rb', line 77

def find_or_create_file(sot_file)
  file = find_file(sot_file)

  if file.nil?
    file = sot_file.create_in_locale(locale)
    @data << { file: file, delta: nil, new_file: true, processed: false }
  end

  file
end

#process_delta_changes(source_of_truth_tree) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/i18n_uno/tree.rb', line 13

def process_delta_changes(source_of_truth_tree)
  entries_with_delta = extract_entries_with_delta

  if entries_with_delta.empty?
    puts "No changes detected for '#{locale}' locale."
    return
  end

  files_completed  = 0
  puts "Processing locale '#{locale}' there are #{entries_with_delta.count} files with changes. "
  entries_with_delta.each_with_index do |entry, index|
    printf("Processing file %-40s [#{index + 1}/#{entries_with_delta.count}] ... ", "'#{entry[:file].pp_file_name}'")

    if entry[:delta].any_new_keys?
      sot_file = source_of_truth_tree.find_file(entry[:file])
      translator = I18nUno::Translator.new(sot_file, entry[:file])
      translator.process(entry[:delta])
    end

    if entry[:delta].any_removed_keys? && !entry[:delta].complete_file_diff?
      entry[:file].remove_keys_from_file!(entry[:delta])
    end

    entry[:processed] = true

    puts 'OK'
  end
end