Class: Txgh::ResourceContents

Inherits:
Object
  • Object
show all
Defined in:
lib/txgh/resource_contents.rb

Constant Summary collapse

EXTRACTOR_MAP =
{
  'YML'          => 'yaml/rails',
  'YAML'         => 'yaml/rails',
  'KEYVALUEJSON' => 'json/key-value',
  'ANDROID'      => 'xml/android',
  'TXT'          => 'txt/lines'
}
SERIALIZER_MAP =
{
  'YML'          => 'yaml/rails',
  'YAML'         => 'yaml/rails',
  'KEYVALUEJSON' => 'json/key-value',
  'ANDROID'      => 'xml/android',
  'TXT'          => 'txt/lines'
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tx_resource, options) ⇒ ResourceContents

Returns a new instance of ResourceContents.



34
35
36
37
38
# File 'lib/txgh/resource_contents.rb', line 34

def initialize(tx_resource, options)
  @tx_resource = tx_resource
  @phrases = options[:phrases]
  @raw = options[:raw]
end

Instance Attribute Details

#tx_resourceObject (readonly)

Returns the value of attribute tx_resource.



32
33
34
# File 'lib/txgh/resource_contents.rb', line 32

def tx_resource
  @tx_resource
end

Class Method Details

.from_phrase_list(tx_resource, phrases) ⇒ Object



23
24
25
# File 'lib/txgh/resource_contents.rb', line 23

def from_phrase_list(tx_resource, phrases)
  new(tx_resource, phrases: phrases)
end

.from_string(tx_resource, string) ⇒ Object



27
28
29
# File 'lib/txgh/resource_contents.rb', line 27

def from_string(tx_resource, string)
  new(tx_resource, raw: string)
end

Instance Method Details

#add(key, value) ⇒ Object



48
49
50
# File 'lib/txgh/resource_contents.rb', line 48

def add(key, value)
  phrases << { 'key' => key, 'string' => value }
end

#diff(other_contents) ⇒ Object



78
79
80
81
82
# File 'lib/txgh/resource_contents.rb', line 78

def diff(other_contents)
  diff = diff_hash(other_contents)
  diff_phrases = diff[:added] + diff[:modified]
  self.class.from_phrase_list(tx_resource, diff_phrases)
end

#diff_hash(other_contents) ⇒ Object



84
85
86
# File 'lib/txgh/resource_contents.rb', line 84

def diff_hash(other_contents)
  DiffCalculator.compare(phrases, other_contents.phrases)
end

#empty?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/txgh/resource_contents.rb', line 92

def empty?
  phrases.empty?
end

#merge(other_contents, diff_hash) ⇒ Object



88
89
90
# File 'lib/txgh/resource_contents.rb', line 88

def merge(other_contents, diff_hash)
  MergeCalculator.merge(other_contents, self, diff_hash)
end

#phrasesObject



40
41
42
43
44
45
46
# File 'lib/txgh/resource_contents.rb', line 40

def phrases
  @phrases ||= extractor.from_string(raw) do |extractor|
    extractor.extract_each(preserve_arrays: true).map do |key, value|
      { 'key' => key, 'string' => value }
    end
  end
end

#to_hObject



74
75
76
# File 'lib/txgh/resource_contents.rb', line 74

def to_h
  Utils.index_on('key', phrases)
end

#to_s(language = tx_resource.source_lang) ⇒ Object

see comment above write_to



68
69
70
71
72
# File 'lib/txgh/resource_contents.rb', line 68

def to_s(language = tx_resource.source_lang)
  stream = StringIO.new
  write_to(stream, language)
  stream.string
end

#write_to(stream, language = tx_resource.source_lang) ⇒ Object

Some formats like Rails YAML require the language to be written somewhere in the file. If you’re using this class to parse and serialize the contents of a translated version of a resource, then you’ll probably want to override the resource’s source language using the second parameter here.



57
58
59
60
61
62
63
64
65
# File 'lib/txgh/resource_contents.rb', line 57

def write_to(stream, language = tx_resource.source_lang)
  serializer.from_stream(stream, language) do |serializer|
    phrases.each do |phrase|
      serializer.write_key_value(
        phrase['key'], str(phrase['string'] || '')
      )
    end
  end
end