data_reader 1.3.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +33 -24
- data/Gemfile +2 -1
- data/README.md +84 -121
- data/Rakefile +1 -0
- data/examples/combined/conditions.yml +6 -0
- data/examples/combined/invalid.yml +8 -0
- data/examples/combined/stars.yml +9 -0
- data/examples/config.yml +5 -0
- data/examples/included/included.yml +2 -0
- data/examples/included/included_nested.yml +2 -0
- data/examples/included/with_includes.yml +4 -0
- data/examples/included/with_nested_includes.yml +10 -0
- data/examples/provision/invalid.yml +8 -0
- data/examples/stardates/conditions.yml +6 -0
- data/examples/warp/stars.yml +9 -0
- data/lib/data_reader.rb +9 -4
- data/lib/data_reader/version.rb +1 -1
- metadata +14 -4
- data/.travis.yml +0 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: da1a5b970137e4a0bca2ccdd9bd4749d4ebd514a
|
4
|
+
data.tar.gz: b0160d317b3318450c19c1ff88889e793d431b35
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5960e1519c33606f3d5443676e69753c4e81cc39a9c7297d72f34ba4533e10fe9504ec6c7ab70ab92352bd61c2f6edfa9ebe49dd7b6e2127f02f2cd6615eca9c
|
7
|
+
data.tar.gz: 6218d82a67cd08da8e5afa71521033779a0e4e527266f16b0c87f182a12de91da158a0a23940eb4375f8df6490e1999eafb3a7a920b6d961de5395a63e754079
|
data/.gitignore
CHANGED
@@ -1,40 +1,49 @@
|
|
1
|
-
# Ruby
|
1
|
+
# Ruby Generated
|
2
2
|
|
3
|
+
/Gemfile.lock
|
3
4
|
/.bundle/
|
4
5
|
/.yardoc
|
5
|
-
/Gemfile.lock
|
6
6
|
/_yardoc/
|
7
|
-
|
8
|
-
# Ouput-Specific
|
9
|
-
|
10
7
|
/coverage/
|
11
8
|
/doc/
|
12
9
|
/pkg/
|
13
10
|
/spec/reports/
|
11
|
+
/spec/coverage/
|
14
12
|
/tmp/
|
15
|
-
*.log
|
16
|
-
*.tmp
|
17
|
-
*.swp
|
18
|
-
*.bak
|
19
13
|
|
20
|
-
#
|
14
|
+
# Generated Reports
|
15
|
+
reports/
|
21
16
|
|
22
|
-
|
23
|
-
.settings
|
24
|
-
.project
|
25
|
-
.classpath
|
26
|
-
*.iws
|
17
|
+
# Rspec Failure Tracking
|
27
18
|
|
28
|
-
|
19
|
+
.rspec_status
|
29
20
|
|
30
|
-
|
21
|
+
# IDE Files
|
31
22
|
|
32
|
-
|
33
|
-
|
34
|
-
*.
|
35
|
-
|
23
|
+
.idea/
|
24
|
+
*.iml
|
25
|
+
*.iws
|
26
|
+
*.ipr
|
27
|
+
.vscode/
|
28
|
+
.settings/
|
29
|
+
.metadata
|
30
|
+
.classpath
|
31
|
+
.loadpath
|
32
|
+
.buildpath
|
33
|
+
.project
|
36
34
|
|
37
|
-
#
|
35
|
+
# OS Files
|
38
36
|
|
39
|
-
.
|
40
|
-
.
|
37
|
+
.DS_Store
|
38
|
+
.DS_Store?
|
39
|
+
._*
|
40
|
+
.Spotlight-V100
|
41
|
+
.Trashes
|
42
|
+
ehthumbs.db
|
43
|
+
Thumbs.db
|
44
|
+
$RECYCLE.BIN/
|
45
|
+
Desktop.ini
|
46
|
+
*.tmp
|
47
|
+
*.bak
|
48
|
+
*.swp
|
49
|
+
*~.nib
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
[![Gem Version](https://badge.fury.io/rb/data_reader.svg)](http://badge.fury.io/rb/data_reader)
|
4
4
|
[![License](http://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/jeffnyman/data_reader/blob/master/LICENSE.md)
|
5
5
|
|
6
|
-
The DataReader gem is used to provide a standard mechanism for providing a YAML data
|
6
|
+
The DataReader gem is used to provide a standard mechanism for providing a YAML data path and loading data from it. DataReader is mainly used as a support gem that can be included by other libraries that need this functionality.
|
7
7
|
|
8
8
|
## Installation
|
9
9
|
|
@@ -33,6 +33,8 @@ $ gem install data_reader
|
|
33
33
|
|
34
34
|
## Usage
|
35
35
|
|
36
|
+
The basic idea of DataReader is simple: you set a data path and DataReader will load data files from that path. But there are some nuances that it's worth discussing in this documentation.
|
37
|
+
|
36
38
|
### Including DataReader
|
37
39
|
|
38
40
|
You can include the DataReader in a class or module.
|
@@ -47,7 +49,7 @@ end
|
|
47
49
|
|
48
50
|
This will provide DataReader functionality on any instance of the class where DataReader is mixed in.
|
49
51
|
|
50
|
-
DataReader does not set defaults for anything. It provides a `data_path` variable that you can set. It also provides a `
|
52
|
+
DataReader does not set defaults for anything. It provides a `data_path` variable that you can set. It also provides a `data_contents` variable that will be populated with the result of a data file that gets loaded from any specified data path.
|
51
53
|
|
52
54
|
### Data Paths
|
53
55
|
|
@@ -55,60 +57,63 @@ Consider the following file and directory setup:
|
|
55
57
|
|
56
58
|
```
|
57
59
|
project_dir\
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
data\
|
60
|
+
combined\
|
61
|
+
invalid.yml
|
62
|
+
conditions.yml
|
62
63
|
stars.yml
|
63
64
|
|
64
|
-
|
65
|
-
|
65
|
+
provision\
|
66
|
+
invalid.yml
|
67
|
+
|
68
|
+
stardates\
|
69
|
+
conditions.yml
|
66
70
|
|
67
|
-
|
71
|
+
warp\
|
72
|
+
stars.yml
|
68
73
|
```
|
69
74
|
|
70
|
-
|
75
|
+
This is in fact the structure that is provided as part of the `examples` directory with this repository.
|
71
76
|
|
72
|
-
|
77
|
+
Within the `project_dir` you could create a file called `script.rb` to see how DataReader works. Put the above class in place in that file and then add this:
|
73
78
|
|
74
79
|
```ruby
|
75
80
|
test = Testing.new
|
76
81
|
|
77
82
|
puts test.data_path
|
78
|
-
puts test.
|
83
|
+
puts test.data_contents
|
79
84
|
```
|
80
85
|
|
81
|
-
This would print nothing for either of those values, showing that they have no default values
|
86
|
+
This would print nothing for either of those values, showing that they have no default values. You could now do this:
|
82
87
|
|
83
88
|
```ruby
|
84
|
-
test.data_path = '
|
89
|
+
test.data_path = 'warp'
|
85
90
|
|
86
91
|
puts test.data_path
|
87
92
|
```
|
88
93
|
|
89
|
-
Here you are setting the `data_path` to a directory called `
|
94
|
+
Here you are setting the `data_path` to a directory called `warp`. The `puts` statement after that simply confirms that this was set. This has now set the data path for DataReader. Here the "data path" indicates where DataReader will look for data files. Thus you could load any file that is in that directory:
|
90
95
|
|
91
96
|
```ruby
|
92
97
|
test.load 'stars.yml'
|
93
98
|
```
|
94
99
|
|
95
|
-
Loading causes the data from the file to be put into
|
100
|
+
Loading causes the data from the file to be put into a `data_contents` attribute.
|
96
101
|
|
97
102
|
|
98
103
|
```ruby
|
99
|
-
puts test.
|
104
|
+
puts test.data_contents
|
100
105
|
```
|
101
106
|
|
102
|
-
The `puts` call for the `
|
107
|
+
The `puts` call for the `data_contents` will show you the contents of the `stars.yml` file.
|
103
108
|
|
104
|
-
You could set the data
|
109
|
+
You could set the data contents on the class instance if you wanted to:
|
105
110
|
|
106
111
|
```ruby
|
107
112
|
class Testing
|
108
113
|
include DataReader
|
109
114
|
|
110
115
|
def data
|
111
|
-
@
|
116
|
+
@data_contents
|
112
117
|
end
|
113
118
|
end
|
114
119
|
```
|
@@ -119,49 +124,42 @@ Now you can access the data via:
|
|
119
124
|
puts test.data
|
120
125
|
```
|
121
126
|
|
122
|
-
The reason this might be useful is because the data
|
127
|
+
The reason this might be useful is because the data contents may change, such as if you read different files at different times, but this way you refer to the relevant contents via one variable.
|
123
128
|
|
124
|
-
|
125
|
-
|
126
|
-
You could have specified the `data_path` as a method of the class instead, like this:
|
129
|
+
Note that you can change the data path on the fly if you need to. For example:
|
127
130
|
|
128
131
|
```ruby
|
129
|
-
|
130
|
-
include DataReader
|
132
|
+
test.data_path = 'stardates'
|
131
133
|
|
132
|
-
|
133
|
-
'data'
|
134
|
-
end
|
135
|
-
end
|
134
|
+
test.load 'conditions.yml'
|
136
135
|
```
|
137
136
|
|
138
|
-
|
137
|
+
This would set the data path to the `stardates` directory and then the load file would grab the contents of the `conditions.yml` file.
|
138
|
+
|
139
|
+
### Data Path on Class
|
140
|
+
|
141
|
+
You could have specified the `data_path` as a method on the class instead, like this:
|
139
142
|
|
140
143
|
```ruby
|
141
144
|
class Testing
|
142
145
|
include DataReader
|
143
146
|
|
144
147
|
def data_path
|
145
|
-
'
|
148
|
+
'provision'
|
146
149
|
end
|
147
150
|
end
|
148
|
-
|
149
|
-
test = Testing.new
|
150
|
-
|
151
|
-
test.data_path = 'combined'
|
152
|
-
|
153
|
-
test.load 'stars.yml'
|
154
151
|
```
|
155
152
|
|
156
|
-
|
153
|
+
Then you don't have to set the path specifically as we've been doing.
|
154
|
+
|
155
|
+
Note that if you are setting the `data_path` on the class, the idea is that you want this to be the data path. So it can't be reassigned. To see that, try the above and the have the script logic as such:
|
157
156
|
|
158
157
|
```ruby
|
159
|
-
test.
|
158
|
+
test.data_path = 'provision'
|
159
|
+
test.load 'invalid.yml'
|
160
160
|
```
|
161
161
|
|
162
|
-
|
163
|
-
|
164
|
-
The upshot is that if you define a `data_path` on the class, that's what will be used.
|
162
|
+
This would lead to an error because while you have set the `data_path`, that will not override what has been set on the class. So the upshot is that if you define a `data_path` as a method on the class, that's what will be used even if you re-define the `data_path` on a specific instance of that class.
|
165
163
|
|
166
164
|
### Default Data Path
|
167
165
|
|
@@ -172,7 +170,7 @@ class Testing
|
|
172
170
|
include DataReader
|
173
171
|
|
174
172
|
def default_data_path
|
175
|
-
'
|
173
|
+
'provision'
|
176
174
|
end
|
177
175
|
end
|
178
176
|
```
|
@@ -182,57 +180,22 @@ Keep in mind that DataReader will always favor whatever it has stored in `data_p
|
|
182
180
|
```ruby
|
183
181
|
test = Testing.new
|
184
182
|
|
185
|
-
test.load '
|
186
|
-
puts test.
|
187
|
-
|
188
|
-
test.data_path = 'config'
|
189
|
-
configs = test.load 'config.yml'
|
190
|
-
puts test.data_source
|
183
|
+
test.load 'invalid.yml'
|
184
|
+
puts test.data_contents
|
191
185
|
```
|
192
186
|
|
193
|
-
|
187
|
+
This owuld work just fine. But if you were to set the data path, that overrides the default:
|
194
188
|
|
195
189
|
```ruby
|
196
|
-
test =
|
197
|
-
|
198
|
-
test.load 'stars.yml'
|
199
|
-
puts test.data_source
|
200
|
-
|
201
|
-
test.data_path = 'config'
|
202
|
-
configs = test.load 'config.yml'
|
203
|
-
puts test.data_source
|
204
|
-
|
205
|
-
test.data_path = nil
|
206
|
-
|
207
|
-
test.load 'stars.yml'
|
208
|
-
puts test.data_source
|
190
|
+
test.data_path = 'warp'
|
191
|
+
test.load 'invalid.yml'
|
209
192
|
```
|
210
193
|
|
211
|
-
|
194
|
+
Here I've set the `data_path` but I'm still trying to load `invalid.yml` (which is in the `provision` directory). But the `data_path`, since it's set, overrides that. The upshot is that a specific data path overrides the default.
|
212
195
|
|
213
|
-
|
196
|
+
If you want to be able to revert to the default, you need to set the `data_path` to nil.
|
214
197
|
|
215
|
-
|
216
|
-
class Testing
|
217
|
-
include DataReader
|
218
|
-
|
219
|
-
def default_data_path
|
220
|
-
'data'
|
221
|
-
end
|
222
|
-
end
|
223
|
-
|
224
|
-
test = Testing.new
|
225
|
-
|
226
|
-
test.data_path = 'config'
|
227
|
-
|
228
|
-
test.load 'stars.yml'
|
229
|
-
```
|
230
|
-
|
231
|
-
This would fail to load `stars.yml`. While `stars.yml` is in `data`, which is the default, you have set a specific data path here to `config`.
|
232
|
-
|
233
|
-
The upshot is that a specific data path overrides the default.
|
234
|
-
|
235
|
-
Note that named sections will currently cause a failure. So for example:
|
198
|
+
### Note that named sections will currently cause a failure. So for example:
|
236
199
|
|
237
200
|
```yaml
|
238
201
|
users: &users
|
@@ -245,39 +208,26 @@ This would fail to load based on the `&users` part.
|
|
245
208
|
|
246
209
|
### Multiple Data Files
|
247
210
|
|
248
|
-
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:
|
211
|
+
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, such as the `combined` directory shown above, you could do this:
|
249
212
|
|
250
213
|
```ruby
|
251
|
-
|
252
|
-
```
|
253
|
-
|
254
|
-
When loading in multiple files, the `data_source` will hold the contents of all the files in the list.
|
255
|
-
|
256
|
-
### Multiple Data Sources
|
257
|
-
|
258
|
-
You don't have to use the `data_source` value. For example, you could do this:
|
214
|
+
test.data_path = 'combined'
|
259
215
|
|
260
|
-
|
261
|
-
configs = app.load 'config.yml'
|
262
|
-
envs = app.load 'environments.yml'
|
216
|
+
test.load 'stars.yml, conditions.yml, invalid.yml'
|
263
217
|
```
|
264
218
|
|
265
|
-
|
219
|
+
When loading in multiple files, the `data_contents` will hold the contents of all the files in the list.
|
266
220
|
|
267
|
-
###
|
221
|
+
### Multiple Data Sources
|
268
222
|
|
269
|
-
You
|
223
|
+
You don't have to use the `data_context` value. For example, you could do this:
|
270
224
|
|
271
225
|
```ruby
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
app.data_path = 'env'
|
276
|
-
envs = app.load 'environments.yml'
|
226
|
+
stars = test.load 'stars.yml'
|
227
|
+
invalid = test.load 'invalid.yml'
|
277
228
|
```
|
278
229
|
|
279
|
-
Do note that
|
280
|
-
|
230
|
+
In this case, the appropriate data would be stored in each variable. Do note that `data_context` will always contain the last data read by the `load` method. So in the above case, `data_context` would contain the contents of `invalid.yml` even if you never intended to use that variable.
|
281
231
|
|
282
232
|
### Parameterizing Data
|
283
233
|
|
@@ -300,11 +250,11 @@ config:
|
|
300
250
|
Now let's say I loaded up this file and looked at the data source:
|
301
251
|
|
302
252
|
```ruby
|
303
|
-
|
304
|
-
puts
|
253
|
+
test.load 'config.yml'
|
254
|
+
puts test.data_contents
|
305
255
|
```
|
306
256
|
|
307
|
-
Assuming the BROWSER environment variable was set, the `
|
257
|
+
Assuming the BROWSER environment variable was set, the `data_contents` variable would look as follows:
|
308
258
|
|
309
259
|
```
|
310
260
|
{
|
@@ -320,27 +270,40 @@ Assuming the BROWSER environment variable was set, the `data_source` variable wo
|
|
320
270
|
|
321
271
|
### Method Calls on Data
|
322
272
|
|
323
|
-
The support for ERB allows for custom method calls. One that is included with DataReader is `include_data
|
273
|
+
The support for ERB allows for custom method calls. One that is included with DataReader is `include_data`. First consider the directory structure:
|
274
|
+
|
275
|
+
```
|
276
|
+
included\
|
277
|
+
included_nested.yml
|
278
|
+
included.yml
|
279
|
+
with_includes.yml
|
280
|
+
with_nested_includes.yml
|
281
|
+
```
|
282
|
+
|
283
|
+
In the `with_includes.yml` file, there is a line like this:
|
324
284
|
|
325
285
|
```yaml
|
326
|
-
<%= include_data("
|
286
|
+
<%= include_data("included.yml") %>
|
327
287
|
```
|
328
288
|
|
329
|
-
|
289
|
+
Now you can do this:
|
330
290
|
|
331
291
|
```ruby
|
332
|
-
|
333
|
-
|
292
|
+
test.data_path = 'included'
|
293
|
+
|
294
|
+
test.load 'with_includes.yml'
|
334
295
|
```
|
335
296
|
|
336
|
-
This will load up `
|
297
|
+
This will load up `with_includes.yml` and, because of the `include_data` call would attempt to load the file `included.yml`.
|
298
|
+
|
299
|
+
In this case, the value of `data_contents` would contain both data sets, first the data from `with_includes.yml` and then the data from `included.yml`.
|
300
|
+
|
301
|
+
Note, however, that DataReader will attempt to load this from the same location as `with_includes.yml`. You can absolute or relative paths as part of the call, as such:
|
337
302
|
|
338
303
|
```yaml
|
339
|
-
<%= include_data("../
|
304
|
+
<%= include_data("../warp/stars.yml") %>
|
340
305
|
````
|
341
306
|
|
342
|
-
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`.
|
343
|
-
|
344
307
|
### Extending DataReader
|
345
308
|
|
346
309
|
You can also extend, rather than include, DataReader. This means you deal with the class rather than an instance of it. For example:
|
data/Rakefile
CHANGED
data/examples/config.yml
ADDED
data/lib/data_reader.rb
CHANGED
@@ -1,21 +1,26 @@
|
|
1
1
|
require "data_reader/version"
|
2
|
+
|
2
3
|
require "pathname"
|
3
4
|
require "yaml"
|
4
5
|
require "erb"
|
5
6
|
|
6
7
|
module DataReader
|
8
|
+
# Sets the path to use when reading data files.
|
7
9
|
def data_path=(path)
|
8
10
|
@data_path = path
|
9
11
|
end
|
10
12
|
|
13
|
+
# Returns the path that will be used to read data files.
|
11
14
|
def data_path
|
12
15
|
return @data_path if @data_path
|
13
|
-
return default_data_path if
|
16
|
+
return default_data_path if respond_to? :default_data_path
|
17
|
+
|
14
18
|
nil
|
15
19
|
end
|
16
20
|
|
17
|
-
|
18
|
-
|
21
|
+
# Returns the contents that have been read in from a loaded file data file.
|
22
|
+
def data_contents
|
23
|
+
return @data_contents if @data_contents
|
19
24
|
|
20
25
|
nil
|
21
26
|
end
|
@@ -24,7 +29,7 @@ module DataReader
|
|
24
29
|
files = file_list.include?(',') ? file_list.split(',') : [file_list]
|
25
30
|
files = files.collect(&:strip)
|
26
31
|
|
27
|
-
@
|
32
|
+
@data_contents = files.inject({}) do |all_data, file|
|
28
33
|
data = include_key(::YAML.safe_load(include_data(file)))
|
29
34
|
all_data.merge!(data) if data
|
30
35
|
end
|
data/lib/data_reader/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: data_reader
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jeff Nyman
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-09-
|
11
|
+
date: 2019-09-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -91,7 +91,6 @@ files:
|
|
91
91
|
- ".hound.yml"
|
92
92
|
- ".rspec"
|
93
93
|
- ".rubocop.yml"
|
94
|
-
- ".travis.yml"
|
95
94
|
- CODE_OF_CONDUCT.md
|
96
95
|
- Gemfile
|
97
96
|
- LICENSE.md
|
@@ -100,6 +99,17 @@ files:
|
|
100
99
|
- bin/console
|
101
100
|
- bin/setup
|
102
101
|
- data_reader.gemspec
|
102
|
+
- examples/combined/conditions.yml
|
103
|
+
- examples/combined/invalid.yml
|
104
|
+
- examples/combined/stars.yml
|
105
|
+
- examples/config.yml
|
106
|
+
- examples/included/included.yml
|
107
|
+
- examples/included/included_nested.yml
|
108
|
+
- examples/included/with_includes.yml
|
109
|
+
- examples/included/with_nested_includes.yml
|
110
|
+
- examples/provision/invalid.yml
|
111
|
+
- examples/stardates/conditions.yml
|
112
|
+
- examples/warp/stars.yml
|
103
113
|
- lib/data_reader.rb
|
104
114
|
- lib/data_reader/version.rb
|
105
115
|
homepage: https://github.com/jeffnyman/data_reader
|
@@ -107,7 +117,7 @@ licenses:
|
|
107
117
|
- MIT
|
108
118
|
metadata: {}
|
109
119
|
post_install_message: "\n(::) (::) (::) (::) (::) (::) (::) (::) (::) (::) (::) (::)\n
|
110
|
-
\ DataReader
|
120
|
+
\ DataReader 2.0.0 has been installed.\n(::) (::) (::) (::) (::) (::) (::) (::)
|
111
121
|
(::) (::) (::) (::)\n "
|
112
122
|
rdoc_options: []
|
113
123
|
require_paths:
|