Class: It::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/it/parser.rb

Overview

Parses the string and replaces all interpolations accordingly.

Constant Summary collapse

INTERPOLATION_REGEXP =
/%\{[^{}]+\}/.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string, options) ⇒ Parser

Returns a new instance of Parser.



12
13
14
15
# File 'lib/it/parser.rb', line 12

def initialize(string, options)
  @string = string
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/it/parser.rb', line 4

def options
  @options
end

#stringObject (readonly)

Returns the value of attribute string.



4
5
6
# File 'lib/it/parser.rb', line 4

def string
  @string
end

Class Method Details

.backend_options(options) ⇒ Object



8
9
10
# File 'lib/it/parser.rb', line 8

def self.backend_options(options)
  options.with_indifferent_access.slice(:default, :locale, :scope)
end

Instance Method Details

#processObject



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/it/parser.rb', line 17

def process
  handle_pluralization
  escape_string

  # For deep nesting, we repeat the process until we have no interpolations anymore
  while contains_interpolation?
    string.gsub!(INTERPOLATION_REGEXP) do |interpolation|
      Interpolation.call(interpolation, options)
    end
  end
  string.html_safe
end