Skip to content

Commit

Permalink
Provide better matching for YAML/JSON files
Browse files Browse the repository at this point in the history
Instead of the strict matching of `.yaml`, `.yml`, or `.json`
extensions, from now and on we check if those parts are present
in any part of the filename. The following cases are recognized
as formatted: `data.YAML`, `data.yml.erb`, `data.JSON.erb` etc.
  • Loading branch information
nepalez committed Jun 9, 2019
1 parent de3cc01 commit 56ec0e3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## WIP

### Added

- Better matching of YAML/JSON files (nepalez)

The loader recognizes complex extensions like `data.yml.erb`
or `data.json.erb`, as well as `data.YAML` in upper register.

## [0.0.5] - [2018-06-04]

### Added
Expand Down
10 changes: 7 additions & 3 deletions lib/fixturama.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ def seed_fixture(path, **opts)
end

def load_fixture(path, **opts)
extname = Pathname.new(path).extname
basename = Pathname.new(path).basename.to_s

read_fixture(path, **opts).tap do |content|
return YAML.load(content) if %w[.yaml .yml].include?(extname)
return JSON.parse(content) if extname == ".json"
return YAML.load(content) if basename[YAML]
return JSON.parse(content) if basename[JSON]
end
end

Expand All @@ -49,4 +49,8 @@ def read_fixture(path, **opts)
def fixturama_stubs
@fixturama_stubs ||= Stubs.new
end

# Matchers for YAML/YML/JSON in file extension like "data.yml.erb" etc.
YAML = /.+\.ya?ml(\.|\z)/i.freeze
JSON = /.+\.json(\.|\z)/i.freeze
end

0 comments on commit 56ec0e3

Please sign in to comment.