Module: Tengine::Support::YamlWithErb

Defined in:
lib/tengine/support/yaml_with_erb.rb

Constant Summary collapse

ERB_EXTNAME =
".erb".freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(klass) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/tengine/support/yaml_with_erb.rb', line 11

def extended(klass)
  return if klass.respond_to?(:load_file_without_erb)
  klass.instance_eval do
    alias :load_file_without_erb :load_file

    def load_file(filepath)
      if File.extname(filepath) == ERB_EXTNAME
        load_file_with_erb(filepath)
      else
        load_file_without_erb(filepath)
      end
    end

  end
end

Instance Method Details

#load_file_with_erb(filepath) ⇒ Object



28
29
30
31
32
33
# File 'lib/tengine/support/yaml_with_erb.rb', line 28

def load_file_with_erb(filepath)
  erb = ERB.new(IO.read(filepath))
  erb.filename = filepath
  text = erb.result
  YAML.load(text)
end