class Tml::Tokens::Map

Map Token Forms

tr(“{user} likes this {animal @ dog: dog, cat: cat, bird: bird}”, user: “Michael”, animal: “dog”) tr(“{user} likes this {animal @ dog, cat, bird}”, user: “Michael”, animal: 0)

Attributes

params[R]

Public Class Methods

expression() click to toggle source
# File lib/tml/tokens/map.rb, line 46
def self.expression
  /(%?\{{1,2}\s*[\w]+\s*@\s*[^\{\}\|]+\}{1,2})/
end

Public Instance Methods

parse_elements() click to toggle source
# File lib/tml/tokens/map.rb, line 50
def parse_elements
  name_without_parens = @full_name.gsub(/^%/, '')[1..-2]
  @context_keys = []
  @case_keys = []
  @short_name, @params = name_without_parens.split('@')
  @short_name.strip!
  @params = @params.split(',').collect{|param| param.strip}
  if @params.first.index(':')
    hash = {}
    @params.each do |param|
      key, value = param.split(':')
      hash[key.to_s.strip] = value.to_s.strip
    end
    @params = hash
  end
end
substitute(label, context, language, options = {}) click to toggle source
# File lib/tml/tokens/map.rb, line 67
def substitute(label, context, language, options = {})
  object = self.class.token_object(context, key)

  if object.nil?
    return error("Missing value for a token \"#{key}\" in \"#{label}\"", false)
  end

  if params.empty?
    return error("Params may not be empty for token \"#{key}\" in \"#{label}\"", false)
  end

  object_value = params[object]

  label.gsub(full_name, decorate(object_value, options))
end