hashematics 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/.editorconfig +8 -0
- data/.gitignore +4 -0
- data/.rubocop.yml +11 -0
- data/.ruby-version +1 -0
- data/.travis.yml +20 -0
- data/CHANGELOG.md +7 -0
- data/Gemfile +5 -0
- data/Gemfile.lock +123 -0
- data/Guardfile +16 -0
- data/LICENSE +7 -0
- data/README.md +660 -0
- data/bin/benchmark +134 -0
- data/bin/console +11 -0
- data/hashematics.gemspec +32 -0
- data/lib/hashematics.rb +10 -0
- data/lib/hashematics/category.rb +67 -0
- data/lib/hashematics/configuration.rb +90 -0
- data/lib/hashematics/dictionary.rb +72 -0
- data/lib/hashematics/graph.rb +62 -0
- data/lib/hashematics/group.rb +57 -0
- data/lib/hashematics/hashematics.rb +36 -0
- data/lib/hashematics/id.rb +29 -0
- data/lib/hashematics/key.rb +76 -0
- data/lib/hashematics/object_interface.rb +35 -0
- data/lib/hashematics/record.rb +60 -0
- data/lib/hashematics/record_set.rb +29 -0
- data/lib/hashematics/type.rb +72 -0
- data/lib/hashematics/version.rb +12 -0
- data/lib/hashematics/visitor.rb +50 -0
- data/spec/examples/person.rb +36 -0
- data/spec/fixtures/config.yml +44 -0
- data/spec/fixtures/data.csv +9 -0
- data/spec/fixtures/people.yml +84 -0
- data/spec/hashematics/category_spec.rb +62 -0
- data/spec/hashematics/graph_spec.rb +572 -0
- data/spec/hashematics/key_spec.rb +37 -0
- data/spec/hashematics/object_interface_spec.rb +42 -0
- data/spec/hashematics/record_set_spec.rb +24 -0
- data/spec/hashematics/record_spec.rb +49 -0
- data/spec/hashematics/type_spec.rb +104 -0
- data/spec/spec_helper.rb +42 -0
- metadata +211 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 5007851850b8910d16671bd8c72ed343c257f64e389e177fe1eaa50a98b118a7
|
4
|
+
data.tar.gz: d3104c63ea0b0e641996c35f363dc8aaf53816ba0824fdfc40a248689124b9d9
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7a5309d785c85d5a0899854f462bfa8d6eebc3802317dedbe58b48f4d9d91b6b64b39583e4389082b9c4d0355130afb7cb359060e2849c29e5d180b0f6ce8d95
|
7
|
+
data.tar.gz: 7eac28ed9ffee6374cbc08585f6d3e985c3d9f0f393b7b0c6993c631282047f16dc718d27b2dd1d1fbd7f789d13b8dd0558e881f77b298a7ecda7906c7f4bbac
|
data/.editorconfig
ADDED
data/.gitignore
ADDED
data/.rubocop.yml
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.6.0
|
data/.travis.yml
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
env:
|
2
|
+
global:
|
3
|
+
- CC_TEST_REPORTER_ID=da9fc61f4ee94591c463b7fc0b8088e1eb5f26feaa95b5b08890959a5e9cc3b6
|
4
|
+
language: ruby
|
5
|
+
rvm:
|
6
|
+
# Build on the latest stable of all supported Rubies (https://www.ruby-lang.org/en/downloads/):
|
7
|
+
- 2.3.8
|
8
|
+
- 2.4.5
|
9
|
+
- 2.5.3
|
10
|
+
- 2.6.0
|
11
|
+
cache: bundler
|
12
|
+
before_script:
|
13
|
+
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
|
14
|
+
- chmod +x ./cc-test-reporter
|
15
|
+
- ./cc-test-reporter before-build
|
16
|
+
script:
|
17
|
+
- bundle exec rubocop
|
18
|
+
- bundle exec rspec spec --format documentation
|
19
|
+
after_script:
|
20
|
+
- ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
|
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
hashematics (1.0.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
Ascii85 (1.0.3)
|
10
|
+
afm (0.2.2)
|
11
|
+
ansi (1.5.0)
|
12
|
+
ast (2.4.0)
|
13
|
+
coderay (1.1.2)
|
14
|
+
concurrent-ruby (1.1.5)
|
15
|
+
diff-lcs (1.3)
|
16
|
+
docile (1.3.1)
|
17
|
+
faker (1.9.3)
|
18
|
+
i18n (>= 0.7)
|
19
|
+
ffi (1.9.25)
|
20
|
+
formatador (0.2.5)
|
21
|
+
guard (2.15.0)
|
22
|
+
formatador (>= 0.2.4)
|
23
|
+
listen (>= 2.7, < 4.0)
|
24
|
+
lumberjack (>= 1.0.12, < 2.0)
|
25
|
+
nenv (~> 0.1)
|
26
|
+
notiffany (~> 0.0)
|
27
|
+
pry (>= 0.9.12)
|
28
|
+
shellany (~> 0.0)
|
29
|
+
thor (>= 0.18.1)
|
30
|
+
guard-compat (1.2.1)
|
31
|
+
guard-rspec (4.7.3)
|
32
|
+
guard (~> 2.1)
|
33
|
+
guard-compat (~> 1.1)
|
34
|
+
rspec (>= 2.99.0, < 4.0)
|
35
|
+
hashery (2.1.2)
|
36
|
+
hirb (0.7.3)
|
37
|
+
i18n (1.6.0)
|
38
|
+
concurrent-ruby (~> 1.0)
|
39
|
+
jaro_winkler (1.5.2)
|
40
|
+
json (2.1.0)
|
41
|
+
listen (3.1.5)
|
42
|
+
rb-fsevent (~> 0.9, >= 0.9.4)
|
43
|
+
rb-inotify (~> 0.9, >= 0.9.7)
|
44
|
+
ruby_dep (~> 1.2)
|
45
|
+
lumberjack (1.0.13)
|
46
|
+
method_source (0.9.2)
|
47
|
+
nenv (0.3.0)
|
48
|
+
notiffany (0.1.1)
|
49
|
+
nenv (~> 0.1)
|
50
|
+
shellany (~> 0.0)
|
51
|
+
parallel (1.13.0)
|
52
|
+
parser (2.6.0.0)
|
53
|
+
ast (~> 2.4.0)
|
54
|
+
pdf-inspector (1.3.0)
|
55
|
+
pdf-reader (>= 1.0, < 3.0.a)
|
56
|
+
pdf-reader (2.2.0)
|
57
|
+
Ascii85 (~> 1.0.0)
|
58
|
+
afm (~> 0.2.1)
|
59
|
+
hashery (~> 2.0)
|
60
|
+
ruby-rc4
|
61
|
+
ttfunk
|
62
|
+
powerpack (0.1.2)
|
63
|
+
pry (0.12.2)
|
64
|
+
coderay (~> 1.1.0)
|
65
|
+
method_source (~> 0.9.0)
|
66
|
+
rainbow (3.0.0)
|
67
|
+
rb-fsevent (0.10.3)
|
68
|
+
rb-inotify (0.9.10)
|
69
|
+
ffi (>= 0.5.0, < 2)
|
70
|
+
rspec (3.8.0)
|
71
|
+
rspec-core (~> 3.8.0)
|
72
|
+
rspec-expectations (~> 3.8.0)
|
73
|
+
rspec-mocks (~> 3.8.0)
|
74
|
+
rspec-core (3.8.0)
|
75
|
+
rspec-support (~> 3.8.0)
|
76
|
+
rspec-expectations (3.8.2)
|
77
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
78
|
+
rspec-support (~> 3.8.0)
|
79
|
+
rspec-mocks (3.8.0)
|
80
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
81
|
+
rspec-support (~> 3.8.0)
|
82
|
+
rspec-support (3.8.0)
|
83
|
+
rubocop (0.63.1)
|
84
|
+
jaro_winkler (~> 1.5.1)
|
85
|
+
parallel (~> 1.10)
|
86
|
+
parser (>= 2.5, != 2.5.1.1)
|
87
|
+
powerpack (~> 0.1)
|
88
|
+
rainbow (>= 2.2.2, < 4.0)
|
89
|
+
ruby-progressbar (~> 1.7)
|
90
|
+
unicode-display_width (~> 1.4.0)
|
91
|
+
ruby-progressbar (1.10.0)
|
92
|
+
ruby-rc4 (0.1.5)
|
93
|
+
ruby_dep (1.5.0)
|
94
|
+
shellany (0.0.1)
|
95
|
+
simplecov (0.16.1)
|
96
|
+
docile (~> 1.1)
|
97
|
+
json (>= 1.8, < 3)
|
98
|
+
simplecov-html (~> 0.10.0)
|
99
|
+
simplecov-console (0.4.2)
|
100
|
+
ansi
|
101
|
+
hirb
|
102
|
+
simplecov
|
103
|
+
simplecov-html (0.10.2)
|
104
|
+
thor (0.20.3)
|
105
|
+
ttfunk (1.5.1)
|
106
|
+
unicode-display_width (1.4.1)
|
107
|
+
|
108
|
+
PLATFORMS
|
109
|
+
ruby
|
110
|
+
|
111
|
+
DEPENDENCIES
|
112
|
+
faker (~> 1)
|
113
|
+
guard-rspec (~> 4.7)
|
114
|
+
hashematics!
|
115
|
+
pdf-inspector (~> 1)
|
116
|
+
pry (~> 0)
|
117
|
+
rspec (~> 3.8)
|
118
|
+
rubocop (~> 0.63.1)
|
119
|
+
simplecov (~> 0.16.1)
|
120
|
+
simplecov-console (~> 0.4.2)
|
121
|
+
|
122
|
+
BUNDLED WITH
|
123
|
+
1.17.3
|
data/Guardfile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
guard :rspec, cmd: 'bundle exec rspec' do
|
4
|
+
require 'guard/rspec/dsl'
|
5
|
+
dsl = Guard::RSpec::Dsl.new(self)
|
6
|
+
|
7
|
+
# RSpec files
|
8
|
+
rspec = dsl.rspec
|
9
|
+
watch(rspec.spec_helper) { rspec.spec_dir }
|
10
|
+
watch(rspec.spec_support) { rspec.spec_dir }
|
11
|
+
watch(rspec.spec_files)
|
12
|
+
|
13
|
+
# Ruby files
|
14
|
+
ruby = dsl.ruby
|
15
|
+
dsl.watch_spec_files_for(ruby.lib_files)
|
16
|
+
end
|
data/LICENSE
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
Copyright 2019 Blue Marble Payroll, LLC
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
4
|
+
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
6
|
+
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,660 @@
|
|
1
|
+
# Hashematics
|
2
|
+
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/hashematics.svg)](https://badge.fury.io/rb/hashematics) [![Build Status](https://travis-ci.org/bluemarblepayroll/hashematics.svg?branch=master)](https://travis-ci.org/bluemarblepayroll/hashematics) [![Maintainability](https://api.codeclimate.com/v1/badges/a171325c301e58eb4fb0/maintainability)](https://codeclimate.com/github/bluemarblepayroll/hashematics/maintainability) [![Test Coverage](https://api.codeclimate.com/v1/badges/a171325c301e58eb4fb0/test_coverage)](https://codeclimate.com/github/bluemarblepayroll/hashematics/test_coverage) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
|
4
|
+
|
5
|
+
Hashematics is a configuration-based object graphing tool which can turn a flat, single dimensional dataset into a structure of deeply nested objects.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
To install through Rubygems:
|
10
|
+
|
11
|
+
````
|
12
|
+
gem install install hashematics
|
13
|
+
````
|
14
|
+
|
15
|
+
You can also add this to your Gemfile:
|
16
|
+
|
17
|
+
````
|
18
|
+
bundle add hashematics
|
19
|
+
````
|
20
|
+
|
21
|
+
## Examples
|
22
|
+
|
23
|
+
### Getting Started
|
24
|
+
|
25
|
+
Take the following simple, non-nested data set:
|
26
|
+
|
27
|
+
id | first | last
|
28
|
+
-- | ----- | ------
|
29
|
+
1 | Bruce | Banner
|
30
|
+
2 | Tony | Stark
|
31
|
+
|
32
|
+
We could read this in using the following configuration:
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
rows = [
|
36
|
+
{
|
37
|
+
id: 1,
|
38
|
+
first: 'Bruce',
|
39
|
+
last: 'Banner'
|
40
|
+
},
|
41
|
+
{
|
42
|
+
id: 2,
|
43
|
+
first: 'Tony',
|
44
|
+
last: 'Stark'
|
45
|
+
}
|
46
|
+
]
|
47
|
+
|
48
|
+
graph = ::Hashematics.graph(rows: rows)
|
49
|
+
objects = graph.rows
|
50
|
+
```
|
51
|
+
|
52
|
+
The variable `objects` will now contain the same data as `rows`. This, so far, is not very useful but it sets up base case.
|
53
|
+
|
54
|
+
### Introduction to Simple Shaping
|
55
|
+
|
56
|
+
Let's say that we only want id and first variables:
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
config = {
|
60
|
+
types: {
|
61
|
+
person: {
|
62
|
+
properties: %i[id first]
|
63
|
+
}
|
64
|
+
},
|
65
|
+
groups: {
|
66
|
+
avengers: {
|
67
|
+
by: :id,
|
68
|
+
type: :person
|
69
|
+
}
|
70
|
+
}
|
71
|
+
}
|
72
|
+
|
73
|
+
rows = [
|
74
|
+
{
|
75
|
+
id: 1,
|
76
|
+
first: 'Bruce',
|
77
|
+
last: 'Banner'
|
78
|
+
},
|
79
|
+
{
|
80
|
+
id: 2,
|
81
|
+
first: 'Tony',
|
82
|
+
last: 'Stark'
|
83
|
+
}
|
84
|
+
]
|
85
|
+
|
86
|
+
graph = ::Hashematics.graph(config: config, rows: rows)
|
87
|
+
objects = graph.data(:avengers)
|
88
|
+
```
|
89
|
+
|
90
|
+
Notice how we are grouping the data and calling the #data API. Now the `objects` variable should now look like:
|
91
|
+
|
92
|
+
```ruby
|
93
|
+
[
|
94
|
+
{
|
95
|
+
id: 1,
|
96
|
+
first: 'Bruce'
|
97
|
+
},
|
98
|
+
{
|
99
|
+
id: 2,
|
100
|
+
first: 'Tony'
|
101
|
+
}
|
102
|
+
]
|
103
|
+
```
|
104
|
+
|
105
|
+
### Cross-Mapping Shape Attribute Names
|
106
|
+
|
107
|
+
Say we wanted to change the attribute names:
|
108
|
+
|
109
|
+
```ruby
|
110
|
+
config = {
|
111
|
+
types: {
|
112
|
+
person: {
|
113
|
+
properties: {
|
114
|
+
id_number: :id,
|
115
|
+
first_name: :first
|
116
|
+
}
|
117
|
+
}
|
118
|
+
},
|
119
|
+
groups: {
|
120
|
+
avengers: {
|
121
|
+
by: :id,
|
122
|
+
type: :person
|
123
|
+
}
|
124
|
+
}
|
125
|
+
}
|
126
|
+
|
127
|
+
rows = [
|
128
|
+
{
|
129
|
+
id: 1,
|
130
|
+
first: 'Bruce',
|
131
|
+
last: 'Banner'
|
132
|
+
},
|
133
|
+
{
|
134
|
+
id: 2,
|
135
|
+
first: 'Tony',
|
136
|
+
last: 'Stark'
|
137
|
+
}
|
138
|
+
]
|
139
|
+
|
140
|
+
graph = ::Hashematics.graph(config: config, rows: rows)
|
141
|
+
objects = graph.data(:avengers)
|
142
|
+
```
|
143
|
+
|
144
|
+
The `objects` variable should now look like:
|
145
|
+
|
146
|
+
```ruby
|
147
|
+
[
|
148
|
+
{
|
149
|
+
id_number: 1,
|
150
|
+
first_name: 'Bruce'
|
151
|
+
},
|
152
|
+
{
|
153
|
+
id_number: 2,
|
154
|
+
first_name: 'Tony'
|
155
|
+
}
|
156
|
+
]
|
157
|
+
```
|
158
|
+
|
159
|
+
### Nested Shaping
|
160
|
+
|
161
|
+
Let's build on our initial data set to:
|
162
|
+
|
163
|
+
* include child data (one-to-many) relationship
|
164
|
+
* start with different attributes (cross map attribute names)
|
165
|
+
|
166
|
+
ID # | First Name | Last Name | Costume ID # | Costume Name | Costume Color
|
167
|
+
---- | ---------- | --------- | ------------ | ------------ | -------------
|
168
|
+
1 | Bruce | Banner | 3 | Basic Hulk | Green
|
169
|
+
1 | Bruce | Banner | 4 | Mad Hulk | Red
|
170
|
+
2 | Tony | Stark | 5 | Mark I | Gray
|
171
|
+
2 | Tony | Stark | 6 | Mark IV | Red
|
172
|
+
2 | Tony | Stark | 7 | Mark VI | Nano-Blue
|
173
|
+
|
174
|
+
We could now read this in as:
|
175
|
+
|
176
|
+
```ruby
|
177
|
+
config = {
|
178
|
+
types: {
|
179
|
+
person: {
|
180
|
+
properties: {
|
181
|
+
id: 'ID #',
|
182
|
+
first: 'First Name',
|
183
|
+
last: 'Last Name'
|
184
|
+
}
|
185
|
+
},
|
186
|
+
costume: {
|
187
|
+
properties: {
|
188
|
+
id: 'Costume ID #',
|
189
|
+
name: 'Costume Name',
|
190
|
+
color: 'Costume Color'
|
191
|
+
}
|
192
|
+
}
|
193
|
+
},
|
194
|
+
groups: {
|
195
|
+
avengers: {
|
196
|
+
by: 'ID #',
|
197
|
+
type: :person,
|
198
|
+
groups: {
|
199
|
+
costumes: {
|
200
|
+
by: 'Costume ID #',
|
201
|
+
type: :costume
|
202
|
+
}
|
203
|
+
}
|
204
|
+
}
|
205
|
+
}
|
206
|
+
}
|
207
|
+
|
208
|
+
rows = [
|
209
|
+
{
|
210
|
+
'ID #' => 1,
|
211
|
+
'First Name' => 'Bruce',
|
212
|
+
'Last Name' => 'Banner',
|
213
|
+
'Costume ID #' => 3,
|
214
|
+
'Costume Name' => 'Basic Hulk',
|
215
|
+
'Costume Color' => 'Green'
|
216
|
+
},
|
217
|
+
{
|
218
|
+
'ID #' => 1,
|
219
|
+
'First Name' => 'Bruce',
|
220
|
+
'Last Name' => 'Banner',
|
221
|
+
'Costume ID #' => 4,
|
222
|
+
'Costume Name' => 'Mad Hulk',
|
223
|
+
'Costume Color' => 'Red'
|
224
|
+
},
|
225
|
+
{
|
226
|
+
'ID #' => 2,
|
227
|
+
'First Name' => 'Tony',
|
228
|
+
'Last Name' => 'Stark',
|
229
|
+
'Costume ID #' => 5,
|
230
|
+
'Costume Name' => 'Mark I',
|
231
|
+
'Costume Color' => 'Gray'
|
232
|
+
},
|
233
|
+
{
|
234
|
+
'ID #' => 2,
|
235
|
+
'First Name' => 'Tony',
|
236
|
+
'Last Name' => 'Stark',
|
237
|
+
'Costume ID #' => 6,
|
238
|
+
'Costume Name' => 'Mark IV',
|
239
|
+
'Costume Color' => 'Red'
|
240
|
+
},
|
241
|
+
{
|
242
|
+
'ID #' => 2,
|
243
|
+
'First Name' => 'Tony',
|
244
|
+
'Last Name' => 'Stark',
|
245
|
+
'Costume ID #' => 7,
|
246
|
+
'Costume Name' => 'Mark VI',
|
247
|
+
'Costume Color' => 'Nano-Blue'
|
248
|
+
}
|
249
|
+
]
|
250
|
+
|
251
|
+
graph = ::Hashematics.graph(config: config, rows: rows)
|
252
|
+
objects = graph.data(:avengers)
|
253
|
+
```
|
254
|
+
|
255
|
+
The `objects` variable should now look like:
|
256
|
+
|
257
|
+
```ruby
|
258
|
+
[
|
259
|
+
{
|
260
|
+
id: 1,
|
261
|
+
first: 'Bruce',
|
262
|
+
last: 'Banner',
|
263
|
+
costumes: [
|
264
|
+
{ id: 3, name: 'Basic Hulk', color: 'Green' },
|
265
|
+
{ id: 4, name: 'Mad Hulk', color: 'Red' }
|
266
|
+
]
|
267
|
+
},
|
268
|
+
{
|
269
|
+
id: 2,
|
270
|
+
first: 'Tony',
|
271
|
+
last: 'Stark',
|
272
|
+
costumes: [
|
273
|
+
{ id: 5, name: 'Mark I', color: 'Gray' },
|
274
|
+
{ id: 6, name: 'Mark IV', color: 'Red' },
|
275
|
+
{ id: 7, name: 'Mark VI', color: 'Nano-Blue' }
|
276
|
+
]
|
277
|
+
}
|
278
|
+
]
|
279
|
+
```
|
280
|
+
|
281
|
+
Shaping / grouping is recursive and should support richer breadth as well as depth graphs.
|
282
|
+
|
283
|
+
### Multiple Top-Level Graphs
|
284
|
+
|
285
|
+
You are not limited to just one top-level graph. For example, we could expand on the previous example to include another grouping of costumes:
|
286
|
+
|
287
|
+
```ruby
|
288
|
+
config = {
|
289
|
+
types: {
|
290
|
+
person: {
|
291
|
+
properties: {
|
292
|
+
id: 'ID #',
|
293
|
+
first: 'First Name',
|
294
|
+
last: 'Last Name'
|
295
|
+
}
|
296
|
+
},
|
297
|
+
costume: {
|
298
|
+
properties: {
|
299
|
+
id: 'Costume ID #',
|
300
|
+
name: 'Costume Name',
|
301
|
+
color: 'Costume Color'
|
302
|
+
}
|
303
|
+
}
|
304
|
+
},
|
305
|
+
groups: {
|
306
|
+
avengers: {
|
307
|
+
by: 'ID #',
|
308
|
+
type: :person,
|
309
|
+
groups: {
|
310
|
+
costumes: {
|
311
|
+
by: 'Costume ID #',
|
312
|
+
type: :costume
|
313
|
+
}
|
314
|
+
}
|
315
|
+
},
|
316
|
+
costumes: {
|
317
|
+
by: 'Costume ID #',
|
318
|
+
type: :costume
|
319
|
+
}
|
320
|
+
}
|
321
|
+
}
|
322
|
+
|
323
|
+
rows = [
|
324
|
+
{
|
325
|
+
'ID #' => 1,
|
326
|
+
'First Name' => 'Bruce',
|
327
|
+
'Last Name' => 'Banner',
|
328
|
+
'Costume ID #' => 3,
|
329
|
+
'Costume Name' => 'Basic Hulk',
|
330
|
+
'Costume Color' => 'Green'
|
331
|
+
},
|
332
|
+
{
|
333
|
+
'ID #' => 1,
|
334
|
+
'First Name' => 'Bruce',
|
335
|
+
'Last Name' => 'Banner',
|
336
|
+
'Costume ID #' => 4,
|
337
|
+
'Costume Name' => 'Mad Hulk',
|
338
|
+
'Costume Color' => 'Red'
|
339
|
+
},
|
340
|
+
{
|
341
|
+
'ID #' => 2,
|
342
|
+
'First Name' => 'Tony',
|
343
|
+
'Last Name' => 'Stark',
|
344
|
+
'Costume ID #' => 5,
|
345
|
+
'Costume Name' => 'Mark I',
|
346
|
+
'Costume Color' => 'Gray'
|
347
|
+
},
|
348
|
+
{
|
349
|
+
'ID #' => 2,
|
350
|
+
'First Name' => 'Tony',
|
351
|
+
'Last Name' => 'Stark',
|
352
|
+
'Costume ID #' => 6,
|
353
|
+
'Costume Name' => 'Mark IV',
|
354
|
+
'Costume Color' => 'Red'
|
355
|
+
},
|
356
|
+
{
|
357
|
+
'ID #' => 2,
|
358
|
+
'First Name' => 'Tony',
|
359
|
+
'Last Name' => 'Stark',
|
360
|
+
'Costume ID #' => 7,
|
361
|
+
'Costume Name' => 'Mark VI',
|
362
|
+
'Costume Color' => 'Nano-Blue'
|
363
|
+
}
|
364
|
+
]
|
365
|
+
|
366
|
+
graph = ::Hashematics.graph(config: config, rows: rows)
|
367
|
+
objects = graph.data(:costumes)
|
368
|
+
```
|
369
|
+
|
370
|
+
The `objects` variable should now look like:
|
371
|
+
|
372
|
+
```ruby
|
373
|
+
[
|
374
|
+
{ id: 3, name: 'Basic Hulk', color: 'Green' },
|
375
|
+
{ id: 4, name: 'Mad Hulk', color: 'Red' },
|
376
|
+
{ id: 5, name: 'Mark I', color: 'Gray' },
|
377
|
+
{ id: 6, name: 'Mark IV', color: 'Red' },
|
378
|
+
{ id: 7, name: 'Mark VI', color: 'Nano-Blue' }
|
379
|
+
]
|
380
|
+
```
|
381
|
+
|
382
|
+
### Handling Blanks
|
383
|
+
|
384
|
+
Records with blank ID's are ignored by default. This is due to the flat nature of the incoming data. Take the following example:
|
385
|
+
|
386
|
+
ID # | First Name | Last Name | Costume ID # | Costume Name | Costume Color
|
387
|
+
---- | ---------- | --------- | ------------ | ------------ | -------------
|
388
|
+
1 | Bruce | Banner | 3 | Basic Hulk | Green
|
389
|
+
2 | Tony | Stark | | |
|
390
|
+
| | | 4 | Undercover | Purple
|
391
|
+
|
392
|
+
This is interpreted as:
|
393
|
+
|
394
|
+
* Bruce Banner is an avenger and has 2 costumes
|
395
|
+
* Tony Stark is an avenger but has no costumes
|
396
|
+
* An undercover purple costume exists, but belongs to no avenger
|
397
|
+
|
398
|
+
We could read this in while ignoring blank IDs (default):
|
399
|
+
|
400
|
+
```ruby
|
401
|
+
config = {
|
402
|
+
types: {
|
403
|
+
person: {
|
404
|
+
properties: {
|
405
|
+
id: 'ID #',
|
406
|
+
first: 'First Name',
|
407
|
+
last: 'Last Name'
|
408
|
+
}
|
409
|
+
},
|
410
|
+
costume: {
|
411
|
+
properties: {
|
412
|
+
id: 'Costume ID #',
|
413
|
+
name: 'Costume Name',
|
414
|
+
color: 'Costume Color'
|
415
|
+
}
|
416
|
+
}
|
417
|
+
},
|
418
|
+
groups: {
|
419
|
+
avengers: {
|
420
|
+
by: 'ID #',
|
421
|
+
type: :person,
|
422
|
+
groups: {
|
423
|
+
costumes: {
|
424
|
+
by: 'Costume ID #',
|
425
|
+
type: :costume
|
426
|
+
}
|
427
|
+
}
|
428
|
+
},
|
429
|
+
costumes: {
|
430
|
+
by: 'Costume ID #',
|
431
|
+
type: :costume
|
432
|
+
}
|
433
|
+
}
|
434
|
+
}
|
435
|
+
|
436
|
+
rows = [
|
437
|
+
{
|
438
|
+
'ID #' => 1,
|
439
|
+
'First Name' => 'Bruce',
|
440
|
+
'Last Name' => 'Banner',
|
441
|
+
'Costume ID #' => 3,
|
442
|
+
'Costume Name' => 'Basic Hulk',
|
443
|
+
'Costume Color' => 'Green'
|
444
|
+
},
|
445
|
+
{
|
446
|
+
'ID #' => 2,
|
447
|
+
'First Name' => 'Tony',
|
448
|
+
'Last Name' => 'Stark',
|
449
|
+
'Costume ID #' => '',
|
450
|
+
'Costume Name' => '',
|
451
|
+
'Costume Color' => ''
|
452
|
+
},
|
453
|
+
{
|
454
|
+
'Costume ID #' => 4,
|
455
|
+
'Costume Name' => 'Undercover',
|
456
|
+
'Costume Color' => 'Purple'
|
457
|
+
}
|
458
|
+
]
|
459
|
+
|
460
|
+
graph = ::Hashematics.graph(config: config, rows: rows)
|
461
|
+
avengers = graph.data(:avengers)
|
462
|
+
costumes = graph.data(:costumes)
|
463
|
+
```
|
464
|
+
|
465
|
+
The `avengers` variable should now look like:
|
466
|
+
|
467
|
+
```ruby
|
468
|
+
[
|
469
|
+
{
|
470
|
+
id: 1,
|
471
|
+
first: 'Bruce',
|
472
|
+
last: 'Banner',
|
473
|
+
costumes: [
|
474
|
+
{ id: 3, name: 'Basic Hulk', color: 'Green' }
|
475
|
+
]
|
476
|
+
},
|
477
|
+
{
|
478
|
+
id: 2,
|
479
|
+
first: 'Tony',
|
480
|
+
last: 'Stark',
|
481
|
+
costumes: []
|
482
|
+
}
|
483
|
+
]
|
484
|
+
```
|
485
|
+
|
486
|
+
The `costumes` variable should now look like:
|
487
|
+
|
488
|
+
```ruby
|
489
|
+
[
|
490
|
+
{ id: 3, name: 'Basic Hulk', color: 'Green' },
|
491
|
+
{ id: 4, name: 'Undercover', color: 'Purple' }
|
492
|
+
]
|
493
|
+
```
|
494
|
+
|
495
|
+
If you wish to include blank objects, then pass in ```include_blank: true``` option into the group configuration:
|
496
|
+
|
497
|
+
```ruby
|
498
|
+
config = {
|
499
|
+
types: {
|
500
|
+
person: {
|
501
|
+
properties: {
|
502
|
+
id: 'ID #',
|
503
|
+
first: 'First Name',
|
504
|
+
last: 'Last Name'
|
505
|
+
}
|
506
|
+
},
|
507
|
+
costume: {
|
508
|
+
properties: {
|
509
|
+
id: 'Costume ID #',
|
510
|
+
name: 'Costume Name',
|
511
|
+
color: 'Costume Color'
|
512
|
+
}
|
513
|
+
}
|
514
|
+
},
|
515
|
+
groups: {
|
516
|
+
avengers: {
|
517
|
+
by: 'ID #',
|
518
|
+
include_blank: true,
|
519
|
+
type: :person,
|
520
|
+
groups: {
|
521
|
+
costumes: {
|
522
|
+
by: 'Costume ID #',
|
523
|
+
type: :costume
|
524
|
+
}
|
525
|
+
}
|
526
|
+
},
|
527
|
+
costumes: {
|
528
|
+
by: 'Costume ID #',
|
529
|
+
include_blank: true,
|
530
|
+
type: :costume
|
531
|
+
}
|
532
|
+
}
|
533
|
+
}
|
534
|
+
|
535
|
+
rows = [
|
536
|
+
{
|
537
|
+
'ID #' => 1,
|
538
|
+
'First Name' => 'Bruce',
|
539
|
+
'Last Name' => 'Banner',
|
540
|
+
'Costume ID #' => 3,
|
541
|
+
'Costume Name' => 'Basic Hulk',
|
542
|
+
'Costume Color' => 'Green'
|
543
|
+
},
|
544
|
+
{
|
545
|
+
'ID #' => 2,
|
546
|
+
'First Name' => 'Tony',
|
547
|
+
'Last Name' => 'Stark',
|
548
|
+
'Costume ID #' => '',
|
549
|
+
'Costume Name' => '',
|
550
|
+
'Costume Color' => ''
|
551
|
+
},
|
552
|
+
{
|
553
|
+
'Costume ID #' => 4,
|
554
|
+
'Costume Name' => 'Undercover',
|
555
|
+
'Costume Color' => 'Purple'
|
556
|
+
}
|
557
|
+
]
|
558
|
+
|
559
|
+
graph = ::Hashematics.graph(config: config, rows: rows)
|
560
|
+
avengers = graph.data(:avengers)
|
561
|
+
costumes = graph.data(:costumes)
|
562
|
+
```
|
563
|
+
|
564
|
+
The `avengers` variable should now look like:
|
565
|
+
|
566
|
+
```ruby
|
567
|
+
[
|
568
|
+
{
|
569
|
+
id: 1,
|
570
|
+
first: 'Bruce',
|
571
|
+
last: 'Banner',
|
572
|
+
costumes: [
|
573
|
+
{ id: 3, name: 'Basic Hulk', color: 'Green' }
|
574
|
+
]
|
575
|
+
},
|
576
|
+
{
|
577
|
+
id: 2,
|
578
|
+
first: 'Tony',
|
579
|
+
last: 'Stark',
|
580
|
+
costumes: []
|
581
|
+
},
|
582
|
+
{
|
583
|
+
id: nil,
|
584
|
+
first: nil,
|
585
|
+
last: nil,
|
586
|
+
costumes: [
|
587
|
+
{ id: 4, name: 'Undercover', color: 'Purple' }
|
588
|
+
]
|
589
|
+
}
|
590
|
+
]
|
591
|
+
```
|
592
|
+
|
593
|
+
The `costumes` variable should now look like:
|
594
|
+
|
595
|
+
```ruby
|
596
|
+
[
|
597
|
+
{ id: 3, name: 'Basic Hulk', color: 'Green' },
|
598
|
+
{ id: '', name: '', color: '' },
|
599
|
+
{ id: 4, name: 'Undercover', color: 'Purple' }
|
600
|
+
]
|
601
|
+
```
|
602
|
+
|
603
|
+
### Advanced Options
|
604
|
+
|
605
|
+
Some other options available are:
|
606
|
+
|
607
|
+
* Custom Object Types: `object_class` attribute for a type defaults to Hash but can be set as a class constant or a proc/lambda. If it is a class constant, then a new instance will be initialized from the incoming Hash. If it is a function then it will be called with the incoming hash passed in and expecting an object as a return.
|
608
|
+
* Compoound Unique Identifiers: `by` can either be a string, symbol, or array.
|
609
|
+
|
610
|
+
## Contributing
|
611
|
+
|
612
|
+
### Development Environment Configuration
|
613
|
+
|
614
|
+
Basic steps to take to get this repository compiling:
|
615
|
+
|
616
|
+
1. Install [Ruby](https://www.ruby-lang.org/en/documentation/installation/) (check hashematics.gemspec for versions supported)
|
617
|
+
2. Install bundler (gem install bundler)
|
618
|
+
3. Clone the repository (git clone git@github.com:bluemarblepayroll/hashematics.git)
|
619
|
+
4. Navigate to the root folder (cd hashematics)
|
620
|
+
5. Install dependencies (bundle)
|
621
|
+
|
622
|
+
### Running Tests
|
623
|
+
|
624
|
+
To execute the test suite run:
|
625
|
+
|
626
|
+
````
|
627
|
+
bundle exec rspec spec --format documentation
|
628
|
+
````
|
629
|
+
|
630
|
+
Alternatively, you can have Guard watch for changes:
|
631
|
+
|
632
|
+
````
|
633
|
+
bundle exec guard
|
634
|
+
````
|
635
|
+
|
636
|
+
Also, do not forget to run Rubocop:
|
637
|
+
|
638
|
+
````
|
639
|
+
bundle exec rubocop
|
640
|
+
````
|
641
|
+
|
642
|
+
### Publishing
|
643
|
+
|
644
|
+
Note: ensure you have proper authorization before trying to publish new versions.
|
645
|
+
|
646
|
+
After code changes have successfully gone through the Pull Request review process then the following steps should be followed for publishing new versions:
|
647
|
+
|
648
|
+
1. Merge Pull Request into master
|
649
|
+
2. Update ```lib/hashematics/version.rb``` using [semantic versioning](https://semver.org/)
|
650
|
+
3. Install dependencies: ```bundle```
|
651
|
+
4. Update ```CHANGELOG.md``` with release notes
|
652
|
+
5. Commit & push master to remote and ensure CI builds master successfully
|
653
|
+
6. Build the project locally: `gem build hashematics`
|
654
|
+
7. Publish package to RubyGems: `gem push hashematics-X.gem` where X is the version to push
|
655
|
+
8. Tag master with new version: `git tag <version>`
|
656
|
+
9. Push tags remotely: `git push origin --tags`
|
657
|
+
|
658
|
+
## License
|
659
|
+
|
660
|
+
This project is MIT Licensed.
|