Class: I18nScopes::TranslationHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/i18n_scopes/translation_helper.rb

Overview

this class provides helper methods to determine the actual scope in your class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, options = {}) ⇒ TranslationHelper

Params:

  • path: the path from i18n_scope

  • options the options from i18n_scope



12
13
14
15
# File 'lib/i18n_scopes/translation_helper.rb', line 12

def initialize(path, options = {})
  self.path     = path
  self.options  = I18nScopes.configuration.as_options.merge(options)
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



7
8
9
# File 'lib/i18n_scopes/translation_helper.rb', line 7

def options
  @options
end

#pathObject

Returns the value of attribute path.



6
7
8
# File 'lib/i18n_scopes/translation_helper.rb', line 6

def path
  @path
end

Instance Method Details

#scope_from_path(object, options, parameter_matchings) ⇒ Object

Params:

  • object: the object which is doing a translation

  • options: additional options for this translation

  • parameter_matchings: a Hash providing arguments to custom methods



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/i18n_scopes/translation_helper.rb', line 21

def scope_from_path(object, options, parameter_matchings)
  scope = []
  custom_path  = if options.include?(:path)
    [*options.delete(:path)]
  else
    self.path
  end
  custom_path += [*options.delete(:path_extension)]     if options.include?(:path_extension)
  respondables = [object]
  respondables += [*options.delete(:respondable)]       if options.include?(:respondable)
  respondables << I18nScopes::TranslationUtils # add utils at last
  parameter_matchings[:options] = self.options.merge(parameter_matchings[:options]) # merge options
  custom_path.each do |path_name|
    if path_name.is_a?(Symbol)
      # find a respondable which responds_to path_name
      respondable = respondables.find{ |respondable| respondable.respond_to?(path_name) }
      if respondable
        # call the path_name method on the respondable
        result = value_from_method_call(respondable, respondables, path_name, parameter_matchings)
        if result.is_a?(Array)
          # add all entries of an array
          scope += result
        else
          scope << result
        end
      else
        # no object is respondable for this path_name, so just add the symbol to the scope
        scope << path_name
      end
    elsif path_name.is_a?(String)
      scope << path_name
    end
  end
  return scope
end