diff --git a/CHANGELOG.md b/CHANGELOG.md index 205202ef..f961d3ef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,8 +4,10 @@ * [#319](https://github.com/intridea/hashie/pull/319): Fix a regression from 3.4.1 where `Hashie::Extensions::DeepFind` is no longer indifference-aware - [@michaelherold](https://github.com/michaelherold). * [#240](https://github.com/intridea/hashie/pull/240): Fixed nesting twice with Clash keys - [@bartoszkopinski](https://github.com/bartoszkopinski). * [#322](https://github.com/intridea/hashie/pull/322): Fixed `reverse_merge` issue with `Mash` subclasses - [@marshall-lee](https://github.com/marshall-lee). +* [#337](https://github.com/intridea/hashie/pull/337): Implemented a new feature that Hashie::Mash.load accepts a Pathname object #331 - [@gipcompany](https://github.com/gipcompany). * Your contribution here. + ## 3.4.3 (10/25/2015) * [#314](https://github.com/intridea/hashie/pull/314): Added a `StrictKeyAccess` extension that will raise an error whenever a key is accessed that does not exist in the hash - [@pboling](https://github.com/pboling). diff --git a/README.md b/README.md index cb56c686..986738f5 100644 --- a/README.md +++ b/README.md @@ -469,6 +469,13 @@ mash.development.api_key = "foo" # => <# RuntimeError can't modify frozen ...> mash.development.api_key? # => true ``` +You can also load with a Pathname object: + +```ruby +mash = Mash.load(Pathname 'settings/twitter.yml') +mash.development.api_key # => 'localhost' +``` + You can access a Mash from another class: ```ruby diff --git a/lib/hashie/extensions/parsers/yaml_erb_parser.rb b/lib/hashie/extensions/parsers/yaml_erb_parser.rb index d0b1cb0b..b3119393 100644 --- a/lib/hashie/extensions/parsers/yaml_erb_parser.rb +++ b/lib/hashie/extensions/parsers/yaml_erb_parser.rb @@ -6,7 +6,7 @@ module Parsers class YamlErbParser def initialize(file_path) @content = File.read(file_path) - @file_path = file_path + @file_path = file_path.is_a?(Pathname) ? file_path.to_s : file_path end def perform diff --git a/spec/hashie/parsers/yaml_erb_parser_spec.rb b/spec/hashie/parsers/yaml_erb_parser_spec.rb index 15482d06..72ea1f14 100644 --- a/spec/hashie/parsers/yaml_erb_parser_spec.rb +++ b/spec/hashie/parsers/yaml_erb_parser_spec.rb @@ -2,28 +2,45 @@ describe Hashie::Extensions::Parsers::YamlErbParser do describe '.perform' do - let(:config) do - <<-EOF + context 'a file' do + let(:config) do + <<-EOF --- foo: verbatim bar: <%= "erb" %> baz: "<%= __FILE__ %>" - EOF - end - let(:path) { 'template.yml' } + EOF + end + let(:path) { 'template.yml' } + + subject { described_class.new(path).perform } - subject { described_class.new(path).perform } + before do + expect(File).to receive(:read).with(path).and_return(config) + end - before do - expect(File).to receive(:read).with(path).and_return(config) + 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 - it { is_expected.to be_a(Hash) } + context 'Pathname' do + let(:tempfile) do + file = Tempfile.new(['foo', '.yml']) + file.write("---\nfoo: hello\n") + file.rewind + file + end + + subject { described_class.new(Pathname tempfile.path) } - 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 + it '"#perform" can be done in case of path is a Pathname object.' do + expect(subject.perform).to eq 'foo' => 'hello' + end end end end