diff options
author | Atsushi Ishida <gipcompany@gmail.com> | 2015-12-26 03:41:05 +0900 |
---|---|---|
committer | dblock <dblock@dblock.org> | 2016-07-15 11:56:55 -0400 |
commit | bbafaded9d5d8619d5000a3a10c17d66ee8830a0 (patch) | |
tree | cc1c9eb16e65246ff132d60a0d24b8ed12683b2b | |
parent | cfd64956294de8f1731ccaaa98349ca3e0311272 (diff) | |
download | hashie-bbafaded9d5d8619d5000a3a10c17d66ee8830a0.tar.gz |
Hashie::Mash.load accepts a Pathname object. Closes #331.
https://github.com/intridea/hashie/issues/331
-rw-r--r-- | CHANGELOG.md | 2 | ||||
-rw-r--r-- | README.md | 7 | ||||
-rw-r--r-- | lib/hashie/extensions/parsers/yaml_erb_parser.rb | 2 | ||||
-rw-r--r-- | spec/hashie/parsers/yaml_erb_parser_spec.rb | 43 |
4 files changed, 39 insertions, 15 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 42e2a38..ab55ec5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,7 +12,7 @@ scheme are considered to be bugs. ### Added -* Nothing yet. +* [#337](https://github.com/intridea/hashie/pull/337), [#331](https://github.com/intridea/hashie/issues/331): `Hashie::Mash#load` accepts a `Pathname` object - [@gipcompany](https://github.com/gipcompany). ### Changed @@ -490,6 +490,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 d0b1cb0..b311939 100644 --- a/lib/hashie/extensions/parsers/yaml_erb_parser.rb +++ b/lib/hashie/extensions/parsers/yaml_erb_parser.rb @@ -6,7 +6,7 @@ module Hashie 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 15482d0..72ea1f1 100644 --- a/spec/hashie/parsers/yaml_erb_parser_spec.rb +++ b/spec/hashie/parsers/yaml_erb_parser_spec.rb @@ -2,28 +2,45 @@ require 'spec_helper' 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 |