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 Jul 15, 2016
1 parent fa52409 commit 39590fc
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 15 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Expand Up @@ -12,7 +12,7 @@ scheme are considered to be bugs.

### Added

* Nothing yet.
* [#337](https://github.com/intridea/hashie/pull/337): `Hashie::Mash.load` accepts a `Pathname` object #331 - [@gipcompany](https://github.com/gipcompany).

### Changed

Expand Down
7 changes: 7 additions & 0 deletions README.md
Expand Up @@ -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
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 39590fc

Please sign in to comment.