pluck_map 0.6.1 → 0.6.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +80 -3
- data/lib/pluck_map/presenter.rb +1 -1
- data/lib/pluck_map/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d6713a4fb63be352fb7c8d54bbbdb7a559fc4b523093f3cff0a72b9507803a8e
|
4
|
+
data.tar.gz: e7f9e9caea6745daeb1beab73c711fd87656309fd3221422ad027072d6fcd8d7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ca03d1e1f7e82934ee018c93ac980b7f35c8d9805782de196cb1941b36992880ef19f193d8d9055ac03a3999cf58168503e65ccd74fd10a59d806178c2d8950f
|
7
|
+
data.tar.gz: 48822bd995380e8a97e5166c94f890b010823b55227884c66f06e6d4d36b6baf22c9b577aa3acf9670d2cca186c4dac3ccb6ee165b8c27e7861f89d31c4ba5ce
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -5,7 +5,19 @@
|
|
5
5
|
|
6
6
|
This library provides a DSL for presenting ActiveRecord::Relations without instantiating ActiveRecord models. It is useful when a Rails controller action does little more than fetch several records from the database and present them in some other data format (like JSON or CSV).
|
7
7
|
|
8
|
-
|
8
|
+
|
9
|
+
### Table of Contents
|
10
|
+
|
11
|
+
- [Why PluckMap?](https://github.com/boblail/pluck_map#why-pluckmap)
|
12
|
+
- Usage
|
13
|
+
- [Defining attributes to present](https://github.com/boblail/pluck_map#defining-attributes-to-present)
|
14
|
+
- [Presenting Records](https://github.com/boblail/pluck_map#presenting-records)
|
15
|
+
- [Installation](https://github.com/boblail/pluck_map#installation)
|
16
|
+
- [Requirements](https://github.com/boblail/pluck_map#requirements)
|
17
|
+
- [Development & Contributing](https://github.com/boblail/pluck_map#development)
|
18
|
+
|
19
|
+
|
20
|
+
## Why PluckMap?
|
9
21
|
|
10
22
|
Suppose you have an action like this:
|
11
23
|
|
@@ -96,7 +108,7 @@ Jbuilder gives you a similar DSL for defining JSON to be presented but it operat
|
|
96
108
|
|
97
109
|
## Usage
|
98
110
|
|
99
|
-
### Attributes
|
111
|
+
### Defining Attributes to Present
|
100
112
|
|
101
113
|
#### Syntax
|
102
114
|
|
@@ -196,6 +208,70 @@ end
|
|
196
208
|
```
|
197
209
|
|
198
210
|
|
211
|
+
### Presenting Records
|
212
|
+
|
213
|
+
#### `:to_h`
|
214
|
+
|
215
|
+
Once you've defined a presenter, pass an `ActiveRecord::Relation` to `to_h` to get an array of hashes:
|
216
|
+
|
217
|
+
```ruby
|
218
|
+
presenter = PluckMap[Person].define do
|
219
|
+
id
|
220
|
+
type value: "Person"
|
221
|
+
end
|
222
|
+
|
223
|
+
presenter.to_h(Person.where(id: 1)) # => [{ id: 1, type: "Person" }]
|
224
|
+
```
|
225
|
+
|
226
|
+
#### `:to_json` and `:to_csv`
|
227
|
+
|
228
|
+
You can `.map` that array to construct whatever document you need, but PluckMap implements two methods that are optimized for generating JSON and CSV:
|
229
|
+
|
230
|
+
```ruby
|
231
|
+
presenter.to_json(Person.where(id: 1)) # => '[{"id":1,"type":"Person"}]'
|
232
|
+
presenter.to_csv(Person.where(id: 1)) # => "id,type\n1,Person"
|
233
|
+
```
|
234
|
+
|
235
|
+
#### Custom Presenters
|
236
|
+
|
237
|
+
You can define new (or override existing) presenter methods by mixing modules into `PluckMap::Presenter`. Here's an example of how you might create a presenter that produces an Excel document using an imaginary `Excel::Document` library:
|
238
|
+
|
239
|
+
```ruby
|
240
|
+
module PluckToXlsxPresenter
|
241
|
+
def to_excel(query)
|
242
|
+
# Every presenter method accepts an ActiveRecord::Relation
|
243
|
+
# and passes it to `pluck` which yields the results.
|
244
|
+
pluck(query) do |results|
|
245
|
+
|
246
|
+
# Use an imaginary Excel gem that has an Excel::Document object
|
247
|
+
spreadsheet = Excel::Document.new
|
248
|
+
|
249
|
+
# Fill in a Header row
|
250
|
+
# `attributes` is a method on `PluckMap::Presenter` that describes
|
251
|
+
# the attributes you defined when you constructed the presenter.
|
252
|
+
attributes.each_with_index do |attribute, i|
|
253
|
+
spreadsheet.cell[0, i] = attribute.name
|
254
|
+
end
|
255
|
+
|
256
|
+
# Results is an array of rows (Rows are an array of values)
|
257
|
+
results.each_with_index do |values, row_number|
|
258
|
+
attributes.each_with_index do |attribute, column_number|
|
259
|
+
|
260
|
+
# `attribute.exec` will pick the right values from the row
|
261
|
+
# and perform any required processing.
|
262
|
+
spreadsheet.cell[row_number + 1, column_number] = attribute.exec(values)
|
263
|
+
end
|
264
|
+
end
|
265
|
+
|
266
|
+
spreadsheet.render # `pluck` returns the result of the block
|
267
|
+
end
|
268
|
+
end
|
269
|
+
end
|
270
|
+
|
271
|
+
PluckMap::Presenter.send :include, PluckToXlsxPresenter
|
272
|
+
```
|
273
|
+
|
274
|
+
|
199
275
|
|
200
276
|
## Installation
|
201
277
|
|
@@ -214,7 +290,8 @@ Or install it yourself as:
|
|
214
290
|
$ gem install pluck_map
|
215
291
|
|
216
292
|
|
217
|
-
|
293
|
+
|
294
|
+
## Requirements
|
218
295
|
|
219
296
|
The gem's only runtime requirement is:
|
220
297
|
|
data/lib/pluck_map/presenter.rb
CHANGED
data/lib/pluck_map/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pluck_map
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.6.
|
4
|
+
version: 0.6.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bob Lail
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-06-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|