summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulien Pervillé <julien.perville@perfect-memory.com>2014-09-16 10:49:23 +0200
committerJulien Pervillé <julien.perville@perfect-memory.com>2014-09-16 10:51:54 +0200
commit104e3200c9a4a8ec4e95cbc81ed9cb07ec5cd342 (patch)
treea2529fbd2b344258d26d634fe0c3334eb6e17895
parentd224f1dff2fcab4644378d897d32236a2497dfdd (diff)
downloadhashie-104e3200c9a4a8ec4e95cbc81ed9cb07ec5cd342.tar.gz
Extend YamlErbParser to understand __FILE__ when interpolating ERB.
-rw-r--r--lib/hashie/extensions/parsers/yaml_erb_parser.rb5
-rw-r--r--spec/hashie/parsers/yaml_erb_parser_spec.rb29
2 files changed, 33 insertions, 1 deletions
diff --git a/lib/hashie/extensions/parsers/yaml_erb_parser.rb b/lib/hashie/extensions/parsers/yaml_erb_parser.rb
index 7b6a5c4..d0b1cb0 100644
--- a/lib/hashie/extensions/parsers/yaml_erb_parser.rb
+++ b/lib/hashie/extensions/parsers/yaml_erb_parser.rb
@@ -6,10 +6,13 @@ module Hashie
class YamlErbParser
def initialize(file_path)
@content = File.read(file_path)
+ @file_path = file_path
end
def perform
- YAML.load ERB.new(@content).result
+ template = ERB.new(@content)
+ template.filename = @file_path
+ YAML.load template.result
end
def self.perform(file_path)
diff --git a/spec/hashie/parsers/yaml_erb_parser_spec.rb b/spec/hashie/parsers/yaml_erb_parser_spec.rb
new file mode 100644
index 0000000..15482d0
--- /dev/null
+++ b/spec/hashie/parsers/yaml_erb_parser_spec.rb
@@ -0,0 +1,29 @@
+require 'spec_helper'
+
+describe Hashie::Extensions::Parsers::YamlErbParser do
+ describe '.perform' do
+ let(:config) do
+ <<-EOF
+---
+foo: verbatim
+bar: <%= "erb" %>
+baz: "<%= __FILE__ %>"
+ EOF
+ end
+ let(:path) { 'template.yml' }
+
+ subject { described_class.new(path).perform }
+
+ before do
+ expect(File).to receive(:read).with(path).and_return(config)
+ end
+
+ it { is_expected.to be_a(Hash) }
+
+ it 'parses YAML after interpolating ERB' do
+ expect(subject['foo']).to eq 'verbatim'
+ expect(subject['bar']).to eq 'erb'
+ expect(subject['baz']).to eq path
+ end
+ end
+end