Class: Tml::Tokens::Map

Inherits:
Data
  • Object
show all
Defined in:
lib/tml/tokens/map.rb

Overview

Map Token Forms

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

Instance Attribute Summary collapse

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, #decorate, #decoration_name, #error, #initialize, #key, #language_cases_enabled?, #name, #name_for_case_keys, parse, #sanitize, #sanitized_name, #to_s, token_object, #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 Tml::Tokens::Data

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



44
45
46
# File 'lib/tml/tokens/map.rb', line 44

def params
  @params
end

Class Method Details

.expressionObject



46
47
48
# File 'lib/tml/tokens/map.rb', line 46

def self.expression
  /(%?\{{1,2}\s*[\w]+\s*@\s*[^\{\}\|]+\}{1,2})/
end

Instance Method Details

#parse_elementsObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# 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 = {}) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# 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