Class: Translatomatic::Metadata
- Inherits:
-
Object
- Object
- Translatomatic::Metadata
- Defined in:
- lib/translatomatic/metadata.rb
Overview
Parses tm.context comments in resource files
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
Instance Method Summary collapse
-
#add_context(context) ⇒ Object
Add to the current context.
-
#assign_key(key, options = {}) ⇒ Object
Assign current metadata to the given key.
-
#clear_context ⇒ Object
Clear the current context.
-
#get_context(key) ⇒ Array<String>
Find associated context(s) for a property.
-
#initialize ⇒ Metadata
constructor
A new instance of Metadata.
-
#parse_comment(comment) ⇒ Array
Parse comment text and extract metadata.
-
#reset ⇒ Object
Clear all metadata.
Constructor Details
#initialize ⇒ Metadata
Returns a new instance of Metadata.
6 7 8 |
# File 'lib/translatomatic/metadata.rb', line 6 def initialize reset end |
Instance Attribute Details
#context ⇒ Object (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
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
25 26 27 28 29 30 |
# File 'lib/translatomatic/metadata.rb', line 25 def assign_key(key, = {}) if @current_context.present? @context[key] = @current_context @current_context = nil unless [:keep_context] end end |
#clear_context ⇒ Object
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.
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
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 |
#reset ⇒ Object
Clear all metadata
18 19 20 21 |
# File 'lib/translatomatic/metadata.rb', line 18 def reset @context = {} @current_context = nil end |