teer 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 +12 -0
- data/.rspec +3 -0
- data/.travis.yml +7 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +282 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/teer/data_store.rb +124 -0
- data/lib/teer/engine.rb +100 -0
- data/lib/teer/vector_store.rb +48 -0
- data/lib/teer/version.rb +3 -0
- data/lib/teer.rb +48 -0
- data/teer.gemspec +45 -0
- metadata +158 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 3feb0b6fd084b70a977ec9cc6676ee6e5b8ac62b10a726890e0c18315236e44c
|
4
|
+
data.tar.gz: '018a9115975f3b770654c3696d9282a591928d632dfc8011e16ee27b472bb8d3'
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 81d1f5f94e88805d9619c6ca33667da5739f1dc0ba6835201e8bdfe3b4b6e7fddfa08ed32c3d3c02e77532937f7229c6ffc11ea0c0f04740acc62eb485463814
|
7
|
+
data.tar.gz: cf7c09a290131637e246e3973c90a20456ce7bf7b1653432662baeeaa4f3fc72b6242c58d1a6b7f696c6688131cdc88f427bddbe23c3c5aaa39ff280d0508f70
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.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 mark.tint.ramotowski@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) 2019 Mark Ramotowski
|
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,282 @@
|
|
1
|
+
[![Build Status](https://travis-ci.com/Rambatino/teer.svg?branch=master)](https://travis-ci.com/Rambatino/teer)
|
2
|
+
|
3
|
+
# Teer - Template Engingeering Rebourn
|
4
|
+
|
5
|
+
Picture the scene. You have a lot of data and you want to present the results of that data on a chart or a web page. How would you go about turning variable data into a human readable format - let's say you wanted to summarise a chart?
|
6
|
+
|
7
|
+
This small library aims to solve that problem.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'teer'
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install teer
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
### Simple Use-Case
|
28
|
+
|
29
|
+
Given a table and the dream of translating it into human readable format:
|
30
|
+
|
31
|
+
| | count |
|
32
|
+
| ----------- | ----- |
|
33
|
+
| <b>name</b> | |
|
34
|
+
| Bob | 4 |
|
35
|
+
| Alan | 14 |
|
36
|
+
| Jeff | 2 |
|
37
|
+
|
38
|
+
You can pass it in with a template yaml/hash with the format:
|
39
|
+
|
40
|
+
```yaml
|
41
|
+
names: # only run if names is a variable in the template scope
|
42
|
+
worst_name: "names.sort[-1].key"
|
43
|
+
worst_value: "names.sort[-1].value"
|
44
|
+
text:
|
45
|
+
GB_en: "{{ worst_name }} has the least apples, having only {{ worst_value }}"
|
46
|
+
FR: "{{ worst_name }} a le moins de pommes, n'en ayant que {{ worst_value }}"
|
47
|
+
```
|
48
|
+
|
49
|
+
(under the hood the yaml gets converted into a hash anyway)
|
50
|
+
|
51
|
+
and by running:
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
teer = Teer::Template.create([
|
55
|
+
{ 'name' => 'Bob', 'count' => 4 },
|
56
|
+
{ 'name' => 'Alan', 'count' => 14 },
|
57
|
+
{ 'name' => 'Jeff', 'count' => 2 }
|
58
|
+
], 'count', template).finding
|
59
|
+
```
|
60
|
+
|
61
|
+
It parses the template and substitutes the values into the text:
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
=> "Jeff has the least apples, having only 2"
|
65
|
+
```
|
66
|
+
|
67
|
+
#### Key Principles to the example
|
68
|
+
|
69
|
+
As the teer parses the data, it takes a mandatory argument "name", in this case `count` which is what the data is indexed against. The teer takes each index (there could be many) and defines new variables by pluralising the index. In this example, `names` becomes a variable (as it's one of the indexes) that we can apply methods to.
|
70
|
+
|
71
|
+
The template passed in has a first key (`names`) which checks for the presence of that variable (it's actually optional to do that check).
|
72
|
+
|
73
|
+
Variables can also be defined in the template itself, and substituted into the output text. In the example above, `worst_name` is defined as variable, defined by: `names.sort[-1].key` which means take each row, and sort (ascending is false) the `names` by `count` and take the last row and the `key` is the row member of name and the `value` is the `count` associated with that name.
|
74
|
+
|
75
|
+
As well as this, as count is a column, it can be accessed like `counts` and simple arithmetic operations can be applied to it, such as `counts.mean` or `counts.sum` to get the required value _if filtering is not required_. If filtering _is required_ then the correct approach is to filter using the indexes.
|
76
|
+
|
77
|
+
We can inspect the `names` variable, as it elucidates how these methods interact with it:
|
78
|
+
|
79
|
+
```ruby
|
80
|
+
teer.data.names # teer is defined above
|
81
|
+
=> <DataStore:0x007f83bd3e1398 @data=[["Bob", 4], ["Alan", 14], ["Jeff", 2]], @locale=:GB_en>
|
82
|
+
# it's useful to try methods here before adding them to the template:
|
83
|
+
teer.data.names.keys
|
84
|
+
=> <Teer::VectorStore:0x00007fbaa7373200 @data=["Bob", "Alan", "Jeff"], @locale=:GB_en>
|
85
|
+
|
86
|
+
teer.data.names.values
|
87
|
+
=> <Teer::VectorStore:0x00007fbaa739a378 @data=[4, 14, 2], @locale=:GB_en>
|
88
|
+
|
89
|
+
teer.data.names.max.key
|
90
|
+
=> "Alan"
|
91
|
+
|
92
|
+
teer.data.names.min.value
|
93
|
+
=> 2
|
94
|
+
|
95
|
+
teer.data.names.slice("Jeff")
|
96
|
+
=> <Teer::DataStore:0x00007fbaa8a02188 @data=[["Jeff", 2]], @locale=:GB_en>
|
97
|
+
```
|
98
|
+
|
99
|
+
Here each index row is associated with the value of the data and methods can be applied to that (defined in `lib/teer/data_store.rb`) such as `min`, `count`, `[]`, `sort` and also conditionals (such as `gt`, `lt`, `ne`, `eq`) which are able to create rich verbatims from the underlying data.
|
100
|
+
|
101
|
+
#### Multiple Language Support
|
102
|
+
|
103
|
+
If you notice, the key inside `text` is `GB_en` this is the default. However, other languages and keys are supported and can be used like:
|
104
|
+
|
105
|
+
```ruby
|
106
|
+
teer = Teer::Template.create([
|
107
|
+
{ 'name' => 'Bob', 'count' => 4 },
|
108
|
+
{ 'name' => 'Alan', 'count' => 14 },
|
109
|
+
{ 'name' => 'Jeff', 'count' => 2 }
|
110
|
+
], 'count', template, {}, :FR).finding
|
111
|
+
|
112
|
+
=> "Jeff a le moins de pommes, n'en ayant que 2"
|
113
|
+
```
|
114
|
+
|
115
|
+
#### Conditional Switch Statements
|
116
|
+
|
117
|
+
To expand on the previous example:
|
118
|
+
|
119
|
+
```yaml
|
120
|
+
names:
|
121
|
+
best_name: names.max.key
|
122
|
+
best_value: names.max.value
|
123
|
+
text:
|
124
|
+
GB_en: "{{ best_name }} has the most apples."
|
125
|
+
|
126
|
+
second_best_value: "names.sort.second.value"
|
127
|
+
much_larger: best_value > second_best_value + 5
|
128
|
+
much_larger:
|
129
|
+
second_best_name: names.sort.second.key
|
130
|
+
text:
|
131
|
+
GB_en: "It's a lot more than {{ second_best_name }} who came in second place."
|
132
|
+
not much_larger:
|
133
|
+
text:
|
134
|
+
GB_en: "However, {{ second_best_name }}'s {{ second_best_value }} was close behind."
|
135
|
+
```
|
136
|
+
|
137
|
+
With this template, the results concatenate (in a top down fashion) and it results in:
|
138
|
+
|
139
|
+
```ruby
|
140
|
+
=> "Alan has the most apples. It's a lot more than Bob who came in second place."
|
141
|
+
```
|
142
|
+
|
143
|
+
You can also collect them all separately (using the method `findings`) so as to join them anyway you like:
|
144
|
+
|
145
|
+
```ruby
|
146
|
+
Teer::Template.create([
|
147
|
+
{ 'name' => 'Bob', 'count' => 4 },
|
148
|
+
{ 'name' => 'Alan', 'count' => 14 },
|
149
|
+
{ 'name' => 'Jeff', 'count' => 2 }
|
150
|
+
], 'count', template).findings.join("\n")
|
151
|
+
|
152
|
+
=> "Alan has the most apples.\nIt's a lot more than Bob who came in second place."
|
153
|
+
```
|
154
|
+
|
155
|
+
#### Passing in extra Variables
|
156
|
+
|
157
|
+
```ruby
|
158
|
+
teer = Teer::Template.create([
|
159
|
+
{ 'name' => 'Bob', 'count' => 4 },
|
160
|
+
{ 'name' => 'Alan', 'count' => 14 },
|
161
|
+
{ 'name' => 'Jeff', 'count' => 2 }
|
162
|
+
], 'count', template, {'some_other_var' => 'my special variable'}).finding
|
163
|
+
```
|
164
|
+
|
165
|
+
`{{ some_other_var }}` will yield 'my special variable' when placed inside the template
|
166
|
+
|
167
|
+
#### A more complex example - linking between rows
|
168
|
+
|
169
|
+
Note: This library is not turing complete, which means it's not for programming inside
|
170
|
+
of the template. For instance, filtering the indexes less than or greater than a value is
|
171
|
+
not appropriate to do inside of the template - you can filter the data before passing it in, using
|
172
|
+
your favourite library.
|
173
|
+
|
174
|
+
Having said that, there is a helper method, which translates as "only select the rows from this index
|
175
|
+
that match these other index keys":
|
176
|
+
|
177
|
+
```ruby
|
178
|
+
kpis.slice_from(names, 'Bob') # translates as select only Bob's KPIs
|
179
|
+
```
|
180
|
+
|
181
|
+
Also see `teer_spec.rb`. Markdown can be written, returning a result such as:
|
182
|
+
|
183
|
+
Behaviour change was worst for respondents who selected:
|
184
|
+
|
185
|
+
- `West` for `regUS`
|
186
|
+
- `Female` for `gender`
|
187
|
+
|
188
|
+
for `Would you change your response to Apple?`
|
189
|
+
|
190
|
+
### Importing Google Sheets rules into Teer
|
191
|
+
|
192
|
+
Our use-case internally was to pull in 'rules' from Google Sheets and alongside the data, display key findings. Using this library you can easily achieve this. Let's say you have wide data with many columns and you don't care about the indexes:
|
193
|
+
|
194
|
+
| | banana | apple | pear | orange |
|
195
|
+
|------------|--------|-------|-------|--------|
|
196
|
+
| person_id | | | | |
|
197
|
+
| a | 0.3 | 0.5 | 1 | 0.45 |
|
198
|
+
| b | 0.5 | 0.1 | 2 | 0.1 |
|
199
|
+
| c | 0.8 | 0.7 | 5 | 0.6 |
|
200
|
+
| d | 0.9 | 0.7 | 9 | 10 |
|
201
|
+
|
202
|
+
``` ruby
|
203
|
+
data = [
|
204
|
+
{'person_id' => 'a', 'banana' => 0.3, 'apple' => 0.5, 'pear' => 1, 'orange' => 0.45},
|
205
|
+
{'person_id' => 'b', 'banana' => 0.5, 'apple' => 0.1, 'pear' => 2, 'orange' => 0.1},
|
206
|
+
{'person_id' => 'c', 'banana' => 0.8, 'apple' => 0.7, 'pear' => 5, 'orange' => 0.6},
|
207
|
+
{'person_id' => 'd', 'banana' => 0.9, 'apple' => 0.7, 'pear' => 9, 'orange' => 10}
|
208
|
+
]
|
209
|
+
```
|
210
|
+
|
211
|
+
Google Sheets rules in 2d array format [[condition, text], ...]:
|
212
|
+
|
213
|
+
|
214
|
+
``` ruby
|
215
|
+
rules = [
|
216
|
+
['bananas.mean > 2', 'Lots of bananas'],
|
217
|
+
['bananas.mean > 1 && bananas.mean < 2', 'Not that many bananas'],
|
218
|
+
['bananas.mean < 1', 'No bananas'],
|
219
|
+
.
|
220
|
+
.
|
221
|
+
.
|
222
|
+
]
|
223
|
+
```
|
224
|
+
* N.B. because there are multiple columns, the variables are accessed as the column name with an `s` on the end (plural columns will return an error) and to access the index data now, you have to access it by the column name e.g. `banana.person_ids.slice("a").value # = 0.3`
|
225
|
+
|
226
|
+
The results of which can be calculated by doing
|
227
|
+
``` ruby
|
228
|
+
template = Teer::Template.create(data, data.first.keys - ['person_id'], rules)
|
229
|
+
template.findings # = ['No bananas'...]
|
230
|
+
```
|
231
|
+
|
232
|
+
#### Helpers | Formatters
|
233
|
+
|
234
|
+
To format variables as the template is interpolated, helpers are used to do things such as round numbers of format time. They take the form of `{{METHOD VARIABLE}}`
|
235
|
+
|
236
|
+
Examples below:
|
237
|
+
|
238
|
+
```ruby
|
239
|
+
horrible_floats_and_time = [
|
240
|
+
{ 'time' => Time.new(1993, 02, 24, 12, 0, 0, "+09:00"), 'name' => 'Bob', 'count' => 4.213432 },
|
241
|
+
{ 'time' => Time.new(1993, 02, 24, 12, 0, 0, "+09:00"), 'name' => 'Alan', 'count' => 14.35 },
|
242
|
+
{ 'time' => Time.new(1993, 02, 24, 12, 0, 0, "+09:00"), 'name' => 'Jeff', 'count' => 2.1 }
|
243
|
+
]
|
244
|
+
|
245
|
+
text = '{{round best_value }}'
|
246
|
+
template = { 'best_value' => 'names.sort[0].value', 'text' => { 'GB_en' => text } }
|
247
|
+
Teer::Template.create(horrible_floats_and_time, 'count', template).finding
|
248
|
+
=> "14.4"
|
249
|
+
|
250
|
+
text = '{{month month_key }}'
|
251
|
+
template = { 'month_key' => 'times.sort[0].key', 'text' => { 'GB_en' => text } }
|
252
|
+
Teer::Template.create(horrible_floats_and_time, 'count', template).finding
|
253
|
+
=> "February"
|
254
|
+
```
|
255
|
+
|
256
|
+
Although only those two currently come out of the box, it's easy to add more when your app initialises:
|
257
|
+
|
258
|
+
```ruby
|
259
|
+
Teer::Template.handlebars.register_helper(:year) do |_context, condition, _block|
|
260
|
+
Time.at(condition).strftime('%Y')
|
261
|
+
end
|
262
|
+
```
|
263
|
+
|
264
|
+
See [here](https://github.com/cowboyd/handlebars.rb) for more advanced usage. If you feel the helper will benefit all, please submit a PR!
|
265
|
+
|
266
|
+
## Development
|
267
|
+
|
268
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
269
|
+
|
270
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
271
|
+
|
272
|
+
## Contributing
|
273
|
+
|
274
|
+
Bug reports and pull requests are welcome on GitHub at <https://github.com/Rambatino/teer>. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
275
|
+
|
276
|
+
## License
|
277
|
+
|
278
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
279
|
+
|
280
|
+
## Code of Conduct
|
281
|
+
|
282
|
+
Everyone interacting in the Teer project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/Rambatino/teer/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "teer"
|
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
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
require "pry"
|
11
|
+
Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,124 @@
|
|
1
|
+
require_relative 'vector_store'
|
2
|
+
|
3
|
+
module Teer
|
4
|
+
class DataStore
|
5
|
+
def initialize(data, locale)
|
6
|
+
@data = data
|
7
|
+
@locale = locale
|
8
|
+
end
|
9
|
+
|
10
|
+
def key
|
11
|
+
@data.transpose[0][0]
|
12
|
+
end
|
13
|
+
|
14
|
+
def value
|
15
|
+
@data.transpose[1][0]
|
16
|
+
end
|
17
|
+
|
18
|
+
def keys
|
19
|
+
VectorStore.new(@data.transpose[0], @locale)
|
20
|
+
end
|
21
|
+
|
22
|
+
def values
|
23
|
+
VectorStore.new(@data.transpose[1], @locale)
|
24
|
+
end
|
25
|
+
|
26
|
+
def uniq
|
27
|
+
VectorStore.new(@data.transpose[0].uniq, @locale)
|
28
|
+
end
|
29
|
+
|
30
|
+
def group_count
|
31
|
+
DataStore.new(@data.transpose[0].each_with_object(Hash.new(0)) { |key, hsh| hsh[key] += 1 }.to_a, @locale)
|
32
|
+
end
|
33
|
+
|
34
|
+
def count
|
35
|
+
@data.count
|
36
|
+
end
|
37
|
+
|
38
|
+
def max
|
39
|
+
VectorStore.new(@data.max_by(&:last), @locale)
|
40
|
+
end
|
41
|
+
|
42
|
+
def min
|
43
|
+
VectorStore.new(@data.min_by(&:last), @locale)
|
44
|
+
end
|
45
|
+
|
46
|
+
def [](i)
|
47
|
+
VectorStore.new(@data[i], @locale)
|
48
|
+
end
|
49
|
+
|
50
|
+
def sort
|
51
|
+
DataStore.new(@data.sort_by(&:last).reverse, @locale)
|
52
|
+
end
|
53
|
+
|
54
|
+
def pluck_by_value(value)
|
55
|
+
DataStore.new(@data.select { |_, val| val == value }, @locale)
|
56
|
+
end
|
57
|
+
|
58
|
+
def first
|
59
|
+
VectorStore.new(@data[0], @locale)
|
60
|
+
end
|
61
|
+
|
62
|
+
def second
|
63
|
+
VectorStore.new(@data[1], @locale)
|
64
|
+
end
|
65
|
+
|
66
|
+
def third
|
67
|
+
VectorStore.new(@data[2], @locale)
|
68
|
+
end
|
69
|
+
|
70
|
+
def fourth
|
71
|
+
VectorStore.new(@data[3], @locale)
|
72
|
+
end
|
73
|
+
|
74
|
+
def last
|
75
|
+
VectorStore.new(@data[-1], @locale)
|
76
|
+
end
|
77
|
+
|
78
|
+
def eq(num)
|
79
|
+
DataStore.new(@data.select { |a| a[-1] == num }, @locale)
|
80
|
+
end
|
81
|
+
|
82
|
+
def gt(num)
|
83
|
+
DataStore.new(@data.select { |a| a[-1] > num }, @locale)
|
84
|
+
end
|
85
|
+
|
86
|
+
def lt(num)
|
87
|
+
DataStore.new(@data.select { |a| a[-1] < num }, @locale)
|
88
|
+
end
|
89
|
+
|
90
|
+
def ne(num)
|
91
|
+
DataStore.new(@data.reject { |a| a[-1] == num }, @locale)
|
92
|
+
end
|
93
|
+
|
94
|
+
def slice(key)
|
95
|
+
DataStore.new(@data.select { |a| a[0] == key }, @locale)
|
96
|
+
end
|
97
|
+
|
98
|
+
def slice_from(data_store, key)
|
99
|
+
idx = data_store.data.map { |a| a[0] == key }
|
100
|
+
DataStore.new(@data.select.with_index { |_a, i| idx[i] }, @locale)
|
101
|
+
end
|
102
|
+
|
103
|
+
def map(*others)
|
104
|
+
@data.each_with_index.map do |a, i|
|
105
|
+
yield(VectorStore.new(a, @locale),
|
106
|
+
*(others || []).map { |other| VectorStore.new(other.data[i], @locale) })
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
def to_s
|
111
|
+
"DataStore.new(#{@data}, #{@locale})"
|
112
|
+
end
|
113
|
+
|
114
|
+
def eql(value) # for equality checks
|
115
|
+
DataStore.new(@data.select { |a| a[1] == value }, @locale)
|
116
|
+
end
|
117
|
+
|
118
|
+
attr_reader :data
|
119
|
+
|
120
|
+
def as_json(options = nil)
|
121
|
+
@data.as_json(options)
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
data/lib/teer/engine.rb
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_support/inflector'
|
4
|
+
require 'teer/data_store'
|
5
|
+
require 'teer/vector_store'
|
6
|
+
require 'ostruct'
|
7
|
+
|
8
|
+
module Teer
|
9
|
+
class Engine
|
10
|
+
def initialize(data, names, template, handlebars, locale, kwargs)
|
11
|
+
@template = template
|
12
|
+
@handlebars = handlebars
|
13
|
+
@locale = locale
|
14
|
+
@findings = []
|
15
|
+
setup_store(data, names)
|
16
|
+
@data_store = @store.clone
|
17
|
+
kwargs.each { |k, v| @store[k] = v }
|
18
|
+
@store.interpolate = interpolate
|
19
|
+
end
|
20
|
+
|
21
|
+
def setup_store(data, names)
|
22
|
+
n_arr = Array(names)
|
23
|
+
if n_arr.count == 1
|
24
|
+
@store = OpenStruct.new((data[0].keys - n_arr).map do |idx|
|
25
|
+
[idx.pluralize, DataStore.new(data.map { |r| [r[idx], r[n_arr[0]]] }, @locale)]
|
26
|
+
end.to_h)
|
27
|
+
else
|
28
|
+
@store = OpenStruct.new.tap do |struct|
|
29
|
+
idxs = (data[0].keys - n_arr)
|
30
|
+
names.each do |name|
|
31
|
+
raise ArgumentError, "column name cannot be plural: #{name}" if name == name.pluralize
|
32
|
+
struct[name] = OpenStruct.new(idxs.map do |idx|
|
33
|
+
[idx.pluralize, DataStore.new(data.map { |r| [r[idx], r[name]] }, @locale)]
|
34
|
+
end.to_h)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
n_arr.each do |name|
|
39
|
+
@store[name.pluralize] = VectorStore.new(data.map { |r| r[name] }, @locale)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def finding
|
44
|
+
@finding ||= parse_template(@template)
|
45
|
+
end
|
46
|
+
|
47
|
+
def findings
|
48
|
+
@finding ||= parse_template(@template)
|
49
|
+
@findings
|
50
|
+
end
|
51
|
+
|
52
|
+
def pre_parsed_finding
|
53
|
+
@finding ||= parse_template(@template)
|
54
|
+
@pre_parsed_finding
|
55
|
+
end
|
56
|
+
|
57
|
+
def data
|
58
|
+
@data ||= @data_store
|
59
|
+
end
|
60
|
+
|
61
|
+
def value_for_store_key(key)
|
62
|
+
finding if @finding.nil?
|
63
|
+
@store[key]
|
64
|
+
end
|
65
|
+
|
66
|
+
def interpolate
|
67
|
+
@interpolate ||= proc { |string| @handlebars.compile(string).call(@store.to_h) }
|
68
|
+
end
|
69
|
+
|
70
|
+
def add_to_store(template)
|
71
|
+
template.each do |k, v|
|
72
|
+
if v.is_a?(String) && k != 'text'
|
73
|
+
@store[k] = eval(v, @store.instance_eval { binding })
|
74
|
+
elsif !!v == v
|
75
|
+
@store[k] = v
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def parse_template(template)
|
81
|
+
text = ''
|
82
|
+
pre_parsed_text = ''
|
83
|
+
add_to_store(template)
|
84
|
+
template.each do |k, v|
|
85
|
+
new_text = nil
|
86
|
+
unparsed_new_text = nil
|
87
|
+
if k == 'text' && (!v.nil? && !v.empty?)
|
88
|
+
@findings << (new_text = interpolate.call(v[@locale.to_s]))
|
89
|
+
unparsed_new_text = v[@locale.to_s]
|
90
|
+
elsif v.is_a?(Hash) && eval(k, @store.instance_eval { binding })
|
91
|
+
new_text = parse_template(v)
|
92
|
+
end
|
93
|
+
text += new_text + ' ' if new_text
|
94
|
+
pre_parsed_text += unparsed_new_text + ' ' if unparsed_new_text
|
95
|
+
end
|
96
|
+
@pre_parsed_finding = CGI.unescapeHTML(pre_parsed_text[0...-1]) if !pre_parsed_text.nil? && !pre_parsed_text.empty?
|
97
|
+
CGI.unescapeHTML(text[0...-1]) if !text.nil? && !text.empty?
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module Teer
|
2
|
+
class VectorStore
|
3
|
+
def initialize(data, locale)
|
4
|
+
@data = data
|
5
|
+
@locale = locale || :GB_en
|
6
|
+
end
|
7
|
+
|
8
|
+
def key
|
9
|
+
@data[0]
|
10
|
+
end
|
11
|
+
|
12
|
+
def value
|
13
|
+
@data[1]
|
14
|
+
end
|
15
|
+
|
16
|
+
def count
|
17
|
+
@data.count
|
18
|
+
end
|
19
|
+
|
20
|
+
def sum
|
21
|
+
@data.reduce(:+)
|
22
|
+
end
|
23
|
+
|
24
|
+
def mean
|
25
|
+
@data.reduce(:+) / @data.count
|
26
|
+
end
|
27
|
+
|
28
|
+
def to_s
|
29
|
+
length = @data.length
|
30
|
+
case length
|
31
|
+
when 0
|
32
|
+
''
|
33
|
+
when 1
|
34
|
+
@data[0]
|
35
|
+
else
|
36
|
+
sentence = @data.join(', ')
|
37
|
+
# should support all languages at some point
|
38
|
+
sentence.reverse.sub(' ,', ' dna ').reverse
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
attr_reader :data
|
43
|
+
|
44
|
+
def as_json(options = nil)
|
45
|
+
@data.as_json(options)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
data/lib/teer/version.rb
ADDED
data/lib/teer.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'teer/version'
|
2
|
+
require 'teer/engine'
|
3
|
+
require 'handlebars'
|
4
|
+
require 'yaml'
|
5
|
+
|
6
|
+
module Teer
|
7
|
+
class Error < StandardError; end
|
8
|
+
|
9
|
+
class Template
|
10
|
+
def self.create(data, names, rules_path_or_obj, kwargs = {}, locale = :GB_en)
|
11
|
+
return OpenStruct.new(data: nil, finding: nil) if data.empty?
|
12
|
+
register_helpers
|
13
|
+
data_keys = data.first.keys
|
14
|
+
raise ArgumentError, "#{names} not present in data" if (Array(names) - data_keys).count != 0
|
15
|
+
@template = Engine.new(data, names, template(rules_path_or_obj, locale), handlebars, locale, kwargs)
|
16
|
+
end
|
17
|
+
|
18
|
+
def pre_parsed_finding
|
19
|
+
@template.pre_parsed_finding
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.template(rules_path_or_obj, locale)
|
23
|
+
case rules_path_or_obj
|
24
|
+
when String
|
25
|
+
YAML.safe_load(File.read(rules_path_or_obj))
|
26
|
+
when Array
|
27
|
+
rules_path_or_obj.map { |condition, text| [condition, { 'text' => { locale.to_s => text } } ] }.to_h
|
28
|
+
when Hash
|
29
|
+
return rules_path_or_obj
|
30
|
+
else
|
31
|
+
raise ArgumentError, "Unknown template structure: #{rules_path_or_obj.class}"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.handlebars
|
36
|
+
@handlebars ||= Handlebars::Context.new
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.register_helpers
|
40
|
+
handlebars.register_helper(:round) do |_context, condition, _block|
|
41
|
+
condition < 1 ? condition.round(2) : condition.round(1)
|
42
|
+
end
|
43
|
+
handlebars.register_helper(:month) do |_context, condition, _block|
|
44
|
+
Time.at(condition).strftime('%B')
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
data/teer.gemspec
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'teer/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'teer'
|
8
|
+
spec.version = Teer::VERSION
|
9
|
+
spec.authors = ['Mark Ramotowski']
|
10
|
+
spec.email = ['mark.tint.ramotowski@gmail.com']
|
11
|
+
|
12
|
+
spec.summary = 'Create human readable summation of data'
|
13
|
+
spec.description = 'Turns data into human readable format using templates with simple logic statements'
|
14
|
+
spec.homepage = 'https://www.github.com/Rambatino/teer'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
# if spec.respond_to?(:metadata)
|
20
|
+
# spec.metadata['allowed_push_host'] = 'http://mygemserver.com'
|
21
|
+
#
|
22
|
+
# spec.metadata['homepage_uri'] = spec.homepage
|
23
|
+
# spec.metadata['source_code_uri'] = spec.homepage
|
24
|
+
# spec.metadata['changelog_uri'] = spec.homepage + '/blob/master/CHANGELOG.md'
|
25
|
+
# else
|
26
|
+
# raise 'RubyGems 2.0 or newer is required to protect against ' \
|
27
|
+
# 'public gem pushes.'
|
28
|
+
# end
|
29
|
+
|
30
|
+
# Specify which files should be added to the gem when it is released.
|
31
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
32
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
33
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
34
|
+
end
|
35
|
+
spec.bindir = 'exe'
|
36
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
37
|
+
spec.require_paths = ['lib']
|
38
|
+
spec.add_dependency 'handlebars'
|
39
|
+
spec.add_dependency 'activesupport'
|
40
|
+
spec.add_development_dependency 'bundler', '~> 1.17'
|
41
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
42
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
43
|
+
spec.add_development_dependency 'pry'
|
44
|
+
spec.add_development_dependency 'rubocop'
|
45
|
+
end
|
metadata
ADDED
@@ -0,0 +1,158 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: teer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mark Ramotowski
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-05-28 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: handlebars
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activesupport
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.17'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.17'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: pry
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rubocop
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
description: Turns data into human readable format using templates with simple logic
|
112
|
+
statements
|
113
|
+
email:
|
114
|
+
- mark.tint.ramotowski@gmail.com
|
115
|
+
executables: []
|
116
|
+
extensions: []
|
117
|
+
extra_rdoc_files: []
|
118
|
+
files:
|
119
|
+
- ".gitignore"
|
120
|
+
- ".rspec"
|
121
|
+
- ".travis.yml"
|
122
|
+
- CODE_OF_CONDUCT.md
|
123
|
+
- Gemfile
|
124
|
+
- LICENSE.txt
|
125
|
+
- README.md
|
126
|
+
- Rakefile
|
127
|
+
- bin/console
|
128
|
+
- bin/setup
|
129
|
+
- lib/teer.rb
|
130
|
+
- lib/teer/data_store.rb
|
131
|
+
- lib/teer/engine.rb
|
132
|
+
- lib/teer/vector_store.rb
|
133
|
+
- lib/teer/version.rb
|
134
|
+
- teer.gemspec
|
135
|
+
homepage: https://www.github.com/Rambatino/teer
|
136
|
+
licenses:
|
137
|
+
- MIT
|
138
|
+
metadata: {}
|
139
|
+
post_install_message:
|
140
|
+
rdoc_options: []
|
141
|
+
require_paths:
|
142
|
+
- lib
|
143
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
144
|
+
requirements:
|
145
|
+
- - ">="
|
146
|
+
- !ruby/object:Gem::Version
|
147
|
+
version: '0'
|
148
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - ">="
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0'
|
153
|
+
requirements: []
|
154
|
+
rubygems_version: 3.0.2
|
155
|
+
signing_key:
|
156
|
+
specification_version: 4
|
157
|
+
summary: Create human readable summation of data
|
158
|
+
test_files: []
|