class ChefApply::Text::TextWrapper
Our text spinner class really doesn't like handling the TranslatedString or Untranslated classes returned by the R18n library. So instead we return these TextWrapper
instances which have dynamically defined methods corresponding to the known structure of the R18n text file. Most importantly, if a user has accessed a leaf node in the code we return a regular String instead of the R18n classes.
Public Class Methods
new(translation_tree)
click to toggle source
# File lib/chef_apply/text/text_wrapper.rb, line 25 def initialize(translation_tree) @tree = translation_tree @tree.translation_keys.each do |k| # Integer keys are not translatable - they're quantity indicators in the key that # are instead sent as arguments. If we see one here, it means it was not correctly # labeled as plural with !!pl in the parent key if k.class == Integer raise MissingPlural.new(@tree.instance_variable_get(:@path), k) end k = k.to_sym define_singleton_method k do |*args| subtree = @tree.send(k, *args) if subtree.translation_keys.empty? # If there are no more possible children, just return the translated value subtree.to_s else TextWrapper.new(subtree) end end end end
Public Instance Methods
method_missing(name, *args)
click to toggle source
# File lib/chef_apply/text/text_wrapper.rb, line 48 def method_missing(name, *args) raise InvalidKey.new(@tree.instance_variable_get(:@path), name) end