Sha256: af6ee570d1c41d0483b2f3040b0b62045f14aced1026e6fc895b14e0cb64325d

Contents?: true

Size: 733 Bytes

Versions: 29

Compression:

Stored size: 733 Bytes

Contents

require 'tengine/support'

require 'yaml'
require 'erb'

module Tengine::Support::YamlWithErb

  ERB_EXTNAME = ".erb".freeze

  class << self
    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
  end

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

YAML.extend(Tengine::Support::YamlWithErb)

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
tengine_support-0.3.10 lib/tengine/support/yaml_with_erb.rb
tengine_support-0.3.9 lib/tengine/support/yaml_with_erb.rb
tengine_support-0.3.8 lib/tengine/support/yaml_with_erb.rb
tengine_support-0.3.7 lib/tengine/support/yaml_with_erb.rb
tengine_support-0.3.6 lib/tengine/support/yaml_with_erb.rb
tengine_support-0.3.5 lib/tengine/support/yaml_with_erb.rb
tengine_support-0.3.4 lib/tengine/support/yaml_with_erb.rb
tengine_support-0.3.3 lib/tengine/support/yaml_with_erb.rb
tengine_support-0.3.0 lib/tengine/support/yaml_with_erb.rb