module Translatomatic::ResourceFile::KeyValueSupport

@private

Constants

Comment

@private

Definition

@private

Public Class Methods

key_value?() click to toggle source

(see Base.key_value?)

# File lib/translatomatic/resource_file/key_value_support.rb, line 6
def self.key_value?
  true
end

Public Instance Methods

save(target = path, options = {}) click to toggle source

(see Base#save)

# File lib/translatomatic/resource_file/key_value_support.rb, line 24
def save(target = path, options = {})
  add_created_by unless options[:no_created_by] || created_by?
  content = @elements.collect { |i| render_element(i) }.join
  content = content.gsub(/[\r\n]+\Z/, '') + "\n"
  target.write(content)
end
set(key, value) click to toggle source

(see Base#set)

Calls superclass method
# File lib/translatomatic/resource_file/key_value_support.rb, line 11
def set(key, value)
  super(key, value)
  if @map.include?(key)
    @map[key].value = value
  else
    element = Definition.new(key, value)
    @elements << element
    @map[key] = element
    @properties[key] = value
  end
end

Private Instance Methods

add_created_by() click to toggle source
# File lib/translatomatic/resource_file/key_value_support.rb, line 95
def add_created_by
  @created_by ||= begin
    created_by = Comment.new(created_by)
    @elements.unshift(created_by)
    created_by
  end
end
content_to_comment(item) click to toggle source
# File lib/translatomatic/resource_file/key_value_support.rb, line 90
def content_to_comment(item)
  text = unescape(item[1])
  Comment.new(text, item[2])
end
content_to_definition(item) click to toggle source
# File lib/translatomatic/resource_file/key_value_support.rb, line 82
def content_to_definition(item)
  key = unescape(item[1])
  value = unescape(item[2])
  # remove line continuations
  value = value.gsub(/\\\n\s*/, '')
  Definition.new(key, value)
end
escape(value) click to toggle source
# File lib/translatomatic/resource_file/key_value_support.rb, line 103
def escape(value)
  Translatomatic::StringEscaping.escape(value)
end
init() click to toggle source
# File lib/translatomatic/resource_file/key_value_support.rb, line 33
def init
  @elements = []  # key/values or comment elements
  @map = {}       # map key to elements
end
init_elements() click to toggle source
# File lib/translatomatic/resource_file/key_value_support.rb, line 70
def init_elements
  # convert to a list of elements
  @doc.content.collect do |item|
    case item[0]
    when :comment
      content_to_comment(item)
    when :definition
      content_to_definition(item)
    end
  end
end
load() click to toggle source
# File lib/translatomatic/resource_file/key_value_support.rb, line 38
def load
  @metadata.reset
  @doc = read_doc
  @elements = init_elements
  @properties = {}
  @elements.each do |element|
    if element.is_a?(Comment)
      @metadata.parse_comment(element.text)
    elsif element.is_a?(Definition)
      @metadata.assign_key(element.key)
      @properties[element.key] = element.value
      @map[element.key] = element
    end
  end
end
parse_doc(_content) click to toggle source
# File lib/translatomatic/resource_file/key_value_support.rb, line 62
def parse_doc(_content)
  raise 'parse_doc must be implemented by subclass'
end
read_doc() click to toggle source

parse document to a list of elements

# File lib/translatomatic/resource_file/key_value_support.rb, line 55
def read_doc
  content = read_contents(@path)
  document = parse_doc(content)
  raise t('file.invalid') unless document
  document
end
render_element(_element) click to toggle source
# File lib/translatomatic/resource_file/key_value_support.rb, line 66
def render_element(_element)
  raise 'render_element must be implemented by subclass'
end
unescape(value) click to toggle source
# File lib/translatomatic/resource_file/key_value_support.rb, line 107
def unescape(value)
  Translatomatic::StringEscaping.unescape_all(value)
end