Module: I18nliner::Extensions::Inferpolation

Included in:
Controller, Model, View
Defined in:
lib/i18nliner/extensions/inferpolation.rb

Instance Method Summary collapse

Instance Method Details

#inferpolate(options) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/i18nliner/extensions/inferpolation.rb', line 4

def inferpolate(options)
  default = options[:default]
  return options unless default
  if default.is_a?(Hash)
    default.each { |key, value| inferpolate_value!(value, options) }
  else
    inferpolate_value!(default, options)
  end

  options
end

#inferpolate_value!(value, options) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/i18nliner/extensions/inferpolation.rb', line 16

def inferpolate_value!(value, options)
  value.gsub!(/%\{((@)?\w+(.\w+)*)\}/).each do
    match = $~
    key = $1
    ivar = $2
    next match if options[key] || options[key.to_sym]
    parts = key.split('.')
    receiver = ivar ? instance_variable_get(parts.shift) : self
    value = parts.inject(receiver) do |obj, message|
      obj.respond_to?(message) ? obj.send(message) : nil
    end

    next match if value.nil?
    new_key = key.sub(/\@/, '').gsub('.', '_')
    options[new_key.to_sym] = value
    "%{#{new_key}}"
  end
end