Class: Translatomatic::Metadata

Inherits:
Object
  • Object
show all
Defined in:
lib/translatomatic/metadata.rb

Overview

Parses tm.context comments in resource files

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMetadata

Returns a new instance of Metadata.



6
7
8
# File 'lib/translatomatic/metadata.rb', line 6

def initialize
  reset
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



4
5
6
# File 'lib/translatomatic/metadata.rb', line 4

def context
  @context
end

Instance Method Details

#add_context(context) ⇒ Object

Add to the current context

Parameters:

  • context (String)

    A context string



39
40
41
42
43
# File 'lib/translatomatic/metadata.rb', line 39

def add_context(context)
  return unless context.present?
  @current_context ||= []
  @current_context << context.strip
end

#assign_key(key, options = {}) ⇒ Object

Assign current metadata to the given key

Parameters:

  • key (String)

    name of the key



25
26
27
28
29
30
# File 'lib/translatomatic/metadata.rb', line 25

def assign_key(key, options = {})
  if @current_context.present?
    @context[key] = @current_context
    @current_context = nil unless options[:keep_context]
  end
end

#clear_contextObject

Clear the current context



33
34
35
# File 'lib/translatomatic/metadata.rb', line 33

def clear_context
  @current_context = nil
end

#get_context(key) ⇒ Array<String>

Find associated context(s) for a property. Contexts are defined using tm.context comments in resource files.

Returns:

  • (Array<String>)

    Context for the given key



13
14
15
# File 'lib/translatomatic/metadata.rb', line 13

def get_context(key)
  @context[key]
end

#parse_comment(comment) ⇒ Array

Parse comment text and extract metadata

Returns:

  • (Array)

    parsed context data



47
48
49
50
51
52
53
54
55
56
# File 'lib/translatomatic/metadata.rb', line 47

def parse_comment(comment)
  return nil if comment.blank?
  contexts = comment.scan(/tm\.context:\s*(.*)/)
  result = []
  contexts.each do |i|
    add_context(i[0])
    result << i[0]
  end
  result
end

#resetObject

Clear all metadata



18
19
20
21
# File 'lib/translatomatic/metadata.rb', line 18

def reset
  @context = {}
  @current_context = nil
end