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'
}
SERIALIZER_MAP =
{
  'YML'          => 'yaml/rails',
  'YAML'         => 'yaml/rails',
  'KEYVALUEJSON' => 'json/key-value',
  'ANDROID'      => 'xml/android'
}

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.



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

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.



30
31
32
# File 'lib/txgh/resource_contents.rb', line 30

def tx_resource
  @tx_resource
end

Class Method Details

.from_phrase_list(tx_resource, phrases) ⇒ Object



21
22
23
# File 'lib/txgh/resource_contents.rb', line 21

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

.from_string(tx_resource, string) ⇒ Object



25
26
27
# File 'lib/txgh/resource_contents.rb', line 25

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

Instance Method Details

#add(key, value) ⇒ Object



46
47
48
# File 'lib/txgh/resource_contents.rb', line 46

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

#diff(other_contents) ⇒ Object



76
77
78
79
80
# File 'lib/txgh/resource_contents.rb', line 76

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



82
83
84
# File 'lib/txgh/resource_contents.rb', line 82

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

#empty?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/txgh/resource_contents.rb', line 90

def empty?
  phrases.empty?
end

#merge(other_contents, diff_hash) ⇒ Object



86
87
88
# File 'lib/txgh/resource_contents.rb', line 86

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

#phrasesObject



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

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

#to_hObject



72
73
74
# File 'lib/txgh/resource_contents.rb', line 72

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

#to_s(language = tx_resource.source_lang) ⇒ Object

see comment above write_to



66
67
68
69
70
# File 'lib/txgh/resource_contents.rb', line 66

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.



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

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'], (phrase['string'] || '').to_s
      )
    end
  end
end