data_accessible 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6c76d3e327287dd460600973b1af9d0eb93192e1
4
- data.tar.gz: fc172d0a8ba01d3c089b95933980fa31899e47a6
3
+ metadata.gz: 99ab12459828452824677b744a9f028ea51c548b
4
+ data.tar.gz: 93783eb27c03791add656c63023b1c2604a2475d
5
5
  SHA512:
6
- metadata.gz: c35c887b5235d752953ff249b56e0b6867f3bd8a449ee24af88f8c704f655361be2380ab8d41a24c74bbe5436c58ad87a863ed806a222a34579bb02c5e5f9f0d
7
- data.tar.gz: f4f5573b5ab372b91511d2c30402cd31b6a02177deb36701076dfdc5a9bb0e117be8e614a7136a43fb188a6a0e73eed07e98f0abb0e5dfa138eec8417c21e06d
6
+ metadata.gz: d7342dee996ad53ef5caa0cb048a1ab7c7d4ff3eacd5ccdee13c304536e8652fcb7b3b9403914952901aaa94e84d4c2a3452682a9fd412d97d791916580cfba0
7
+ data.tar.gz: 8293503539f468b2bb7791bfe514bf1b17160f2672c95e26ed71883562fff04cb0d15f98ea4993eacf18a9acb34db0d52b8477c598c534ebff729ac20a0d1afe
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,67 @@
1
+ AllCops:
2
+ Exclude:
3
+ - data_accessible.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
63
+
64
+ # Need to use load, rather than safe_load to support
65
+ # symbol parsing.
66
+ Security/YAMLLoad:
67
+ Enabled: false
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,2 @@
1
+ inherit_from:
2
+ - .hound.yml
@@ -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
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in data_accessible.gemspec
4
+ gemspec
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,363 @@
1
+ # DataAccessible
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/data_accessible.svg)](http://badge.fury.io/rb/data_accessible)
4
+ [![License](http://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/jeffnyman/data_accessible/blob/master/LICENSE.txt)
5
+
6
+ The goal of DataAccessible 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 'data_accessible'
14
+ ```
15
+
16
+ And then include it in your bundle:
17
+
18
+ $ bundle
19
+
20
+ You can also install DataAccessible just as you would any other gem:
21
+
22
+ $ gem install data_accessible
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. DataAccessible 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
+ ### DataAccessible Sources
40
+
41
+ You can create an DataAccessible class like this:
42
+
43
+ ```ruby
44
+ TestData = DataAccessible.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.data_accessible
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 `data_accessible` methods to have all of the data loaded in your class returned to you as a hash.
119
+
120
+ If you feel it reads better, you can also use `accessible_data` as the method call.
121
+
122
+ ## Merging Data
123
+
124
+ Let's say you have another data source called `another_sample_data.yml` with this:
125
+
126
+ ```yaml
127
+ numbers:
128
+ integers:
129
+ two: 2
130
+ ```
131
+
132
+ You could now do something like this:
133
+
134
+ ```ruby
135
+ TestData = DataAccessible.sources do |source|
136
+ source.data_load "data/sample_data.yml"
137
+ source.data_merge "data/another_sample_data.yml"
138
+ end
139
+ ```
140
+
141
+ Given the data in those files, you would end up with this:
142
+
143
+ ```
144
+ {"numbers"=>{"integers"=>{"one"=>1, "two"=>2}}, "letters"=>{"vowels"=>"a, e, i, o, u"}}
145
+ ```
146
+
147
+ Now you could do something similar to the above with the merged data set:
148
+
149
+ ```ruby
150
+ puts TestData.numbers
151
+ puts TestData.numbers.integers
152
+ puts TestData.numbers.integers.one
153
+ puts TestData.numbers.integers.two
154
+ puts TestData.letters
155
+ puts TestData.letters.vowels
156
+ ```
157
+
158
+ You would end up with:
159
+
160
+ ```
161
+ {"integers"=>{"one"=>1, "two"=>2}}
162
+ {"one"=>1, "two"=>2}
163
+ 1
164
+ 2
165
+ {"vowels"=>"a, e, i, o, u"}
166
+ a, e, i, o, u
167
+ ```
168
+
169
+ So notice how the similar data got merged together (for "numbers") and you still end up with all data ("numbers" and "letters").
170
+
171
+ You can also merge specific data without referring to a file. For example:
172
+
173
+ ```ruby
174
+ TestData = DataAccessible.sources do |source|
175
+ source.data_load "data/sample_data.yml"
176
+ source.data_merge "data/another_sample_data.yml"
177
+ source.data_merge test: 'xyzzy'
178
+ end
179
+
180
+ puts TestData.test
181
+ ```
182
+
183
+ 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:
184
+
185
+ ```
186
+ xyzzy
187
+ ```
188
+
189
+ 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:
190
+
191
+ ```ruby
192
+ TestData.data_merge "data/sample_data.yml", "letters"
193
+ ````
194
+
195
+ ### DataAccessible Mixin
196
+
197
+ You can potentially make the above approach a bit easier to by having your class include `DataAccessible`, rather than calling `sources` directly. For example:
198
+
199
+ ```ruby
200
+ class TestData
201
+ include DataAccessible
202
+
203
+ data_load "data/sample_data.yml"
204
+ data_merge "data/another_sample_data.yml"
205
+ data_merge test: 'xyzzy'
206
+ end
207
+
208
+ puts TestData.accessible_data
209
+ ```
210
+
211
+ 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:
212
+
213
+ ```ruby
214
+ class TestData
215
+ include DataAccessible
216
+
217
+ data_load "data/sample_data.yml"
218
+ data_merge "data/another_sample_data.yml"
219
+ data_merge test: 'xyzzy'
220
+
221
+ def action
222
+ puts self.class.accessible_data
223
+ end
224
+ end
225
+
226
+ data = TestData.new
227
+ data.action
228
+ ```
229
+
230
+ ## Getting and Setting Data
231
+
232
+ Consider the following:
233
+
234
+ ```ruby
235
+ class TestData
236
+ include DataAccessible
237
+ end
238
+
239
+ TestData.data_load({
240
+ :superheroes => {
241
+ :green_lantern => {
242
+ :secret_identity => [
243
+ { name: 'Hal Jordan' },
244
+ { name: 'John Stewart '},
245
+ { name: 'Guy Gardner' }
246
+ ]
247
+ }
248
+ }
249
+ })
250
+ ```
251
+
252
+ You can reference the data as such:
253
+
254
+ ```ruby
255
+ puts TestData.superheroes.green_lantern.secret_identity[0].name
256
+ puts TestData.superheroes[:green_lantern].secret_identity[0].name
257
+ puts TestData.superheroes.green_lantern
258
+ ```
259
+
260
+ That will get you:
261
+
262
+ ```
263
+ Hal Jordan
264
+ Hal Jordan
265
+ {:secret_identity=>[{:name=>"Hal Jordan"}, {:name=>"Kyle Rayner"}, {:name=>"Guy Gardner"}]}
266
+ ```
267
+
268
+ You can also set the data by referencing an index directly:
269
+
270
+ ```ruby
271
+ TestData[:superheroes].green_lantern.secret_identity[1].name = 'Kyle Rayner'
272
+ ```
273
+
274
+ 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:
275
+
276
+ ```ruby
277
+ villain = TestData[:supervillain] || 'Parallax'
278
+ ```
279
+
280
+ You can also use a `[]=` method.
281
+
282
+ ```ruby
283
+ TestData[:supervillain] = 'Parallax'
284
+ puts TestData.supervillain
285
+ ```
286
+
287
+ ### Caveat on Accessors
288
+
289
+ Consider the following:
290
+
291
+ ```ruby
292
+ class TestData
293
+ include DataAccessible
294
+ end
295
+
296
+ TestData.data_load({})
297
+
298
+ TestData[:hal_jordan] = "Green Lantern" # []=
299
+ puts TestData[:hal_jordan] # []
300
+ puts TestData.hal_jordan
301
+ ```
302
+
303
+ 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 DataAccessible are being called. Notice that the "hal_jordan" key is being created on the fly. Now consider this:
304
+
305
+ ```ruby
306
+ class TestData
307
+ include DataAccessible
308
+ end
309
+
310
+ TestData.data_load({ hal_jordan: { :superhero => 'Green Lantern' } })
311
+ puts TestData.hal_jordan
312
+ puts TestData.hal_jordan.superhero
313
+
314
+ TestData.hal_jordan[:evil] = 'Parallax'
315
+ puts TestData.hal_jordan[:evil] # WORKS
316
+ puts TestData.hal_jordan.evil # DOES NOT WORK
317
+ ```
318
+
319
+ 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 DataAccessible but rather the standard mechanism for insertion into a hash provided by Ruby. This means the accessors that DataAccessible provides are not created. So this is where you would want to use `data_merge` to bring in data.
320
+
321
+ ## ERB Processing
322
+
323
+ DataAccessible will process files with Embedded Ruby. For example, you could have a data source like this:
324
+
325
+ ```yaml
326
+ numbers:
327
+ integers:
328
+ one: 1
329
+ four: <%= 2 + 2 %>
330
+ ```
331
+
332
+ This would be referenced by DataAccessible as such:
333
+
334
+ ```
335
+ {"numbers"=>{"integers"=>{"one"=>1, "four"=>4}}}
336
+ ```
337
+
338
+ Notice how the keyword "four" shows the calculated value.
339
+
340
+ ## Development
341
+
342
+ 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`.
343
+
344
+ ## Contributing
345
+
346
+ Bug reports and pull requests are welcome on GitHub at [https://github.com/jeffnyman/data_accessible](https://github.com/jeffnyman/data_accessible). 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.
347
+
348
+ To contribute to DataAccessible:
349
+
350
+ 1. [Fork the project](http://gun.io/blog/how-to-github-fork-branch-and-pull-request/).
351
+ 2. Create your feature branch. (`git checkout -b my-new-feature`)
352
+ 3. Commit your changes. (`git commit -am 'new feature'`)
353
+ 4. Push the branch. (`git push origin my-new-feature`)
354
+ 5. Create a new [pull request](https://help.github.com/articles/using-pull-requests).
355
+
356
+ ## Author
357
+
358
+ * [Jeff Nyman](http://testerstories.com)
359
+
360
+ ## License
361
+
362
+ DataAccessible is distributed under the [MIT](http://www.opensource.org/licenses/MIT) license.
363
+ See the [LICENSE](https://github.com/jeffnyman/data_accessible/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 = "DataAccessible #{DataAccessible::VERSION}"
28
+ rdoc.rdoc_files.include('README*', 'lib/**/*.rb')
29
+ end
30
+
31
+ task default: ['spec:all', :rubocop]
data/bin/console ADDED
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "data_accessible"
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,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -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 'data_accessible/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "data_accessible"
8
+ spec.version = DataAccessible::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 = "https://github.com/jeffnyman/data_accessible"
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
+ DataAccessible #{DataAccessible::VERSION} has been installed.
33
+ (::) (::) (::) (::) (::) (::) (::) (::) (::) (::) (::) (::)
34
+ }
35
+ end
@@ -0,0 +1,37 @@
1
+ module DataAccessible
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] = DataAccessible::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 DataAccessible
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("#{DataAccessible.data_path}/#{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 DataAccessible
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
@@ -0,0 +1,3 @@
1
+ module DataAccessible
2
+ VERSION = "1.0.1".freeze
3
+ end
@@ -0,0 +1,65 @@
1
+ require "data_accessible/version"
2
+ require "data_accessible/data_loader"
3
+ require "data_accessible/hash_methods"
4
+ require "data_accessible/data_accessors"
5
+
6
+ module DataAccessible
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
+ def self.default_data_path
18
+ @data_path = 'data'
19
+ end
20
+
21
+ def self.data_path=(path)
22
+ @data_path = path
23
+ end
24
+
25
+ def self.data_path
26
+ return @data_path if @data_path
27
+ default_data_path
28
+ end
29
+
30
+ module ClassMethods
31
+ def data_load(data_source, namespace = nil)
32
+ to_h.clear
33
+ data_merge(data_source, namespace)
34
+ end
35
+
36
+ def data_merge(data_source, namespace = nil)
37
+ source_data = DataLoader.load_source(data_source)
38
+ new_data = namespace ? source_data.fetch(namespace) : source_data
39
+
40
+ @data = HashMethods.deep_merge(to_h, new_data)
41
+
42
+ DataAccessors.accessor_for_obj(self)
43
+ DataAccessors.accessor_for_data(to_h)
44
+
45
+ to_h
46
+ end
47
+
48
+ def to_h
49
+ @data ||= {}
50
+ end
51
+
52
+ def [](key)
53
+ to_h[key]
54
+ end
55
+
56
+ def []=(key, value)
57
+ DataAccessors.define_accessor(to_h, key)
58
+ DataAccessors.define_accessor(self, key)
59
+ to_h[key] = DataAccessors.accessor_for_data(value)
60
+ end
61
+
62
+ alias accessible_data to_h
63
+ alias data_accessible to_h
64
+ end
65
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: data_accessible
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeff Nyman
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-01-18 00:00:00.000000000 Z
11
+ date: 2017-01-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -86,13 +86,30 @@ email:
86
86
  executables: []
87
87
  extensions: []
88
88
  extra_rdoc_files: []
89
- 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
+ - bin/console
100
+ - bin/setup
101
+ - data_accessible.gemspec
102
+ - lib/data_accessible.rb
103
+ - lib/data_accessible/data_accessors.rb
104
+ - lib/data_accessible/data_loader.rb
105
+ - lib/data_accessible/hash_methods.rb
106
+ - lib/data_accessible/version.rb
90
107
  homepage: https://github.com/jeffnyman/data_accessible
91
108
  licenses:
92
109
  - MIT
93
110
  metadata: {}
94
111
  post_install_message: "\n(::) (::) (::) (::) (::) (::) (::) (::) (::) (::) (::) (::)\n
95
- \ DataAccessible 1.0.0 has been installed.\n(::) (::) (::) (::) (::) (::) (::) (::)
112
+ \ DataAccessible 1.0.1 has been installed.\n(::) (::) (::) (::) (::) (::) (::) (::)
96
113
  (::) (::) (::) (::)\n "
97
114
  rdoc_options: []
98
115
  require_paths:
@@ -109,7 +126,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
109
126
  version: '0'
110
127
  requirements: []
111
128
  rubyforge_project:
112
- rubygems_version: 2.5.1
129
+ rubygems_version: 2.6.10
113
130
  signing_key:
114
131
  specification_version: 4
115
132
  summary: Provides accessibility mechanism for data.