data_reader 1.0.1 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.hound.yml +4 -4
- data/README.md +162 -36
- data/Rakefile +3 -3
- data/lib/data_reader/version.rb +1 -1
- data/lib/data_reader.rb +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a8f2497b2d64abf0036c3a8e7a91d276b00b6328
|
4
|
+
data.tar.gz: aa6ed30aa2fbb6d7a98521ab78bb863970038060
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bc142680fa2d7da4ebbdd28182d88ca593b1280b06b9fdccad2411d4eba1fd8708edde7663351ce780b5d17fbf89929ff01afe7faaff2d66d3ecd95b70a338ea
|
7
|
+
data.tar.gz: 6b214e2cd16ae778b0d87c67eaa826432b512eccd9234807b2c6d96c9c675d3480f00cfc52e68ad18000e1ec1b9f7d7bd80048e4759ee3589f4a7a434bb25658
|
data/.hound.yml
CHANGED
@@ -30,15 +30,15 @@ Style/SignalException:
|
|
30
30
|
Enabled: false
|
31
31
|
|
32
32
|
# This never works for validations.
|
33
|
-
|
33
|
+
Layout/AlignHash:
|
34
34
|
EnforcedLastArgumentHashStyle: ignore_implicit
|
35
35
|
|
36
36
|
# Align multi-line params with previous line.
|
37
|
-
|
37
|
+
Layout/AlignParameters:
|
38
38
|
EnforcedStyle: with_fixed_indentation
|
39
39
|
|
40
40
|
# Indent `when` clause one step from `case`.
|
41
|
-
|
41
|
+
Layout/CaseIndentation:
|
42
42
|
IndentOneStep: true
|
43
43
|
|
44
44
|
# Don't force bad var names for reduce/inject loops.
|
@@ -46,7 +46,7 @@ Style/SingleLineBlockParams:
|
|
46
46
|
Enabled: false
|
47
47
|
|
48
48
|
# For method chains, keep the dot with the method name.
|
49
|
-
|
49
|
+
Layout/DotPosition:
|
50
50
|
EnforcedStyle: leading
|
51
51
|
|
52
52
|
# Stop nesting so hard.
|
data/README.md
CHANGED
@@ -35,9 +35,31 @@ class Testing
|
|
35
35
|
end
|
36
36
|
```
|
37
37
|
|
38
|
-
|
38
|
+
This will provide DataReader functionality on any instance of the class where DataReader is mixed in.
|
39
39
|
|
40
|
-
|
40
|
+
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.
|
41
|
+
|
42
|
+
### Data Paths
|
43
|
+
|
44
|
+
Consider the following file and directory setup:
|
45
|
+
|
46
|
+
```
|
47
|
+
project_dir\
|
48
|
+
config\
|
49
|
+
config.yml
|
50
|
+
|
51
|
+
data\
|
52
|
+
stars.yml
|
53
|
+
|
54
|
+
env\
|
55
|
+
environments.yml
|
56
|
+
|
57
|
+
example-data-reader.rb
|
58
|
+
```
|
59
|
+
|
60
|
+
All the code shown below would go in the `example-data-reader` file.
|
61
|
+
|
62
|
+
With the above class in place and the above directory structure, you could do this:
|
41
63
|
|
42
64
|
```ruby
|
43
65
|
test = Testing.new
|
@@ -46,14 +68,16 @@ test.data_path = 'data'
|
|
46
68
|
|
47
69
|
puts test.data_path
|
48
70
|
|
49
|
-
test.load '
|
71
|
+
test.load 'stars.yml'
|
50
72
|
|
51
73
|
puts test.data_source
|
52
74
|
```
|
53
75
|
|
54
76
|
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.
|
55
77
|
|
56
|
-
|
78
|
+
### Data Path on Class
|
79
|
+
|
80
|
+
You could have specified the `data_path` as a method of the class instead, like this:
|
57
81
|
|
58
82
|
```ruby
|
59
83
|
class Testing
|
@@ -65,67 +89,91 @@ class Testing
|
|
65
89
|
end
|
66
90
|
```
|
67
91
|
|
68
|
-
Then you don't have to set the path specifically.
|
92
|
+
Then you don't have to set the path specifically on the instance.
|
93
|
+
|
94
|
+
### Multiple Data Files
|
69
95
|
|
70
|
-
You can load multiple YAML files. The `load` method takes a list of comma separated names of files that are in that same directory.
|
96
|
+
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:
|
71
97
|
|
72
98
|
```ruby
|
73
|
-
load '
|
99
|
+
load 'config.yml, environments.yml, stars.yml'
|
74
100
|
```
|
75
101
|
|
76
102
|
When loading in multiple files, the `data_source` will hold the contents of all the files in the list.
|
77
103
|
|
78
|
-
###
|
104
|
+
### Multiple Data Sources
|
79
105
|
|
80
|
-
You
|
106
|
+
You don't have to use the `data_source` value. For example, you could do this:
|
81
107
|
|
82
108
|
```ruby
|
83
|
-
|
109
|
+
configs = app.load 'config.yml'
|
110
|
+
envs = app.load 'environments.yml'
|
111
|
+
```
|
84
112
|
|
85
|
-
|
86
|
-
extend DataReader
|
87
|
-
end
|
113
|
+
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.
|
88
114
|
|
89
|
-
|
115
|
+
### Setting a Data Pata
|
90
116
|
|
91
|
-
|
117
|
+
You can, at any time, set a data path. When you do, any calls to `load` will use that data path. Consider this example:
|
92
118
|
|
93
|
-
|
119
|
+
```ruby
|
120
|
+
app.data_path = 'config'
|
121
|
+
configs = app.load 'config.yml'
|
94
122
|
|
95
|
-
|
123
|
+
app.data_path = 'env'
|
124
|
+
envs = app.load 'environments.yml'
|
96
125
|
```
|
97
126
|
|
98
|
-
|
127
|
+
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.
|
128
|
+
|
129
|
+
### Default Data Path
|
130
|
+
|
131
|
+
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:
|
99
132
|
|
100
133
|
```ruby
|
101
134
|
class Testing
|
102
|
-
|
135
|
+
include DataReader
|
103
136
|
|
104
|
-
def
|
137
|
+
def default_data_path
|
105
138
|
'data'
|
106
139
|
end
|
107
140
|
end
|
108
141
|
```
|
109
142
|
|
110
|
-
|
111
|
-
|
112
|
-
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:
|
143
|
+
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:
|
113
144
|
|
114
145
|
```ruby
|
115
|
-
|
116
|
-
include DataReader
|
146
|
+
test = Testing.new
|
117
147
|
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
148
|
+
test.load 'stars.yml'
|
149
|
+
puts test.data_source
|
150
|
+
|
151
|
+
test.data_path = 'config'
|
152
|
+
configs = test.load 'config.yml'
|
153
|
+
puts test.data_source
|
122
154
|
```
|
123
155
|
|
124
|
-
|
156
|
+
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:
|
157
|
+
|
158
|
+
```ruby
|
159
|
+
test = Testing.new
|
160
|
+
|
161
|
+
test.load 'stars.yml'
|
162
|
+
puts test.data_source
|
163
|
+
|
164
|
+
test.data_path = 'config'
|
165
|
+
configs = test.load 'config.yml'
|
166
|
+
puts test.data_source
|
167
|
+
|
168
|
+
app.data_path = nil
|
169
|
+
|
170
|
+
app.load 'stars.yml'
|
171
|
+
puts app.data_source
|
172
|
+
```
|
125
173
|
|
126
|
-
|
174
|
+
The second call to load the `stars.yml` file reverts to using the default data path.
|
127
175
|
|
128
|
-
### Parameterizing
|
176
|
+
### Parameterizing Data
|
129
177
|
|
130
178
|
You can set environment variables in YAML files. To do this you have to use ERB, like this:
|
131
179
|
|
@@ -133,15 +181,93 @@ You can set environment variables in YAML files. To do this you have to use ERB,
|
|
133
181
|
<%= ENV['XYZZY'] %>
|
134
182
|
```
|
135
183
|
|
136
|
-
To handle this, DataReader parses any values with ERB before it parses the YAML itself.
|
184
|
+
To handle this, DataReader parses any values with ERB before it parses the YAML itself. Here's an example YAML file:
|
137
185
|
|
138
|
-
|
186
|
+
```yaml
|
187
|
+
config:
|
188
|
+
current:
|
189
|
+
server: test
|
190
|
+
user: jeff_nyman
|
191
|
+
browser: <%= ENV['BROWSER'] %>
|
192
|
+
```
|
193
|
+
|
194
|
+
Now let's say I loaded up this file and looked at the data source:
|
195
|
+
|
196
|
+
```ruby
|
197
|
+
app.load 'config.yml'
|
198
|
+
puts app.data_source
|
199
|
+
```
|
200
|
+
|
201
|
+
Assuming the BROWSER environment variable was set, the `data_source` variable would look as follows:
|
202
|
+
|
203
|
+
```
|
204
|
+
{
|
205
|
+
"config" => {
|
206
|
+
"current" => {
|
207
|
+
"server" => "test",
|
208
|
+
"user" => "jeff_nyman",
|
209
|
+
"browser" => "chrome"
|
210
|
+
}
|
211
|
+
}
|
212
|
+
}
|
213
|
+
```
|
214
|
+
|
215
|
+
### Method Calls on Data
|
216
|
+
|
217
|
+
The support for ERB allows for custom method calls. One that is included with DataReader is `include_data`, which can be used like this:
|
139
218
|
|
140
219
|
```yaml
|
141
|
-
<%= include_data("
|
220
|
+
<%= include_data("config.yml") %>
|
221
|
+
```
|
222
|
+
|
223
|
+
Say that this line was included in line was in the YAML `environment.yml` from the above structure and you did this:
|
224
|
+
|
225
|
+
```ruby
|
226
|
+
app.data_path = 'env'
|
227
|
+
app.load 'environments.yml'
|
228
|
+
```
|
229
|
+
|
230
|
+
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:
|
231
|
+
|
232
|
+
```yaml
|
233
|
+
<%= include_data("../config/config.yml") %>
|
234
|
+
````
|
235
|
+
|
236
|
+
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`.
|
237
|
+
|
238
|
+
### Extending DataReader
|
239
|
+
|
240
|
+
You can also extend, rather than include, DataReader. This means you deal with the class rather than an instance of it. For example:
|
241
|
+
|
242
|
+
```ruby
|
243
|
+
require "data_reader"
|
244
|
+
|
245
|
+
class Testing
|
246
|
+
extend DataReader
|
247
|
+
end
|
248
|
+
|
249
|
+
Testing.data_path = 'config'
|
250
|
+
|
251
|
+
puts Testing.data_path
|
252
|
+
|
253
|
+
Testing.load 'config.yml'
|
254
|
+
|
255
|
+
puts Testing.data_source
|
256
|
+
```
|
257
|
+
|
258
|
+
Note that you can provide methods as you did in the include case, but make sure they are defined on `self`. For example:
|
259
|
+
|
260
|
+
```ruby
|
261
|
+
class Testing
|
262
|
+
extend DataReader
|
263
|
+
|
264
|
+
def self.data_path
|
265
|
+
'config'
|
266
|
+
end
|
267
|
+
end
|
142
268
|
```
|
143
269
|
|
144
|
-
If
|
270
|
+
If you were using `default_data_path`, likewise just make sure you prepend `self` to it.
|
145
271
|
|
146
272
|
## Development
|
147
273
|
|
data/Rakefile
CHANGED
@@ -13,9 +13,9 @@ namespace :spec do
|
|
13
13
|
end
|
14
14
|
|
15
15
|
RSpec::Core::RakeTask.new(all: :clean) do |config|
|
16
|
-
options = %w
|
17
|
-
options += %w
|
18
|
-
options += %w
|
16
|
+
options = %w[--color]
|
17
|
+
options += %w[--format documentation]
|
18
|
+
options += %w[--format html --out spec/reports/unit-test-report.html]
|
19
19
|
|
20
20
|
config.rspec_opts = options
|
21
21
|
end
|
data/lib/data_reader/version.rb
CHANGED
data/lib/data_reader.rb
CHANGED
@@ -22,7 +22,7 @@ module DataReader
|
|
22
22
|
files = file_list.include?(',') ? file_list.split(',') : [file_list]
|
23
23
|
files = files.collect(&:strip)
|
24
24
|
@data_source = files.inject({}) do |data, file|
|
25
|
-
data.merge!(YAML.
|
25
|
+
data.merge!(YAML.safe_load(
|
26
26
|
ERB.new(File.read("#{data_path}/#{file}")).result(binding)
|
27
27
|
))
|
28
28
|
end
|
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.0.
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jeff Nyman
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-10-05 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.0.
|
110
|
+
\ DataReader 1.0.2 has been installed.\n(::) (::) (::) (::) (::) (::) (::) (::)
|
111
111
|
(::) (::) (::) (::)\n "
|
112
112
|
rdoc_options: []
|
113
113
|
require_paths:
|
@@ -124,7 +124,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
124
124
|
version: '0'
|
125
125
|
requirements: []
|
126
126
|
rubyforge_project:
|
127
|
-
rubygems_version: 2.
|
127
|
+
rubygems_version: 2.6.13
|
128
128
|
signing_key:
|
129
129
|
specification_version: 4
|
130
130
|
summary: Provides a standard method for reading YAML data files
|