Class: Tr8n::Tokens::TransformToken
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Tr8n::Token
#apply_case, #case_key, #caseless_name, #declared_name, #decoration?, #dependant?, #dependencies, #dependency, #dependency_rules, #evaluate_token_method_array, #full_name, #has_case_key?, #has_type?, #initialize, #language_rule, #language_rules, #name_for_case, #name_key, #name_with_case, #original_label, parse, #permutable_name, #pipeless_name, register_data_tokens, register_decoration_tokens, #sanitize_token_value, #sanitized_name_for_case, #suffix, #supports_cases?, #to_s, #token_array_value, #token_value, #type
Constructor Details
This class inherits a constructor from Tr8n::Token
Class Method Details
.expression ⇒ Object
42
43
44
|
# File 'lib/tr8n/tokens/transform_token.rb', line 42
def self.expression
/(\{[^_:|][\w]*(:[\w]+)?(::[\w]+)?\s*\|\|?[^{^}]+\})/
end
|
Instance Method Details
#allowed_in_translation? ⇒ Boolean
62
63
64
|
# File 'lib/tr8n/tokens/transform_token.rb', line 62
def allowed_in_translation?
pipe_separator == "||"
end
|
#name ⇒ Object
46
47
48
|
# File 'lib/tr8n/tokens/transform_token.rb', line 46
def name
@name ||= declared_name.split('|').first.split(':').first.strip
end
|
#pipe_separator ⇒ Object
54
55
56
|
# File 'lib/tr8n/tokens/transform_token.rb', line 54
def pipe_separator
@pipe_separator ||= (full_name.index("||") ? "||" : "|")
end
|
#piped_params ⇒ Object
58
59
60
|
# File 'lib/tr8n/tokens/transform_token.rb', line 58
def piped_params
@piped_params ||= declared_name.split(pipe_separator).last.split(",").collect{|param| param.strip}
end
|
#prepare_label_for_suggestion(label, index) ⇒ Object
return only the internal part
104
105
106
107
|
# File 'lib/tr8n/tokens/transform_token.rb', line 104
def prepare_label_for_suggestion(label, index)
validate_language_rule
label.gsub(full_name, language_rule.default_transform(*piped_params))
end
|
#prepare_label_for_translator(label) ⇒ Object
return with the default transform substitution
92
93
94
95
96
97
98
99
100
101
|
# File 'lib/tr8n/tokens/transform_token.rb', line 92
def prepare_label_for_translator(label)
validate_language_rule
substitution_value = ""
substitution_value << sanitized_name if allowed_in_translation?
substitution_value << " " unless substitution_value.blank?
substitution_value << language_rule.default_transform(*piped_params)
label.gsub(full_name, substitution_value)
end
|
#sanitized_name ⇒ Object
50
51
52
|
# File 'lib/tr8n/tokens/transform_token.rb', line 50
def sanitized_name
"{#{name}}"
end
|
#substitute(label, values = {}, options = {}, language = Tr8n::Config.current_language) ⇒ Object
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
# File 'lib/tr8n/tokens/transform_token.rb', line 109
def substitute(label, values = {}, options = {}, language = Tr8n::Config.current_language)
return label unless language.default?
object = values[name_key]
unless object
raise Tr8n::TokenException.new("Missing value for a token: #{full_name}")
end
validate_language_rule
params = [token_object(object)] + piped_params
substitution_value = ""
substitution_value << token_value(object, options, language) if allowed_in_translation?
substitution_value << " " unless substitution_value.blank?
substitution_value << language_rule.transform(*params)
label.gsub(full_name, substitution_value)
end
|
#token_object(object) ⇒ Object
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
# File 'lib/tr8n/tokens/transform_token.rb', line 66
def token_object(object)
if object.is_a?(Array)
if object.empty?
return raise Tr8n::TokenException.new("Invalid array object for a transform token: #{full_name}")
end
return object.first
end
object
end
|
#validate_language_rule ⇒ Object
81
82
83
84
85
86
87
88
89
|
# File 'lib/tr8n/tokens/transform_token.rb', line 81
def validate_language_rule
unless dependant?
raise Tr8n::TokenException.new("Unknown dependency type for #{full_name} token in #{original_label}; no way to apply the transform method.")
end
unless language_rule.respond_to?(:default_transform)
raise Tr8n::TokenException.new("#{language_rule.class.name} does not respond to the default transform method.")
end
end
|