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/dotted-key',
  'ANDROID'      => 'xml/android',
  'TXT'          => 'txt/lines'
}.freeze
SERIALIZER_MAP =
{
  'YML'          => 'yaml/rails',
  'YAML'         => 'yaml/rails',
  'KEYVALUEJSON' => 'json/dotted-key',
  'ANDROID'      => 'xml/android',
  'TXT'          => 'txt/lines'
}.freeze
DEFAULT_OPTIONS_MAP =
{
  'json/dotted-key' => {
    pretty: true, indent_size: 4
  }.freeze
}.freeze

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.



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

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

Instance Attribute Details

#serialization_optionsObject



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

def serialization_options
  @serialization_options || DEFAULT_OPTIONS_MAP.fetch(serializer_id, {})
end

#tx_resourceObject (readonly)

Returns the value of attribute tx_resource.



38
39
40
# File 'lib/txgh/resource_contents.rb', line 38

def tx_resource
  @tx_resource
end

Class Method Details

.from_phrase_list(tx_resource, phrases) ⇒ Object



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

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

.from_string(tx_resource, string) ⇒ Object



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

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

Instance Method Details

#add(key, value) ⇒ Object



59
60
61
# File 'lib/txgh/resource_contents.rb', line 59

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

#diff(other_contents) ⇒ Object



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

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



95
96
97
# File 'lib/txgh/resource_contents.rb', line 95

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

#empty?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/txgh/resource_contents.rb', line 103

def empty?
  phrases.empty?
end

#merge(other_contents, diff_hash) ⇒ Object



99
100
101
# File 'lib/txgh/resource_contents.rb', line 99

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

#phrasesObject



51
52
53
54
55
56
57
# File 'lib/txgh/resource_contents.rb', line 51

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



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

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

#to_s(language = tx_resource.source_lang) ⇒ Object

see comment above write_to



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

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.



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

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