README.md in data_reader-1.0.1 vs README.md in data_reader-1.0.2
- old
+ new
@@ -33,116 +33,242 @@
class Testing
include DataReader
end
```
-DataReader does not set defaults for anything. It does hold a `data_path` that you set. It holds a `data_source` that is populated with the result of a file load.
+This will provide DataReader functionality on any instance of the class where DataReader is mixed in.
-With the above class in place, you could do this:
+DataReader does not set defaults for anything. It provides a `data_path` variable that you can set. It also provides a `data_source` variable that will be populated with the result of a file that gets loaded from any specified data path.
+### Data Paths
+
+Consider the following file and directory setup:
+
+```
+project_dir\
+ config\
+ config.yml
+
+ data\
+ stars.yml
+
+ env\
+ environments.yml
+
+ example-data-reader.rb
+```
+
+All the code shown below would go in the `example-data-reader` file.
+
+With the above class in place and the above directory structure, you could do this:
+
```ruby
test = Testing.new
test.data_path = 'data'
puts test.data_path
-test.load 'default.yml'
+test.load 'stars.yml'
puts test.data_source
```
Here you are setting the `data_path` to a directory called `data`. The `puts` statement after that simply confirms that this was set. You then call the `load` method for a YAML file that is in that directory. The `puts` call for the `data_source` will show you the contents of the YAML.
-You could have specified the `data_path` as part of the class instead, like this:
+### Data Path on Class
+You could have specified the `data_path` as a method of the class instead, like this:
+
```ruby
class Testing
include DataReader
def data_path
'data'
end
end
```
-Then you don't have to set the path specifically.
+Then you don't have to set the path specifically on the instance.
-You can load multiple YAML files. The `load` method takes a list of comma separated names of files that are in that same directory.
+### Multiple Data Files
+You can load multiple YAML files. The `load` method takes a list of comma separated names of files that are in that same directory. So if you were to place all the above example YAML files in one directory, you could do this:
+
```ruby
-load 'users.yml, accounts.yml, billing.yml'
+load 'config.yml, environments.yml, stars.yml'
```
When loading in multiple files, the `data_source` will hold the contents of all the files in the list.
-### Extending DataReader
+### Multiple Data Sources
-You can also extend, rather than include, DataReader. This means you deal with the class rather than an instance of it. For example:
+You don't have to use the `data_source` value. For example, you could do this:
```ruby
-require "data_reader"
+configs = app.load 'config.yml'
+envs = app.load 'environments.yml'
+```
-class Testing
- extend DataReader
-end
+In this case, the appropriate data would be stored in each variable. Do note that `data_source` will always contain the last data read by the `load` method. So in the above case, `data_source` would contain the contents of `environments.yml` even if you never intended to use that variable.
-Testing.data_path = 'data'
+### Setting a Data Pata
-puts Testing.data_path
+You can, at any time, set a data path. When you do, any calls to `load` will use that data path. Consider this example:
-Testing.load 'default.yml'
+```ruby
+app.data_path = 'config'
+configs = app.load 'config.yml'
-puts Testing.data_source
+app.data_path = 'env'
+envs = app.load 'environments.yml'
```
-Note that you can provide methods as you did in the include class, but make sure they are defined on `self`. For example:
+Do note that if you had defined a `data_path` method in your class, as shown above, that will always overridde a local instance setting as shown in the preceding code.
+### Default Data Path
+
+You may want to make sure that a default data path is always available should a data path not have been specifically set. You can do that as follows:
+
```ruby
class Testing
- extend DataReader
+ include DataReader
- def self.data_path
+ def default_data_path
'data'
end
end
```
-### Default Path
+Keep in mind that DataReader will always favor whatever it has stored in `data_path`. The `default_data_path` can be used for a fallback. So, with the default data path specifed as above, consider this:
-You can, at any time, set a data path. When you do, any calls to `load` will use that data path. However, you may want to make sure that a default data path is always available should a data path not have been specifically set. You can do that as follows:
-
```ruby
-class Testing
- include DataReader
+test = Testing.new
- def default_data_path
- 'data'
- end
-end
+test.load 'stars.yml'
+puts test.data_source
+
+test.data_path = 'config'
+configs = test.load 'config.yml'
+puts test.data_source
```
-Remember to add a `self` to the method call if you are extending DataReader.
+Here the first `load` call works by using the default path. Then a data path is set and a file loaded from that path. Once that data path has been set, the default data path is no longer going to be used. If you want to be able to revert to the default, you need to set the `data_path` to nil. For example, here's the same code as the preceding with a few additions at the end:
-Keep in mind that DataReader will always favor whatever it has stored in `data_path`. The `default_data_path` can be used for a fallback.
+```ruby
+test = Testing.new
-### Parameterizing Files
+test.load 'stars.yml'
+puts test.data_source
+test.data_path = 'config'
+configs = test.load 'config.yml'
+puts test.data_source
+
+app.data_path = nil
+
+app.load 'stars.yml'
+puts app.data_source
+```
+
+The second call to load the `stars.yml` file reverts to using the default data path.
+
+### Parameterizing Data
+
You can set environment variables in YAML files. To do this you have to use ERB, like this:
```yaml
<%= ENV['XYZZY'] %>
```
-To handle this, DataReader parses any values with ERB before it parses the YAML itself.
+To handle this, DataReader parses any values with ERB before it parses the YAML itself. Here's an example YAML file:
-The support for ERB allows for custom calls. One that is included with DataReader is `include_data`, which can be used like this:
+```yaml
+config:
+ current:
+ server: test
+ user: jeff_nyman
+ browser: <%= ENV['BROWSER'] %>
+```
+Now let's say I loaded up this file and looked at the data source:
+
+```ruby
+app.load 'config.yml'
+puts app.data_source
+```
+
+Assuming the BROWSER environment variable was set, the `data_source` variable would look as follows:
+
+```
+{
+ "config" => {
+ "current" => {
+ "server" => "test",
+ "user" => "jeff_nyman",
+ "browser" => "chrome"
+ }
+ }
+}
+```
+
+### Method Calls on Data
+
+The support for ERB allows for custom method calls. One that is included with DataReader is `include_data`, which can be used like this:
+
```yaml
-<%= include_data("my_data.yml") %>
+<%= include_data("config.yml") %>
```
-If the above line was in a file called `default.yml` and you used the `load 'default.yml'` command, then, because of the call to `include_data` you would end up with the data from both files.
+Say that this line was included in line was in the YAML `environment.yml` from the above structure and you did this:
+
+```ruby
+app.data_path = 'env'
+app.load 'environments.yml'
+```
+
+This will load up `environments.yml` and, because of the `include_data` call would attempt to load the file `config.yml`. Note, however, that DataReader will attempt to load this from the same location as `environments.yml`. You can absolute or relative paths as part of the call, as such:
+
+```yaml
+<%= include_data("../config/config.yml") %>
+````
+
+In this case, the value of `data_source` would contain both data sets, first the data from `config.yml` and then the data from `environments.yml`.
+
+### Extending DataReader
+
+You can also extend, rather than include, DataReader. This means you deal with the class rather than an instance of it. For example:
+
+```ruby
+require "data_reader"
+
+class Testing
+ extend DataReader
+end
+
+Testing.data_path = 'config'
+
+puts Testing.data_path
+
+Testing.load 'config.yml'
+
+puts Testing.data_source
+```
+
+Note that you can provide methods as you did in the include case, but make sure they are defined on `self`. For example:
+
+```ruby
+class Testing
+ extend DataReader
+
+ def self.data_path
+ 'config'
+ end
+end
+```
+
+If you were using `default_data_path`, likewise just make sure you prepend `self` to it.
## 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`.