Class: FoxTail::Translator

Inherits:
Object
  • Object
show all
Defined in:
lib/fox_tail/translator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, object_name, method, options = {}) ⇒ Translator

Returns a new instance of Translator.



7
8
9
10
11
12
13
14
# File 'lib/fox_tail/translator.rb', line 7

def initialize(object, object_name, method, options = {})
  @model = object.respond_to?(:to_model) ? object.to_model : nil
  @object_name = object_name.gsub(/\[(.*)_attributes\]\[\d+\]/, '.\1')
  @method = method
  @value = options[:value]
  @scope = options[:scope]
  @default_value = options.fetch(:default, "")
end

Instance Attribute Details

#default_valueObject (readonly)

Returns the value of attribute default_value.



5
6
7
# File 'lib/fox_tail/translator.rb', line 5

def default_value
  @default_value
end

#methodObject (readonly)

Returns the value of attribute method.



5
6
7
# File 'lib/fox_tail/translator.rb', line 5

def method
  @method
end

#modelObject (readonly)

Returns the value of attribute model.



5
6
7
# File 'lib/fox_tail/translator.rb', line 5

def model
  @model
end

#object_nameObject (readonly)

Returns the value of attribute object_name.



5
6
7
# File 'lib/fox_tail/translator.rb', line 5

def object_name
  @object_name
end

#scopeObject (readonly)

Returns the value of attribute scope.



5
6
7
# File 'lib/fox_tail/translator.rb', line 5

def scope
  @scope
end

#valueObject (readonly)

Returns the value of attribute value.



5
6
7
# File 'lib/fox_tail/translator.rb', line 5

def value
  @value
end

Instance Method Details

#to_sObject



33
34
35
# File 'lib/fox_tail/translator.rb', line 33

def to_s
  translate
end

#translateObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/fox_tail/translator.rb', line 16

def translate
  return default_value if method.blank?

  defaults = []

  object_scopes.each do |object_scope|
    method_scopes.each do |method_scope|
      defaults << [object_scope, method_scope].compact_blank.join(".").to_sym
    end
  end

  defaults << method.to_sym
  defaults << default_value

  I18n.t(defaults.shift, default: defaults, scope: scope).presence
end