class Tr8n::Tokens::TransformToken

Public Class Methods

expression() click to toggle source
# File lib/tr8n/tokens/transform_token.rb, line 42
def self.expression
  /(\{[^_:|][\w]*(:[\w]+)?(::[\w]+)?\s*\|\|?[^{^}]+\})/
end

Public Instance Methods

allowed_in_translation?() click to toggle source
# File lib/tr8n/tokens/transform_token.rb, line 62
def allowed_in_translation?
  pipe_separator == "||" 
end
name() click to toggle source
# File lib/tr8n/tokens/transform_token.rb, line 46
def name
  @name ||= declared_name.split('|').first.split(':').first.strip
end
pipe_separator() click to toggle source
# File lib/tr8n/tokens/transform_token.rb, line 54
def pipe_separator
  @pipe_separator ||= (full_name.index("||") ? "||" : "|") 
end
piped_params() click to toggle source
# 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) click to toggle source

return only the internal part

# 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) click to toggle source

return with the default transform substitution

# 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() click to toggle source
# File lib/tr8n/tokens/transform_token.rb, line 50
def sanitized_name
  "{#{name}}"
end
substitute(label, values = {}, options = {}, language = Tr8n::Config.current_language) click to toggle source
# File lib/tr8n/tokens/transform_token.rb, line 109
def substitute(label, values = {}, options = {}, language = Tr8n::Config.current_language)
  # only the default language allows for the transform tokens
  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) click to toggle source
# File lib/tr8n/tokens/transform_token.rb, line 66
def token_object(object)
  # token is an array
  if object.is_a?(Array)
    # if you provided an array, it better have some values
    if object.empty?
      return raise Tr8n::TokenException.new("Invalid array object for a transform token: #{full_name}")
    end
    
    # if the first item in the array is an object, process it
    return object.first
  end
    
  object    
end
validate_language_rule() click to toggle source
# 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