Skip to content

Commit

Permalink
Implemented a new feature that Hashie::Mash.load accepts a Pathname o…
Browse files Browse the repository at this point in the history
…bject #331

#331
  • Loading branch information
gipcompany committed Dec 27, 2015
1 parent 8a72ed9 commit 3d3282e
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 14 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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).
Expand Down
7 changes: 7 additions & 0 deletions README.md
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/hashie/extensions/parsers/yaml_erb_parser.rb
Expand Up @@ -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
Expand Down
43 changes: 30 additions & 13 deletions spec/hashie/parsers/yaml_erb_parser_spec.rb
Expand Up @@ -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

0 comments on commit 3d3282e

Please sign in to comment.