Class: Traject::TranslationMap
- Inherits:
-
Object
- Object
- Traject::TranslationMap
- Defined in:
- lib/traject/translation_map.rb
Overview
A TranslationMap is basically just something that has a hash-like #[] method to map from input strings to output strings:
translation_map["some_input"] #=> some_output
Input is assumed to always be string, output is either string or array of strings.
What makes it more useful than a stunted hash is it’s ability to load the hash definitions from configuration files, either pure ruby or yaml.
TranslationMap.new(“dir/some_file”)
Will look through the entire ruby $LOAD_PATH, for a translation_maps subdir that contains either some_file.rb OR some_file.yaml
-
Looks for “/translation_maps” subdir in load paths, so for instance you can have a gem that keeps translation maps in ./lib/translation_maps, and it Just Works.
-
Note you do NOT supply the “.rb” or “.yaml” suffix yourself,
it’ll use whichever it finds (allows calling code to not care which is used).
Ruby files just need to have their last line eval to a hash. They file will be run through eval, don’t do it with untrusted content (naturally)
You can also pass in a Hash for consistency to TranslationMap.new, although I don’t know why you’d want to.
Special default handling
The key “__default__” in the hash is treated specially. If set to a string, that string will be returned by the TranslationMap for any input not otherwise included. If set to the special string “__passthrough__”, then for input not mapped, the original input string will be returned.
This is most useful for YAML definition files, if you are using an actual ruby hash, you could just set the hash to do what you want using Hash#default_proc etc.
Or, when calling TranslationMap.new(), you can pass in options over-riding special key too:
TranslationMap.new("something", :default => "foo")
TranslationMap.new("something", :default => :passthrough)
Output: String or array of strings
The output can be a string or an array of strings, or nil. It should not be anything When used with the #translate_array! method, one string can be replaced by multiple values (array of strings) or removed (nil)
Caching
Lookup and loading of configuration files will be cached, for efficiency. You can reset with TranslationMap.reset_cache!
YAML example:
key: value
key2: value2 multiple words fine
key2b: "Although you can use quotes if you want: Or need."
key3:
- array
- of
- values look like this
Defined Under Namespace
Class Attribute Summary collapse
-
.cache ⇒ Object
Returns the value of attribute cache.
Instance Attribute Summary collapse
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#hash ⇒ Object
readonly
Returns the value of attribute hash.
Class Method Summary collapse
Instance Method Summary collapse
- #[](key) ⇒ Object (also: #map)
-
#initialize(defn, options = {}) ⇒ TranslationMap
constructor
A new instance of TranslationMap.
-
#translate_array(array) ⇒ Object
Run every element of an array through this translation map, return the resulting array.
- #translate_array!(array) ⇒ Object
Constructor Details
#initialize(defn, options = {}) ⇒ TranslationMap
Returns a new instance of TranslationMap.
125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/traject/translation_map.rb', line 125 def initialize(defn, = {}) if defn.kind_of? Hash @hash = defn else @hash = self.class.cache.lookup(defn) raise NotFound.new(defn) if @hash.nil? end if [:default] @default = [:default] elsif @hash.has_key? "__default__" @default = @hash.delete("__default__") end end |
Class Attribute Details
.cache ⇒ Object
Returns the value of attribute cache.
117 118 119 |
# File 'lib/traject/translation_map.rb', line 117 def cache @cache end |
Instance Attribute Details
#default ⇒ Object (readonly)
Returns the value of attribute default.
114 115 116 |
# File 'lib/traject/translation_map.rb', line 114 def default @default end |
#hash ⇒ Object (readonly)
Returns the value of attribute hash.
113 114 115 |
# File 'lib/traject/translation_map.rb', line 113 def hash @hash end |
Class Method Details
.reset_cache! ⇒ Object
118 119 120 |
# File 'lib/traject/translation_map.rb', line 118 def reset_cache! cache.reset_cache! end |
Instance Method Details
#[](key) ⇒ Object Also known as: map
140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/traject/translation_map.rb', line 140 def [](key) if self.default && (! @hash.has_key?(key)) if self.default == "__passthrough__" return key else return self.default end end @hash[key] end |
#translate_array(array) ⇒ Object
Run every element of an array through this translation map, return the resulting array. If translation map returns nil, original element will be missing from output.
If an input maps to an array, each element of the array will be flattened into the output.
If an input maps to nil, it will cause the input element to be removed entirely.
162 163 164 165 166 167 168 169 170 171 |
# File 'lib/traject/translation_map.rb', line 162 def translate_array(array) array.each_with_object([]) do |input_element, output_array| output_element = self.map(input_element) if output_element.kind_of? Array output_array.concat output_element elsif ! output_element.nil? output_array << output_element end end end |
#translate_array!(array) ⇒ Object
173 174 175 |
# File 'lib/traject/translation_map.rb', line 173 def translate_array!(array) array.replace( self.translate_array(array)) end |