Class: TextInterpolator
- Inherits:
-
Object
- Object
- TextInterpolator
- Defined in:
- lib/text_interpolator/version.rb,
lib/text_interpolator/text_interpolator.rb
Constant Summary collapse
- VERSION =
"1.1.9"
Instance Method Summary collapse
- #clear_errors ⇒ Object
- #errors ⇒ Object
- #interpolate(object, env = {}) ⇒ Object
- #interpolate_hash(hash) ⇒ Object
- #interpolate_io(io, env = {}) ⇒ Object
- #interpolate_string(string, env = {}) ⇒ Object
Instance Method Details
#clear_errors ⇒ Object
9 10 11 |
# File 'lib/text_interpolator/text_interpolator.rb', line 9 def clear_errors errors.clear end |
#errors ⇒ Object
5 6 7 |
# File 'lib/text_interpolator/text_interpolator.rb', line 5 def errors @errors ||= [] end |
#interpolate(object, env = {}) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/text_interpolator/text_interpolator.rb', line 13 def interpolate object, env={} if object.kind_of? String interpolate_string object, env elsif object.kind_of? Hash interpolate_hash object elsif object.kind_of? IO interpolate_io object, env else object end end |
#interpolate_hash(hash) ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/text_interpolator/text_interpolator.rb', line 42 def interpolate_hash hash content = interpolate_system_variables(hash) var_table = build_variables_table(content) # one-dimensional collection of variables interpolate_variables(content, var_table) end |
#interpolate_io(io, env = {}) ⇒ Object
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/text_interpolator/text_interpolator.rb', line 50 def interpolate_io io, env={} result = '' io.each do |line| result += interpolate_string(line, env) result += '\n' unless io.eof? end result end |
#interpolate_string(string, env = {}) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/text_interpolator/text_interpolator.rb', line 25 def interpolate_string string, env={} env = symbolize_keys env value = interpolate_system_variable string var_table = build_variables_table(env) # one-dimensional collection of variables begin new_value = interpolate_variable value, var_table rescue KeyError => e new_value = string errors << e. end new_value end |