Class: Tr8n::Tokens::Method

Inherits:
Data
  • Object
show all
Defined in:
lib/tr8n/tokens/method.rb

Overview

Method Token Forms

useruser.name

user.name:gender

Instance Attribute Summary

Attributes inherited from Data

#case_keys, #context_keys, #full_name, #label, #short_name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Data

#apply_case, #apply_language_cases, #context_for_language, #error, #initialize, #key, #language_cases_enabled?, #name, #name_for_case_keys, parse, #parse_elements, #sanitize, #sanitized_name, #to_s, token_object, #token_value, #token_value_from_array_param, #token_value_from_hash_param, #token_values_from_array

Constructor Details

This class inherits a constructor from Tr8n::Tokens::Data

Class Method Details

.expressionObject



43
44
45
# File 'lib/tr8n/tokens/method.rb', line 43

def self.expression
  /(\{[^_:.][\w]*(\.[\w]+)(:[\w]+)*(::[\w]+)*\})/
end

Instance Method Details

#object_method_nameObject



51
52
53
# File 'lib/tr8n/tokens/method.rb', line 51

def object_method_name
  @object_method_name ||= short_name.split(".").last
end

#object_nameObject



47
48
49
# File 'lib/tr8n/tokens/method.rb', line 47

def object_name
  @object_name ||= short_name.split(".").first
end

#substitute(label, context, language, options = {}) ⇒ Object

Raises:



55
56
57
58
59
60
# File 'lib/tr8n/tokens/method.rb', line 55

def substitute(label, context, language, options = {})
  object = Tr8n::Utils.hash_value(context, object_name)
  raise Tr8n::Exception.new("Missing value for a token: #{full_name}") unless object
  object_value = sanitize(object.send(object_method_name), object, language, options.merge(:safe => false))
  label.gsub(full_name, object_value)
end