DataReader
The DataReader gem is used to provide a standard mechanism for providing a YAML data source and loading data from it. DataReader is mainly used as a support gem that can be included by other libraries that need this functionality.
Installation
To get the latest stable release, add this line to your application's Gemfile:
gem 'data_reader'
And then include it in your bundle:
$ bundle
You can also install DataReader just as you would any other gem:
$ gem install data_reader
Usage
You can extend the DataReader to use it in another class or module.
class DataBuilder
extend DataReader
end
By extending the DataReader module you have three methods and two instance variables available to you. The three methods:
data_path=data_pathload
The instance variables:
@data_path@data_source
The @data_path instance variable will contain a reference to the location where the YAML file (or files) can be found. The @data_source instance variable will contain the contents of the YAML file after a call to load.
Multiple Data Files
The load method can be used in two ways.
First, it can take the name of a file that is in the directory specified by the @data_path instance variable.
load 'my_data.yml'
Second, it can also take a list of comma separated names of files that are in that same directory.
load 'users.yml,accounts.yml,billing.yml'
When loading in multiple files, the @data_source instance will hold the contents of all the files in the list.
Parameterizing Files
You can set environment variables in YAML files. To do this you have to use ERB, like this:
<%= ENV['XYZZY'] %>
To handle this, DataReader parses any values with ERB before it parses the YAML itself.
The support for ERB allows for custom calls. One that is included with DataReader is include_data, which can be used like this:
<%= include_data("my_data.yml") %>
Development
After checking out the repo, run bin/setup to install dependencies. Then, run rake spec:all to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment. To install this gem onto your local machine, run bundle exec rake install.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/jnyman/data_reader. The testing ecosystem of Ruby is very large and this project is intended to be a welcoming arena for collaboration on yet another testing tool. As such, contributors are very much welcome but are expected to adhere to the Contributor Covenant code of conduct.
To contribute to Decohere:
- Fork the project.
- Create your feature branch. (
git checkout -b my-new-feature) - Commit your changes. (
git commit -am 'new feature') - Push the branch. (
git push origin my-new-feature) - Create a new pull request.
Author
Credits
This code is based upon the YmlReader gem. I wanted to make a more generic version that may not be focused only on YAML files.
License
DataReader is distributed under the MIT license. See the LICENSE file for details.