Class: Haml::I18n::Extractor::InterpolationHelper

Inherits:
Object
  • Object
show all
Includes:
Helpers::StringHelpers
Defined in:
lib/haml-i18n-extractor/extraction/replacer/interpolation_helper.rb

Constant Summary

Constants included from Helpers::StringHelpers

Helpers::StringHelpers::LIMIT_KEY_NAME, Helpers::StringHelpers::NOT_ALLOWED_IN_KEYNAME

Instance Method Summary collapse

Methods included from Helpers::StringHelpers

#change_one_interpolation, #html_comment?, #interpolated?, #link_to?, #normalize_interpolation, #normalized_name

Constructor Details

#initialize(text_to_replace, t_name) ⇒ InterpolationHelper

takes an original_line and text_to_replace, keyname_name and gives back the result for that…



9
10
11
12
13
14
15
# File 'lib/haml-i18n-extractor/extraction/replacer/interpolation_helper.rb', line 9

def initialize(text_to_replace, t_name)
  if Extractor.debug?
    puts "<interpolationhelper>#{text_to_replace.inspect} #{t_name.inspect}</interpolationhelper>"
  end
  @t_name = t_name
  @text_to_replace = text_to_replace
end

Instance Method Details

#extract_interpolation(str) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/haml-i18n-extractor/extraction/replacer/interpolation_helper.rb', line 38

def extract_interpolation(str)
  scanner = StringScanner.new(str)
  scanner.scan_until /\#{/
  rest_scanner = StringScanner.new(scanner.rest)
  rest_scanner.scan_until(/}/)
  interpolated = rest_scanner.pre_match
end

#interpolated_varsObject



21
22
23
24
25
# File 'lib/haml-i18n-extractor/extraction/replacer/interpolation_helper.rb', line 21

def interpolated_vars
  interpolations.map{|v|
    ":#{normalized_name(v.dup)} => (#{v})"
  }.join(", ")
end

#interpolations(arr = [], str = @text_to_replace) ⇒ Object

matches multiple



28
29
30
31
32
33
34
35
36
# File 'lib/haml-i18n-extractor/extraction/replacer/interpolation_helper.rb', line 28

def interpolations(arr = [], str = @text_to_replace)
  # recurse scanner until no interpolations or string left
  return arr if str.nil? || str.empty? || !interpolated?(str)
  scanner = StringScanner.new(str)
  scanner.scan_until(/\#{.*?}/)
  so_far = scanner.pre_match + scanner.matched
  arr << extract_interpolation(so_far)
  arr = interpolations(arr, scanner.rest)
end

#keyname_with_varsObject



17
18
19
# File 'lib/haml-i18n-extractor/extraction/replacer/interpolation_helper.rb', line 17

def keyname_with_vars()
  "t('.#{@t_name}', #{interpolated_vars})"
end