accessible_data 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +40 -0
- data/.hound.yml +62 -0
- data/.rspec +3 -0
- data/.rubocop.yml +2 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +361 -0
- data/Rakefile +31 -0
- data/accessible_data.gemspec +35 -0
- data/bin/console +13 -0
- data/bin/setup +8 -0
- data/lib/accessible_data.rb +51 -0
- data/lib/accessible_data/data_accessors.rb +37 -0
- data/lib/accessible_data/data_loader.rb +31 -0
- data/lib/accessible_data/hash_methods.rb +23 -0
- data/lib/accessible_data/version.rb +3 -0
- metadata +133 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 7e6434afaa14df3e277849fc6a173dce77e26939
|
4
|
+
data.tar.gz: d368761a780f1dbae651feabe69e52a88199a775
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f47e34d1c367e5d74ef54bb25ab04a7d48ffb11626ca28afe4567e389cc741e9f742941097fa4b7207c0b61ffc60cf51372c8337729992c036fd75181a4736e7
|
7
|
+
data.tar.gz: ba66ba5da093e3fabd9c5c5aeb5c8106ec23a00811ea803dd2a059efafd3dbe45c42c2116669264c90791a053f72b207db0e19fe2094180e8d683aaeeb244334
|
data/.gitignore
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# Ruby-Specific
|
2
|
+
|
3
|
+
/.bundle/
|
4
|
+
/.yardoc
|
5
|
+
/Gemfile.lock
|
6
|
+
/_yardoc/
|
7
|
+
|
8
|
+
# Ouput-Specific
|
9
|
+
|
10
|
+
/coverage/
|
11
|
+
/doc/
|
12
|
+
/pkg/
|
13
|
+
/spec/reports/
|
14
|
+
/tmp/
|
15
|
+
*.log
|
16
|
+
*.tmp
|
17
|
+
*.swp
|
18
|
+
*.bak
|
19
|
+
|
20
|
+
# IDE-Specific
|
21
|
+
|
22
|
+
.idea
|
23
|
+
.settings
|
24
|
+
.project
|
25
|
+
.classpath
|
26
|
+
*.iws
|
27
|
+
|
28
|
+
# Windows-Specific
|
29
|
+
|
30
|
+
Thumbs.db
|
31
|
+
|
32
|
+
# Mac OS-Specific
|
33
|
+
|
34
|
+
*.DS_Store
|
35
|
+
._*
|
36
|
+
|
37
|
+
# Linux-Specific
|
38
|
+
|
39
|
+
.directory
|
40
|
+
.Trash-*
|
data/.hound.yml
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
AllCops:
|
2
|
+
Exclude:
|
3
|
+
- accessible_data.gemspec
|
4
|
+
- test/*.rb
|
5
|
+
- spec/**/*
|
6
|
+
|
7
|
+
# Removing need for frozen string literal comment.
|
8
|
+
Style/FrozenStringLiteralComment:
|
9
|
+
Enabled: false
|
10
|
+
|
11
|
+
# Removing the preference for string single quotes.
|
12
|
+
Style/StringLiterals:
|
13
|
+
Enabled: false
|
14
|
+
|
15
|
+
# Missing top-level module documentation comment.
|
16
|
+
Style/Documentation:
|
17
|
+
Enabled: false
|
18
|
+
|
19
|
+
# Prefer reduce over inject.
|
20
|
+
Style/CollectionMethods:
|
21
|
+
PreferredMethods:
|
22
|
+
reduce: 'inject'
|
23
|
+
|
24
|
+
# Use each_with_object instead of inject.
|
25
|
+
Style/EachWithObject:
|
26
|
+
Enabled: false
|
27
|
+
|
28
|
+
# Prefer fail over raise.
|
29
|
+
Style/SignalException:
|
30
|
+
Enabled: false
|
31
|
+
|
32
|
+
# This never works for validations.
|
33
|
+
Style/AlignHash:
|
34
|
+
EnforcedLastArgumentHashStyle: ignore_implicit
|
35
|
+
|
36
|
+
# Align multi-line params with previous line.
|
37
|
+
Style/AlignParameters:
|
38
|
+
EnforcedStyle: with_fixed_indentation
|
39
|
+
|
40
|
+
# Indent `when` clause one step from `case`.
|
41
|
+
Style/CaseIndentation:
|
42
|
+
IndentOneStep: true
|
43
|
+
|
44
|
+
# Don't force bad var names for reduce/inject loops.
|
45
|
+
Style/SingleLineBlockParams:
|
46
|
+
Enabled: false
|
47
|
+
|
48
|
+
# For method chains, keep the dot with the method name.
|
49
|
+
Style/DotPosition:
|
50
|
+
EnforcedStyle: leading
|
51
|
+
|
52
|
+
# Stop nesting so hard.
|
53
|
+
Metrics/BlockNesting:
|
54
|
+
Max: 2
|
55
|
+
|
56
|
+
# Encourage short methods.
|
57
|
+
Metrics/MethodLength:
|
58
|
+
Max: 10
|
59
|
+
|
60
|
+
# Encourage fewer parameters.
|
61
|
+
Metrics/ParameterLists:
|
62
|
+
Max: 4
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at jeffnyman@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Jeff Nyman
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,361 @@
|
|
1
|
+
# AccessibleData
|
2
|
+
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/accessible_data.svg)](http://badge.fury.io/rb/accessible_data)
|
4
|
+
[![License](http://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/jeffnyman/accessible_data/blob/master/LICENSE.txt)
|
5
|
+
|
6
|
+
The goal of AccessibleData is to allow for an expressive mechanism for referencing data by making that data a "first class citizen" of the structure that it applies to.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
To get the latest stable release, add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'accessible_data'
|
14
|
+
```
|
15
|
+
|
16
|
+
And then include it in your bundle:
|
17
|
+
|
18
|
+
$ bundle
|
19
|
+
|
20
|
+
You can also install AccessibleData just as you would any other gem:
|
21
|
+
|
22
|
+
$ gem install accessible_data
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
Consider a data source like this:
|
27
|
+
|
28
|
+
```yaml
|
29
|
+
numbers:
|
30
|
+
integers:
|
31
|
+
one: 1
|
32
|
+
|
33
|
+
letters:
|
34
|
+
vowels: "a, e, i, o, u"
|
35
|
+
```
|
36
|
+
|
37
|
+
This would be a YAML file. AccessibleData lets you refer to the data in this file via how it is structured by turning the keys into referenceable elements. There are a couple of ways to handle all of this.
|
38
|
+
|
39
|
+
### AccessibleData Sources
|
40
|
+
|
41
|
+
You can create an AccessibleData class like this:
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
TestData = AccessibleData.sources do |source|
|
45
|
+
source.data_load "data/sample_data.yml"
|
46
|
+
end
|
47
|
+
```
|
48
|
+
|
49
|
+
Here you assign the result of calling the `sources` action and you can pass a block where you load or merge data sources.
|
50
|
+
|
51
|
+
You can check the data you have available by either of the following approaches:
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
puts TestData.to_h
|
55
|
+
puts TestData.accessible_data
|
56
|
+
```
|
57
|
+
|
58
|
+
Given the data from the YAML file, you would end up with this:
|
59
|
+
|
60
|
+
```
|
61
|
+
{"numbers"=>{"integers"=>{"one"=>1}}, "letters"=>{"vowels"=>"a, e, i, o, u"}}
|
62
|
+
```
|
63
|
+
|
64
|
+
When you load data into your class like this, accessor methods are defined on the class and recursively through the loaded data for each key. In the example data shown, the keys are "numbers" and "letters". This means you can easily walk through your data:
|
65
|
+
|
66
|
+
```ruby
|
67
|
+
puts TestData.numbers
|
68
|
+
puts TestData.numbers.integers
|
69
|
+
puts TestData.numbers.integers.one
|
70
|
+
puts TestData.letters
|
71
|
+
puts TestData.letters.vowels
|
72
|
+
```
|
73
|
+
|
74
|
+
The result of this would be:
|
75
|
+
|
76
|
+
```
|
77
|
+
{"integers"=>{"one"=>1}}
|
78
|
+
{"one"=>1}
|
79
|
+
1
|
80
|
+
{"vowels"=>"a, e, i, o, u"}
|
81
|
+
a, e, i, o, u
|
82
|
+
```
|
83
|
+
|
84
|
+
### Data Loading
|
85
|
+
|
86
|
+
The `data_load` method loads data into your class, wiping out any previously loaded data. So you don't want to do multiple `data_load` statements. You have to provide a data source to the `data_load` call. A data source can be a string and that string should represent a file path to an existing YAML file to be loaded. An error will be throw if the file cannot be found.
|
87
|
+
|
88
|
+
```ruby
|
89
|
+
TestData.data_load('data/sample_data.yml')
|
90
|
+
```
|
91
|
+
|
92
|
+
Here you can specify any path that you want, relative to the working directory of the running script.
|
93
|
+
|
94
|
+
If you want to shorten that, you can use a symbol. This approach requires that `data` is your default directory. Here's an example:
|
95
|
+
|
96
|
+
```ruby
|
97
|
+
TestData.data_load(:sample_data)
|
98
|
+
```
|
99
|
+
|
100
|
+
The given symbol represents the name of a `.yml` file located in the `data` directory.
|
101
|
+
|
102
|
+
You can also load up a specific hash as opposed to a file:
|
103
|
+
|
104
|
+
```ruby
|
105
|
+
TestData.data_load({ :names => ['Flash', 'Green Arrow', 'Firestorm'] })
|
106
|
+
```
|
107
|
+
|
108
|
+
The data loading mechanism also accepts an optional second parameter representing the name of a specific key within the data source from which data should be loaded.
|
109
|
+
|
110
|
+
```ruby
|
111
|
+
TestData.data_load "data/sample_data.yml", "letters"
|
112
|
+
```
|
113
|
+
|
114
|
+
So here only the `letters` key and anything within it would be loaded from the `sample_data.yml` file.
|
115
|
+
|
116
|
+
### Data Referencing
|
117
|
+
|
118
|
+
As you saw above, you can use the `to_h` or `accessible_data` methods to have all of the data loaded in your class returned to you as a hash.
|
119
|
+
|
120
|
+
## Merging Data
|
121
|
+
|
122
|
+
Let's say you have another data source called `another_sample_data.yml` with this:
|
123
|
+
|
124
|
+
```yaml
|
125
|
+
numbers:
|
126
|
+
integers:
|
127
|
+
two: 2
|
128
|
+
```
|
129
|
+
|
130
|
+
You could now do something like this:
|
131
|
+
|
132
|
+
```ruby
|
133
|
+
TestData = AccessibleData.sources do |source|
|
134
|
+
source.data_load "data/sample_data.yml"
|
135
|
+
source.data_merge "data/another_sample_data.yml"
|
136
|
+
end
|
137
|
+
```
|
138
|
+
|
139
|
+
Given the data in those files, you would end up with this:
|
140
|
+
|
141
|
+
```
|
142
|
+
{"numbers"=>{"integers"=>{"one"=>1, "two"=>2}}, "letters"=>{"vowels"=>"a, e, i, o, u"}}
|
143
|
+
```
|
144
|
+
|
145
|
+
Now you could do something similar to the above with the merged data set:
|
146
|
+
|
147
|
+
```ruby
|
148
|
+
puts TestData.numbers
|
149
|
+
puts TestData.numbers.integers
|
150
|
+
puts TestData.numbers.integers.one
|
151
|
+
puts TestData.numbers.integers.two
|
152
|
+
puts TestData.letters
|
153
|
+
puts TestData.letters.vowels
|
154
|
+
```
|
155
|
+
|
156
|
+
You would end up with:
|
157
|
+
|
158
|
+
```
|
159
|
+
{"integers"=>{"one"=>1, "two"=>2}}
|
160
|
+
{"one"=>1, "two"=>2}
|
161
|
+
1
|
162
|
+
2
|
163
|
+
{"vowels"=>"a, e, i, o, u"}
|
164
|
+
a, e, i, o, u
|
165
|
+
```
|
166
|
+
|
167
|
+
So notice how the similar data got merged together (for "numbers") and you still end up with all data ("numbers" and "letters").
|
168
|
+
|
169
|
+
You can also merge specific data without referring to a file. For example:
|
170
|
+
|
171
|
+
```ruby
|
172
|
+
TestData = AccessibleData.sources do |source|
|
173
|
+
source.data_load "data/sample_data.yml"
|
174
|
+
source.data_merge "data/another_sample_data.yml"
|
175
|
+
source.data_merge test: 'xyzzy'
|
176
|
+
end
|
177
|
+
|
178
|
+
puts TestData.test
|
179
|
+
```
|
180
|
+
|
181
|
+
Here you can reference the previous data as before but now you are also merging in a specific set of data and that last statement would show you:
|
182
|
+
|
183
|
+
```
|
184
|
+
xyzzy
|
185
|
+
```
|
186
|
+
|
187
|
+
So what you are seeing here is that the `data_merge` method is somewhat equivalent to `data_load` with the exception that the data source is _merged_. This means entries with duplicate keys are overwritten with previously loaded data. Also, you can pass a namespace to merge just as you did during loading:
|
188
|
+
|
189
|
+
```ruby
|
190
|
+
TestData.data_merge "data/sample_data.yml", "letters"
|
191
|
+
````
|
192
|
+
|
193
|
+
### AccessibleData Mixin
|
194
|
+
|
195
|
+
You can potentially make the above approach a bit easier to by having your class include `AccessibleData`, rather than calling `sources` directly. For example:
|
196
|
+
|
197
|
+
```ruby
|
198
|
+
class TestData
|
199
|
+
include AccessibleData
|
200
|
+
|
201
|
+
data_load "data/sample_data.yml"
|
202
|
+
data_merge "data/another_sample_data.yml"
|
203
|
+
data_merge test: 'xyzzy'
|
204
|
+
end
|
205
|
+
|
206
|
+
puts TestData.accessible_data
|
207
|
+
```
|
208
|
+
|
209
|
+
A key thing to note here is that the data is a first class citizen of the class, not of instances of the class. You can reference data from an object, however, by doing something like this:
|
210
|
+
|
211
|
+
```ruby
|
212
|
+
class TestData
|
213
|
+
include AccessibleData
|
214
|
+
|
215
|
+
data_load "data/sample_data.yml"
|
216
|
+
data_merge "data/another_sample_data.yml"
|
217
|
+
data_merge test: 'xyzzy'
|
218
|
+
|
219
|
+
def action
|
220
|
+
puts self.class.accessible_data
|
221
|
+
end
|
222
|
+
end
|
223
|
+
|
224
|
+
data = TestData.new
|
225
|
+
data.action
|
226
|
+
```
|
227
|
+
|
228
|
+
## Getting and Setting Data
|
229
|
+
|
230
|
+
Consider the following:
|
231
|
+
|
232
|
+
```ruby
|
233
|
+
class TestData
|
234
|
+
include AccessibleData
|
235
|
+
end
|
236
|
+
|
237
|
+
TestData.data_load({
|
238
|
+
:superheroes => {
|
239
|
+
:green_lantern => {
|
240
|
+
:secret_identity => [
|
241
|
+
{ name: 'Hal Jordan' },
|
242
|
+
{ name: 'John Stewart '},
|
243
|
+
{ name: 'Guy Gardner' }
|
244
|
+
]
|
245
|
+
}
|
246
|
+
}
|
247
|
+
})
|
248
|
+
```
|
249
|
+
|
250
|
+
You can reference the data as such:
|
251
|
+
|
252
|
+
```ruby
|
253
|
+
puts TestData.superheroes.green_lantern.secret_identity[0].name
|
254
|
+
puts TestData.superheroes[:green_lantern].secret_identity[0].name
|
255
|
+
puts TestData.superheroes.green_lantern
|
256
|
+
```
|
257
|
+
|
258
|
+
That will get you:
|
259
|
+
|
260
|
+
```
|
261
|
+
Hal Jordan
|
262
|
+
Hal Jordan
|
263
|
+
{:secret_identity=>[{:name=>"Hal Jordan"}, {:name=>"Kyle Rayner"}, {:name=>"Guy Gardner"}]}
|
264
|
+
```
|
265
|
+
|
266
|
+
You can also set the data by referencing an index directly:
|
267
|
+
|
268
|
+
```ruby
|
269
|
+
TestData[:superheroes].green_lantern.secret_identity[1].name = 'Kyle Rayner'
|
270
|
+
```
|
271
|
+
|
272
|
+
What this is showing you is the reference via a `[]` method. This gets data from your class and will return `nil` if the key does not exist. That can make this method useful for assigning default values in the absence of a key, as such:
|
273
|
+
|
274
|
+
```ruby
|
275
|
+
villain = TestData[:supervillain] || 'Parallax'
|
276
|
+
```
|
277
|
+
|
278
|
+
You can also use a `[]=` method.
|
279
|
+
|
280
|
+
```ruby
|
281
|
+
TestData[:supervillain] = 'Parallax'
|
282
|
+
puts TestData.supervillain
|
283
|
+
```
|
284
|
+
|
285
|
+
### Caveat on Accessors
|
286
|
+
|
287
|
+
Consider the following:
|
288
|
+
|
289
|
+
```ruby
|
290
|
+
class TestData
|
291
|
+
include AccessibleData
|
292
|
+
end
|
293
|
+
|
294
|
+
TestData.data_load({})
|
295
|
+
|
296
|
+
TestData[:hal_jordan] = "Green Lantern" # []=
|
297
|
+
puts TestData[:hal_jordan] # []
|
298
|
+
puts TestData.hal_jordan
|
299
|
+
```
|
300
|
+
|
301
|
+
You can see how the key is referenced. In both cases you will get a value of "Green Lantern" returned. The comments show you which methods on AccessibleData are being called. Notice that the "hal_jordan" key is being created on the fly. Now consider this:
|
302
|
+
|
303
|
+
```ruby
|
304
|
+
class TestData
|
305
|
+
include AccessibleData
|
306
|
+
end
|
307
|
+
|
308
|
+
TestData.data_load({ hal_jordan: { :superhero => 'Green Lantern' } })
|
309
|
+
puts TestData.hal_jordan
|
310
|
+
puts TestData.hal_jordan.superhero
|
311
|
+
|
312
|
+
TestData.hal_jordan[:evil] = 'Parallax'
|
313
|
+
puts TestData.hal_jordan[:evil] # WORKS
|
314
|
+
puts TestData.hal_jordan.evil # DOES NOT WORK
|
315
|
+
```
|
316
|
+
|
317
|
+
Notice the comments here. The reason the last statement does not work is because the when the `[:evil]` key and value is established above, this does not call `[]=` on AccessibleData but rather the standard mechanism for insertion into a hash provided by Ruby. This means the accessors that AccessibleData provides are not created. So this is where you would want to use `data_merge` to bring in data.
|
318
|
+
|
319
|
+
## ERB Processing
|
320
|
+
|
321
|
+
AccessibleData will process files with Embedded Ruby. For example, you could have a data source like this:
|
322
|
+
|
323
|
+
```yaml
|
324
|
+
numbers:
|
325
|
+
integers:
|
326
|
+
one: 1
|
327
|
+
four: <%= 2 + 2 %>
|
328
|
+
```
|
329
|
+
|
330
|
+
This would be referenced by AccessibleData as such:
|
331
|
+
|
332
|
+
```
|
333
|
+
{"numbers"=>{"integers"=>{"one"=>1, "four"=>4}}}
|
334
|
+
```
|
335
|
+
|
336
|
+
Notice how the keyword "four" shows the calculated value.
|
337
|
+
|
338
|
+
## Development
|
339
|
+
|
340
|
+
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`.
|
341
|
+
|
342
|
+
## Contributing
|
343
|
+
|
344
|
+
Bug reports and pull requests are welcome on GitHub at [https://github.com/jeffnyman/accessible_data](https://github.com/jeffnyman/accessible_data). 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](http://contributor-covenant.org) code of conduct.
|
345
|
+
|
346
|
+
To contribute to AccessibleData:
|
347
|
+
|
348
|
+
1. [Fork the project](http://gun.io/blog/how-to-github-fork-branch-and-pull-request/).
|
349
|
+
2. Create your feature branch. (`git checkout -b my-new-feature`)
|
350
|
+
3. Commit your changes. (`git commit -am 'new feature'`)
|
351
|
+
4. Push the branch. (`git push origin my-new-feature`)
|
352
|
+
5. Create a new [pull request](https://help.github.com/articles/using-pull-requests).
|
353
|
+
|
354
|
+
## Author
|
355
|
+
|
356
|
+
* [Jeff Nyman](http://testerstories.com)
|
357
|
+
|
358
|
+
## License
|
359
|
+
|
360
|
+
AccessibleData is distributed under the [MIT](http://www.opensource.org/licenses/MIT) license.
|
361
|
+
See the [LICENSE](https://github.com/jeffnyman/accessible_data/blob/master/LICENSE.txt) file for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
require "bundler/gem_tasks"
|
3
|
+
require "rdoc/task"
|
4
|
+
require "rubocop/rake_task"
|
5
|
+
require "rspec/core/rake_task"
|
6
|
+
|
7
|
+
RuboCop::RakeTask.new
|
8
|
+
|
9
|
+
namespace :spec do
|
10
|
+
desc 'Clean all generated reports'
|
11
|
+
task :clean do
|
12
|
+
system('rm -rf spec/reports')
|
13
|
+
end
|
14
|
+
|
15
|
+
RSpec::Core::RakeTask.new(all: :clean) do |config|
|
16
|
+
options = %w(--color)
|
17
|
+
options += %w(--format documentation)
|
18
|
+
options += %w(--format html --out spec/reports/unit-test-report.html)
|
19
|
+
|
20
|
+
config.rspec_opts = options
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
Rake::RDocTask.new do |rdoc|
|
25
|
+
rdoc.rdoc_dir = 'doc'
|
26
|
+
rdoc.main = 'README.md'
|
27
|
+
rdoc.title = "AccessibleData #{AccessibleData::VERSION}"
|
28
|
+
rdoc.rdoc_files.include('README*', 'lib/**/*.rb')
|
29
|
+
end
|
30
|
+
|
31
|
+
task default: ['spec:all', :rubocop]
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'accessible_data/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "accessible_data"
|
8
|
+
spec.version = AccessibleData::VERSION
|
9
|
+
spec.authors = ["Jeff Nyman"]
|
10
|
+
spec.email = ["jeffnyman@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Provides accessibility mechanism for data.}
|
13
|
+
spec.description = %q{Provides accessibility mechanism for data.}
|
14
|
+
spec.homepage = ""
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.13"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
27
|
+
spec.add_development_dependency "rubocop"
|
28
|
+
spec.add_development_dependency "pry"
|
29
|
+
|
30
|
+
spec.post_install_message = %{
|
31
|
+
(::) (::) (::) (::) (::) (::) (::) (::) (::) (::) (::) (::)
|
32
|
+
AccessibleData #{AccessibleData::VERSION} has been installed.
|
33
|
+
(::) (::) (::) (::) (::) (::) (::) (::) (::) (::) (::) (::)
|
34
|
+
}
|
35
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "accessible_data"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
require "pry"
|
10
|
+
Pry.start
|
11
|
+
|
12
|
+
# require "irb"
|
13
|
+
# IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
require "accessible_data/version"
|
2
|
+
require "accessible_data/data_loader"
|
3
|
+
require "accessible_data/hash_methods"
|
4
|
+
require "accessible_data/data_accessors"
|
5
|
+
|
6
|
+
module AccessibleData
|
7
|
+
def self.included(caller)
|
8
|
+
caller.extend ClassMethods
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.sources
|
12
|
+
klass = Class.new { extend ClassMethods }
|
13
|
+
yield klass if block_given?
|
14
|
+
klass
|
15
|
+
end
|
16
|
+
|
17
|
+
module ClassMethods
|
18
|
+
def data_load(data_source, namespace = nil)
|
19
|
+
to_h.clear
|
20
|
+
data_merge(data_source, namespace)
|
21
|
+
end
|
22
|
+
|
23
|
+
def data_merge(data_source, namespace = nil)
|
24
|
+
source_data = DataLoader.load_source(data_source)
|
25
|
+
new_data = namespace ? source_data.fetch(namespace) : source_data
|
26
|
+
|
27
|
+
@data = HashMethods.deep_merge(to_h, new_data)
|
28
|
+
|
29
|
+
DataAccessors.accessor_for_obj(self)
|
30
|
+
DataAccessors.accessor_for_data(to_h)
|
31
|
+
|
32
|
+
to_h
|
33
|
+
end
|
34
|
+
|
35
|
+
def to_h
|
36
|
+
@data ||= {}
|
37
|
+
end
|
38
|
+
|
39
|
+
def [](key)
|
40
|
+
to_h[key]
|
41
|
+
end
|
42
|
+
|
43
|
+
def []=(key, value)
|
44
|
+
DataAccessors.define_accessor(to_h, key)
|
45
|
+
DataAccessors.define_accessor(self, key)
|
46
|
+
to_h[key] = DataAccessors.accessor_for_data(value)
|
47
|
+
end
|
48
|
+
|
49
|
+
alias accessible_data to_h
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module AccessibleData
|
2
|
+
module DataAccessors
|
3
|
+
module_function
|
4
|
+
|
5
|
+
def accessor_for_obj(obj)
|
6
|
+
obj.to_h.keys.each do |key|
|
7
|
+
define_accessor(obj, key)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def accessor_for_data(data)
|
12
|
+
HashMethods.each_hash(data) do |hash|
|
13
|
+
hash.each do |key, value|
|
14
|
+
define_accessor(hash, key)
|
15
|
+
accessor_for_data(value)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def define_accessor(obj, key)
|
21
|
+
define_getter(obj, key)
|
22
|
+
define_setter(obj, key)
|
23
|
+
end
|
24
|
+
|
25
|
+
def define_getter(obj, key)
|
26
|
+
obj.define_singleton_method(key) do
|
27
|
+
obj.to_h.fetch(key)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def define_setter(obj, key)
|
32
|
+
obj.define_singleton_method("#{key}=") do |value|
|
33
|
+
obj.to_h[key] = AccessibleData::DataAccessors.accessor_for_data(value)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require "erb"
|
2
|
+
require "yaml"
|
3
|
+
|
4
|
+
module AccessibleData
|
5
|
+
module DataLoader
|
6
|
+
module_function
|
7
|
+
|
8
|
+
def process_erb(text)
|
9
|
+
ERB.new(text).result
|
10
|
+
end
|
11
|
+
|
12
|
+
def load_from_file(file)
|
13
|
+
contents = File.read(file)
|
14
|
+
evaluated_contents = process_erb(contents)
|
15
|
+
YAML.load(evaluated_contents) || {}
|
16
|
+
end
|
17
|
+
|
18
|
+
def load_source(data_source)
|
19
|
+
case data_source
|
20
|
+
when Hash
|
21
|
+
data_source
|
22
|
+
when String
|
23
|
+
load_from_file(data_source)
|
24
|
+
when Symbol
|
25
|
+
load_from_file("data/#{data_source}.yml")
|
26
|
+
else
|
27
|
+
raise("Invalid data source provided: #{data_source}")
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module AccessibleData
|
2
|
+
module HashMethods
|
3
|
+
module_function
|
4
|
+
|
5
|
+
def deep_merge(original_data, new_data)
|
6
|
+
merger = proc do |_key, v1, v2|
|
7
|
+
v1.is_a?(Hash) && v2.is_a?(Hash) ? v1.merge(v2, &merger) : v2
|
8
|
+
end
|
9
|
+
|
10
|
+
original_data.merge(new_data, &merger)
|
11
|
+
end
|
12
|
+
|
13
|
+
def each_hash(data, &block)
|
14
|
+
case data
|
15
|
+
when Hash
|
16
|
+
yield data
|
17
|
+
when Array
|
18
|
+
data.each { |element| each_hash(element, &block) }
|
19
|
+
end
|
20
|
+
data
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,133 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: accessible_data
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jeff Nyman
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-12-09 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.13'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.13'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rubocop
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pry
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: Provides accessibility mechanism for data.
|
84
|
+
email:
|
85
|
+
- jeffnyman@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".hound.yml"
|
92
|
+
- ".rspec"
|
93
|
+
- ".rubocop.yml"
|
94
|
+
- CODE_OF_CONDUCT.md
|
95
|
+
- Gemfile
|
96
|
+
- LICENSE.txt
|
97
|
+
- README.md
|
98
|
+
- Rakefile
|
99
|
+
- accessible_data.gemspec
|
100
|
+
- bin/console
|
101
|
+
- bin/setup
|
102
|
+
- lib/accessible_data.rb
|
103
|
+
- lib/accessible_data/data_accessors.rb
|
104
|
+
- lib/accessible_data/data_loader.rb
|
105
|
+
- lib/accessible_data/hash_methods.rb
|
106
|
+
- lib/accessible_data/version.rb
|
107
|
+
homepage: ''
|
108
|
+
licenses:
|
109
|
+
- MIT
|
110
|
+
metadata: {}
|
111
|
+
post_install_message: "\n(::) (::) (::) (::) (::) (::) (::) (::) (::) (::) (::) (::)\n
|
112
|
+
\ AccessibleData 1.0.0 has been installed.\n(::) (::) (::) (::) (::) (::) (::) (::)
|
113
|
+
(::) (::) (::) (::)\n "
|
114
|
+
rdoc_options: []
|
115
|
+
require_paths:
|
116
|
+
- lib
|
117
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
118
|
+
requirements:
|
119
|
+
- - ">="
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
version: '0'
|
122
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
123
|
+
requirements:
|
124
|
+
- - ">="
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
version: '0'
|
127
|
+
requirements: []
|
128
|
+
rubyforge_project:
|
129
|
+
rubygems_version: 2.5.1
|
130
|
+
signing_key:
|
131
|
+
specification_version: 4
|
132
|
+
summary: Provides accessibility mechanism for data.
|
133
|
+
test_files: []
|