Class: MissingText::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/missing_text/writer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Writer

Returns a new instance of Writer.



7
8
9
10
11
12
# File 'lib/missing_text/writer.rb', line 7

def initialize(options = {})
  options.each do |key, value|
    self.send("#{key}=", value)
  end
  self.batch_id = MissingText::Batch.last.id
end

Instance Attribute Details

#batch_idObject

Returns the value of attribute batch_id.



5
6
7
# File 'lib/missing_text/writer.rb', line 5

def batch_id
  @batch_id
end

#diffmapObject

Returns the value of attribute diffmap.



5
6
7
# File 'lib/missing_text/writer.rb', line 5

def diffmap
  @diffmap
end

#filesObject

Returns the value of attribute files.



5
6
7
# File 'lib/missing_text/writer.rb', line 5

def files
  @files
end

#hashesObject

Returns the value of attribute hashes.



5
6
7
# File 'lib/missing_text/writer.rb', line 5

def hashes
  @hashes
end

#langmapObject

Returns the value of attribute langmap.



5
6
7
# File 'lib/missing_text/writer.rb', line 5

def langmap
  @langmap
end

#languagesObject

Returns the value of attribute languages.



5
6
7
# File 'lib/missing_text/writer.rb', line 5

def languages
  @languages
end

#parent_dirObject

Returns the value of attribute parent_dir.



5
6
7
# File 'lib/missing_text/writer.rb', line 5

def parent_dir
  @parent_dir
end

Instance Method Details

#get_entry_for(entry, language) ⇒ Object

Takes in a locale code and returns the string for that language



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/missing_text/writer.rb', line 63

def get_entry_for(entry, language)
  if entry.length > 1
    entry_string = get_entry_for_rec(entry[1..-1], language, hashes[language][entry[0]])
  else
    entry_string = hashes[language][entry[0]]
  end

  if entry_string.kind_of?(Array)
    entry_string = entry_string.map(&:inspect).join(', ')
  end
  entry_string
end

#get_entry_for_rec(entry, language, subhash) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/missing_text/writer.rb', line 76

def get_entry_for_rec(entry, language, subhash)
  if entry.length > 1
    get_entry_for_rec(entry[1..-1], language, subhash[entry[0]])
  else
    subhash[entry[0]]
  end
end

#writeObject



14
15
16
17
# File 'lib/missing_text/writer.rb', line 14

def write
  @record = write_record
  write_entries(@record)
end

#write_entries(record) ⇒ Object



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
55
56
57
58
59
60
# File 'lib/missing_text/writer.rb', line 28

def write_entries(record)
  languages.each do |lang|
    # for each language, get the entries from the diffmap where lang is the base language
    lang_entries = diffmap.select{|k, v| k.first == lang }

    # so now we have all entires that specify the target language. That is to say, comparator will be [:lang1, :lang2] where all entries in items are in lang1 but not in lang2
    # we need to find out all the languages that the item for lang1 is missing in and write that to the db
    entry_map = {}
    lang_entries.each do |comparator, items|
      target_language = comparator.last

      items.each do |item|
        if entry_map.has_key?(item)
          entry_map[item] << target_language
        else
          entry_map[item] = [target_language]
        end
      end

    end

    # There doesn't seem to be an efficient way to do this in one giant update... if anybody knows of another way please let me know!
    MissingText::Entry.transaction{
      MissingText::Entry.create(entry_map.map do |entry, target_languages|
          {missing_text_records_id: @record.id,
            locale_code: entry.join("."),
            base_language: lang.to_s,
            base_string: get_entry_for(entry, lang),
            target_languages: target_languages}
        end)
    }
  end
end

#write_recordObject



19
20
21
22
23
24
25
26
# File 'lib/missing_text/writer.rb', line 19

def write_record
  record = Record.create(
    parent_dir: self.parent_dir,
    files: self.files,
    missing_text_batch_id: self.batch_id
    )
  record
end