Class: ChefApply::TextWrapper
- Inherits:
-
Object
- Object
- ChefApply::TextWrapper
- Defined in:
- lib/chef_apply/text.rb
Overview
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.
Defined Under Namespace
Classes: InvalidKey
Instance Method Summary collapse
-
#initialize(translation_tree) ⇒ TextWrapper
constructor
A new instance of TextWrapper.
- #method_missing(name, *args) ⇒ Object
Constructor Details
#initialize(translation_tree) ⇒ TextWrapper
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/chef_apply/text.rb', line 39 def initialize(translation_tree) @tree = translation_tree @tree.translation_keys.each do |k| 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 |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
55 56 57 |
# File 'lib/chef_apply/text.rb', line 55 def method_missing(name, *args) raise InvalidKey.new(@tree.instance_variable_get(:@path), name) end |