Class: TextInterpolator

Inherits:
Object
  • Object
show all
Defined in:
lib/text_interpolator/version.rb,
lib/text_interpolator/text_interpolator.rb

Constant Summary collapse

VERSION =
"1.1.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



6
7
8
# File 'lib/text_interpolator/text_interpolator.rb', line 6

def errors
  @errors
end

Instance Method Details

#interpolate(object, env = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/text_interpolator/text_interpolator.rb', line 8

def interpolate object, env={}
  if object.kind_of? String
    interpolate_string object, env
  elsif object.kind_of? IO
    interpolate_io object, env
  elsif object.kind_of? Hash
    interpolate_hash object
  else
    object
  end
end

#interpolate_hash(hash) ⇒ Object



38
39
40
41
42
# File 'lib/text_interpolator/text_interpolator.rb', line 38

def interpolate_hash hash
  result, @errors = *HashProcessor.instance.process(hash)

  result
end

#interpolate_io(io, env = {}) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/text_interpolator/text_interpolator.rb', line 27

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



20
21
22
23
24
25
# File 'lib/text_interpolator/text_interpolator.rb', line 20

def interpolate_string string, env={}
  string = interpolate_env_vars string
  string = string.gsub(/\#{/, '%{') if string.index(/\#\{/)

  StringIO.new(string).read % env
end