data_reader 1.2.0 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/README.md +126 -30
- data/lib/data_reader/version.rb +1 -1
- data/lib/data_reader.rb +2 -7
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 03ea2f3976b4b25953823cb6a13ef4df119c7267
|
4
|
+
data.tar.gz: 1254a6838cb0bbecd4736e7039db6541c75354d7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2fd827774f7505783ff69cad7fa5a6eb5351c821cf33b77dfa629a1bfc722b8a45442f83dfbf11de5878553b19a3f1581fe0ae010bcb25527a8c2158cf6d099f
|
7
|
+
data.tar.gz: 5062f627830900e1cafd5b5c36020e0eb1d07f0f2bf782edf59cffd5c2582e3f3af3b94e684c515d739354e162b48cffcebd607a5990e4762c060174e6a8108b
|
data/README.md
CHANGED
@@ -64,26 +64,62 @@ project_dir\
|
|
64
64
|
env\
|
65
65
|
environments.yml
|
66
66
|
|
67
|
-
|
67
|
+
test-data-reader.rb
|
68
68
|
```
|
69
69
|
|
70
|
-
All the code shown below would go in the `
|
70
|
+
All the code shown below would go in the `test-data-reader.rb` file.
|
71
71
|
|
72
72
|
With the above class in place and the above directory structure, you could do this:
|
73
73
|
|
74
74
|
```ruby
|
75
75
|
test = Testing.new
|
76
76
|
|
77
|
+
puts test.data_path
|
78
|
+
puts test.data_source
|
79
|
+
```
|
80
|
+
|
81
|
+
This would print nothing for either of those values, showing that they have no default values as just stated. You could now do this:
|
82
|
+
|
83
|
+
```ruby
|
77
84
|
test.data_path = 'data'
|
78
85
|
|
79
86
|
puts test.data_path
|
87
|
+
```
|
88
|
+
|
89
|
+
Here you are setting the `data_path` to a directory called `data`. The `puts` statement after that simply confirms that this was set. That will 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:
|
80
90
|
|
91
|
+
```ruby
|
81
92
|
test.load 'stars.yml'
|
93
|
+
```
|
94
|
+
|
95
|
+
Loading causes the data from the file to be put into the data source. This can be referenced directly by the `data_source` attribute.
|
96
|
+
|
82
97
|
|
98
|
+
```ruby
|
83
99
|
puts test.data_source
|
84
100
|
```
|
85
101
|
|
86
|
-
|
102
|
+
The `puts` call for the `data_source` will show you the contents of the YAML.
|
103
|
+
|
104
|
+
You could set the data source on the class instance if you wanted to:
|
105
|
+
|
106
|
+
```ruby
|
107
|
+
class Testing
|
108
|
+
include DataReader
|
109
|
+
|
110
|
+
def data
|
111
|
+
@data_source
|
112
|
+
end
|
113
|
+
end
|
114
|
+
```
|
115
|
+
|
116
|
+
Now you can access the data via:
|
117
|
+
|
118
|
+
```ruby
|
119
|
+
puts test.data
|
120
|
+
```
|
121
|
+
|
122
|
+
The reason this might be useful is because the data source may change but this way you refer to it via one variable.
|
87
123
|
|
88
124
|
### Data Path on Class
|
89
125
|
|
@@ -99,42 +135,33 @@ class Testing
|
|
99
135
|
end
|
100
136
|
```
|
101
137
|
|
102
|
-
Then you don't have to set the path specifically on the instance.
|
103
|
-
|
104
|
-
### Multiple Data Files
|
105
|
-
|
106
|
-
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:
|
138
|
+
Then you don't have to set the path specifically on the instance. That being said, you generally don't want to have both in place. Meaning either have the `data_path` defined on the test class or on the instance, not both. To show why this is problematic, consider this:
|
107
139
|
|
108
140
|
```ruby
|
109
|
-
|
110
|
-
|
141
|
+
class Testing
|
142
|
+
include DataReader
|
111
143
|
|
112
|
-
|
144
|
+
def data_path
|
145
|
+
'data'
|
146
|
+
end
|
147
|
+
end
|
113
148
|
|
114
|
-
|
149
|
+
test = Testing.new
|
115
150
|
|
116
|
-
|
151
|
+
test.data_path = 'combined'
|
117
152
|
|
118
|
-
|
119
|
-
configs = app.load 'config.yml'
|
120
|
-
envs = app.load 'environments.yml'
|
153
|
+
test.load 'stars.yml'
|
121
154
|
```
|
122
155
|
|
123
|
-
|
124
|
-
|
125
|
-
### Setting a Data Pata
|
126
|
-
|
127
|
-
You can, at any time, set a data path. When you do, any calls to `load` will use that data path. Consider this example:
|
156
|
+
Here I'm setting the `data_path` twice. This works because the data file `stars.yml` is in the `data`. But now try this:
|
128
157
|
|
129
158
|
```ruby
|
130
|
-
|
131
|
-
configs = app.load 'config.yml'
|
132
|
-
|
133
|
-
app.data_path = 'env'
|
134
|
-
envs = app.load 'environments.yml'
|
159
|
+
test.load 'config.yml'
|
135
160
|
```
|
136
161
|
|
137
|
-
|
162
|
+
That will fail. And that's because even though `config.yml` is in the `combined` directory, it's not in `data`. And that's what's being used here.
|
163
|
+
|
164
|
+
The upshot is that if you define a `data_path` on the class, that's what will be used.
|
138
165
|
|
139
166
|
### Default Data Path
|
140
167
|
|
@@ -175,14 +202,83 @@ test.data_path = 'config'
|
|
175
202
|
configs = test.load 'config.yml'
|
176
203
|
puts test.data_source
|
177
204
|
|
178
|
-
|
205
|
+
test.data_path = nil
|
179
206
|
|
180
|
-
|
181
|
-
puts
|
207
|
+
test.load 'stars.yml'
|
208
|
+
puts test.data_source
|
182
209
|
```
|
183
210
|
|
184
211
|
The second call to load the `stars.yml` file reverts to using the default data path.
|
185
212
|
|
213
|
+
Just to show how this can fail, consider the following:
|
214
|
+
|
215
|
+
```ruby
|
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:
|
236
|
+
|
237
|
+
```yaml
|
238
|
+
users: &users
|
239
|
+
admin:
|
240
|
+
- username: admin
|
241
|
+
password: admin
|
242
|
+
```
|
243
|
+
|
244
|
+
This would fail to load based on the `&users` part.
|
245
|
+
|
246
|
+
### Multiple Data Files
|
247
|
+
|
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:
|
249
|
+
|
250
|
+
```ruby
|
251
|
+
load 'config.yml, environments.yml, stars.yml'
|
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:
|
259
|
+
|
260
|
+
```ruby
|
261
|
+
configs = app.load 'config.yml'
|
262
|
+
envs = app.load 'environments.yml'
|
263
|
+
```
|
264
|
+
|
265
|
+
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.
|
266
|
+
|
267
|
+
### Setting a Data Pata
|
268
|
+
|
269
|
+
You can, at any time, set a data path. When you do, any calls to `load` will use that data path. Consider this example:
|
270
|
+
|
271
|
+
```ruby
|
272
|
+
app.data_path = 'config'
|
273
|
+
configs = app.load 'config.yml'
|
274
|
+
|
275
|
+
app.data_path = 'env'
|
276
|
+
envs = app.load 'environments.yml'
|
277
|
+
```
|
278
|
+
|
279
|
+
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.
|
280
|
+
|
281
|
+
|
186
282
|
### Parameterizing Data
|
187
283
|
|
188
284
|
You can set environment variables in YAML files. To do this you have to use ERB, like this:
|
data/lib/data_reader/version.rb
CHANGED
data/lib/data_reader.rb
CHANGED
@@ -10,12 +10,7 @@ module DataReader
|
|
10
10
|
|
11
11
|
def data_path
|
12
12
|
return @data_path if @data_path
|
13
|
-
|
14
|
-
if respond_to? :default_data_path
|
15
|
-
@data_path = default_data_path
|
16
|
-
return default_data_path
|
17
|
-
end
|
18
|
-
|
13
|
+
return default_data_path if self.respond_to? :default_data_path
|
19
14
|
nil
|
20
15
|
end
|
21
16
|
|
@@ -36,7 +31,7 @@ module DataReader
|
|
36
31
|
end
|
37
32
|
|
38
33
|
def include_data(file)
|
39
|
-
filename = Pathname.new(file).absolute? ? file : "#{
|
34
|
+
filename = Pathname.new(file).absolute? ? file : "#{data_path}/#{file}"
|
40
35
|
ERB.new(IO.read(filename)).result(binding) if File.exist?(filename)
|
41
36
|
end
|
42
37
|
|
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: 1.
|
4
|
+
version: 1.3.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-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -107,7 +107,7 @@ licenses:
|
|
107
107
|
- MIT
|
108
108
|
metadata: {}
|
109
109
|
post_install_message: "\n(::) (::) (::) (::) (::) (::) (::) (::) (::) (::) (::) (::)\n
|
110
|
-
\ DataReader 1.
|
110
|
+
\ DataReader 1.3.0 has been installed.\n(::) (::) (::) (::) (::) (::) (::) (::)
|
111
111
|
(::) (::) (::) (::)\n "
|
112
112
|
rdoc_options: []
|
113
113
|
require_paths:
|
@@ -123,7 +123,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
123
123
|
- !ruby/object:Gem::Version
|
124
124
|
version: '0'
|
125
125
|
requirements: []
|
126
|
-
|
126
|
+
rubyforge_project:
|
127
|
+
rubygems_version: 2.5.2
|
127
128
|
signing_key:
|
128
129
|
specification_version: 4
|
129
130
|
summary: Provides a standard method for reading YAML data files
|