diff options
author | Daniel Doubrovkine (dB.) @dblockdotorg <dblock@dblock.org> | 2014-09-16 09:42:29 -0400 |
---|---|---|
committer | Daniel Doubrovkine (dB.) @dblockdotorg <dblock@dblock.org> | 2014-09-16 09:42:29 -0400 |
commit | fed4b5573b19c0189bab5f0f9c744021e78277d0 (patch) | |
tree | 40dbae3518e6358585e24dd73969af428100f172 | |
parent | d224f1dff2fcab4644378d897d32236a2497dfdd (diff) | |
parent | 4ceb35b632be64dbef13a5623208591bb1dd3a26 (diff) | |
download | hashie-fed4b5573b19c0189bab5f0f9c744021e78277d0.tar.gz |
Merge pull request #228 from jperville/yaml_erb_parser_with_filename
Extend YamlErbParser to understand __FILE__ when interpolating ERB.
-rw-r--r-- | CHANGELOG.md | 1 | ||||
-rw-r--r-- | lib/hashie/extensions/parsers/yaml_erb_parser.rb | 5 | ||||
-rw-r--r-- | spec/hashie/parsers/yaml_erb_parser_spec.rb | 29 |
3 files changed, 34 insertions, 1 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index ca914db..4a55538 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ## Next Release * Your contribution here. +* [#228](https://github.com/intridea/hashie/pull/228): Made Hashie::Extensions::Parsers::YamlErbParser pass template filename to ERB - [@jperville](https://github.com/jperville). * [#224](https://github.com/intridea/hashie/pull/224): Merging Hashie::Mash now correctly only calls the block on duplicate values - [@amysutedja](https://github.com/amysutedja). * [#221](https://github.com/intridea/hashie/pull/221): Reduce amount of allocated objects on calls with suffixes in Hashie::Mash - [@kubum](https://github.com/kubum). 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 |