Class: FoxTail::Translator
- Inherits:
-
Object
- Object
- FoxTail::Translator
- Defined in:
- lib/fox_tail/translator.rb
Instance Attribute Summary collapse
-
#default_value ⇒ Object
readonly
Returns the value of attribute default_value.
-
#method ⇒ Object
readonly
Returns the value of attribute method.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#object_name ⇒ Object
readonly
Returns the value of attribute object_name.
-
#scope ⇒ Object
readonly
Returns the value of attribute scope.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
-
#initialize(object, object_name, method, options = {}) ⇒ Translator
constructor
A new instance of Translator.
- #to_s ⇒ Object
- #translate ⇒ Object
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, = {}) @model = object.respond_to?(:to_model) ? object.to_model : nil @object_name = object_name.gsub(/\[(.*)_attributes\]\[\d+\]/, '.\1') @method = method @value = [:value] @scope = [:scope] @default_value = .fetch(:default, "") end |
Instance Attribute Details
#default_value ⇒ Object (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 |
#method ⇒ Object (readonly)
Returns the value of attribute method.
5 6 7 |
# File 'lib/fox_tail/translator.rb', line 5 def method @method end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
5 6 7 |
# File 'lib/fox_tail/translator.rb', line 5 def model @model end |
#object_name ⇒ Object (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 |
#scope ⇒ Object (readonly)
Returns the value of attribute scope.
5 6 7 |
# File 'lib/fox_tail/translator.rb', line 5 def scope @scope end |
#value ⇒ Object (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_s ⇒ Object
33 34 35 |
# File 'lib/fox_tail/translator.rb', line 33 def to_s translate end |
#translate ⇒ Object
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 |