pluck_map 0.6.1 → 0.6.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 719535934f95bc4fe3db48d59fb8f8c762f23260c6438763672612d82ea12feb
4
- data.tar.gz: ada8d0481118adc7d04f47b3b801e826e4b07a60d07d0d063ef10a652c891568
3
+ metadata.gz: d6713a4fb63be352fb7c8d54bbbdb7a559fc4b523093f3cff0a72b9507803a8e
4
+ data.tar.gz: e7f9e9caea6745daeb1beab73c711fd87656309fd3221422ad027072d6fcd8d7
5
5
  SHA512:
6
- metadata.gz: 9d35814e0ef40d24e7ff441250c692880116540f6668ef79ce047da3424e7ff98203964e013f81c4d32d100d4f29e3e3995beb74f7f166ffa4597e5653e893f3
7
- data.tar.gz: c887937585d78ef34a9d44e7fedf9ad7810ba9dd5d54525b90c577ca77eaef9450575986adb43c5f9d97d874903a688bf3f81ab538f03b520d4fa849ce0bef7f
6
+ metadata.gz: ca03d1e1f7e82934ee018c93ac980b7f35c8d9805782de196cb1941b36992880ef19f193d8d9055ac03a3999cf58168503e65ccd74fd10a59d806178c2d8950f
7
+ data.tar.gz: 48822bd995380e8a97e5166c94f890b010823b55227884c66f06e6d4d36b6baf22c9b577aa3acf9670d2cca186c4dac3ccb6ee165b8c27e7861f89d31c4ba5ce
@@ -1,3 +1,7 @@
1
+ ## v0.6.2 (2019 Jun 13)
2
+
3
+ * FIX: Allow presenting a subclass of the presenter's model (@kobsy)
4
+
1
5
  ## v0.6.1 (2019 May 12)
2
6
 
3
7
  * FIX: Aliased SQL expressions to work-around a bug with Rails Postgres adapter (@kobsy)
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
- ### Why?
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
- ### Requirements
293
+
294
+ ## Requirements
218
295
 
219
296
  The gem's only runtime requirement is:
220
297
 
@@ -33,7 +33,7 @@ module PluckMap
33
33
  protected
34
34
 
35
35
  def pluck(query)
36
- if model && query.model != model
36
+ unless model.nil? || query.model <= model
37
37
  raise ArgumentError, "Query for #{query.model} but #{model} expected"
38
38
  end
39
39
 
@@ -1,3 +1,3 @@
1
1
  module PluckMapPresenter
2
- VERSION = "0.6.1"
2
+ VERSION = "0.6.2"
3
3
  end
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.1
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-05-13 00:00:00.000000000 Z
11
+ date: 2019-06-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord