pluck_map 0.2.1 → 0.3.0

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
- SHA1:
3
- metadata.gz: 7b3bad6e2e72b21e54b69aa8fcfe417ad05c6461
4
- data.tar.gz: 637341da36aecba00cc754f48384475921f663c7
2
+ SHA256:
3
+ metadata.gz: baee1fa797afc235f781a0b9d2492a8796ff54b3e90fd795bda2fd772c70a146
4
+ data.tar.gz: a90a51ca0fb57d382f00228f31ace8e2157153aa4bf8f29f75589a6b6b6812f9
5
5
  SHA512:
6
- metadata.gz: 9be09ab2988c556b85a383f60e166c5628065b0bc061ceeab3e4c125e0499b22b91be8cbdcee9724d02db97c4fb3ba5986c143e42c9df74f0003a271372cc39a
7
- data.tar.gz: 6a03d1b833c76b944a6c6d0271222839bc1a483da2c89f90e39f2fe9eb62833e08d162ffe7e06d32afb01bf478c30cd13476c29da8f2ebffb24c4e39b16ad4a0
6
+ metadata.gz: 8ca7ae6a27609c8e26edfe2f33d9929773b2fc1bba4ee193633def8f4f5637d6914654c36cfc67812fb1debae1d0095fa04636c4734e9b31f98f6582e16cbff2
7
+ data.tar.gz: cb720743428655889f2ea30e7e4de92f27a07564faf8190b18d6b44dc7165e971cbd7e027c252aedcb39ae85e1de8c16346f147142424c521ddeb71c2096c8dc
data/.gitignore CHANGED
@@ -1,6 +1,7 @@
1
1
  /.bundle/
2
2
  /.yardoc
3
3
  /Gemfile.lock
4
+ /gemfiles/*.gemfile.lock
4
5
  /_yardoc/
5
6
  /coverage/
6
7
  /doc/
@@ -1,4 +1,22 @@
1
1
  language: ruby
2
- rvm:
3
- - 2.1.2
4
- before_install: gem install bundler -v 1.10.6
2
+ rvm: 2.5.5
3
+
4
+ # Rails 6 requires Postgres 9.3+
5
+ addons:
6
+ postgresql: "9.3"
7
+
8
+ services:
9
+ - postgresql
10
+ - mysql
11
+
12
+ # Test on supported versions of Rails + Edge Rails
13
+ # (When Rails 6.0 is released, drop Rails 4.2)
14
+ matrix:
15
+ include:
16
+ - gemfile: gemfiles/rails_4.2.gemfile
17
+ - gemfile: gemfiles/rails_5.0.gemfile
18
+ - gemfile: gemfiles/rails_5.1.gemfile
19
+ - gemfile: gemfiles/rails_5.2.gemfile
20
+ - gemfile: gemfiles/rails_edge.gemfile
21
+ allow_failures:
22
+ - gemfile: gemfiles/rails_edge.gemfile
@@ -0,0 +1,25 @@
1
+ appraise "rails-4.2" do
2
+ gem "activerecord", "~> 4.2.0"
3
+ gem "sqlite3", "~> 1.3.6"
4
+ gem "pg", "~> 0.20"
5
+ end
6
+
7
+ appraise "rails-5.0" do
8
+ gem "activerecord", "~> 5.0.0"
9
+ gem "sqlite3", "~> 1.3.6"
10
+ end
11
+
12
+ appraise "rails-5.1" do
13
+ gem "activerecord", "~> 5.1.0"
14
+ gem "sqlite3", "~> 1.3.6"
15
+ end
16
+
17
+ appraise "rails-5.2" do
18
+ gem "activerecord", "~> 5.2.0"
19
+ gem "sqlite3", "~> 1.3.6"
20
+ end
21
+
22
+ appraise "rails-edge" do
23
+ gem "rails", git: "https://github.com/rails/rails.git", branch: "master", require: "activerecord"
24
+ gem "sqlite3", "~> 1.4.0"
25
+ end
@@ -0,0 +1,15 @@
1
+ ## v0.3.0 (2019 Apr 7)
2
+
3
+ * IMPROVEMENT: Add support for MySQL (@boblail)
4
+
5
+ ## v0.2.1 (2018 Mar 29)
6
+
7
+ * FIX: Allow static values to be falsey (@kobsy)
8
+
9
+ ## v0.2.0 (2018 Mar 28)
10
+
11
+ * FIX: Allow static values to be falsey (@kobsy)
12
+
13
+ ## v0.1.0 (2015 Nov 7)
14
+
15
+ * Initial version (@boblail)
data/README.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # PluckMap::Presenter
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/pluck_map.svg)](https://rubygems.org/gems/pluck_map)
4
+ [![Build Status](https://travis-ci.org/boblail/pluck_map.svg)](https://travis-ci.org/boblail/pluck_map)
5
+
3
6
  The PluckMap presenter provides a DSL for creating performant presenters. 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).
4
7
 
5
8
  Let's take an example. Suppose you have an action like this:
@@ -108,4 +111,3 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
108
111
  ## Contributing
109
112
 
110
113
  Bug reports and pull requests are welcome on GitHub at https://github.com/boblail/pluck_map.
111
-
data/Rakefile CHANGED
@@ -1 +1,61 @@
1
1
  require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ namespace :db do
5
+ namespace :mysql do
6
+ task :env do
7
+ system "mysql -e 'drop database if exists pluck_map_test'"
8
+ system "mysql -e 'create database pluck_map_test'"
9
+ ENV["ACTIVE_RECORD_ADAPTER"] = "mysql2"
10
+ end
11
+ end
12
+
13
+ namespace :postgres do
14
+ task :env do
15
+ system "psql -c 'drop database if exists pluck_map_test'"
16
+ system "psql -c 'create database pluck_map_test'"
17
+ ENV["ACTIVE_RECORD_ADAPTER"] = "postgresql"
18
+ end
19
+ end
20
+
21
+ namespace :sqlite do
22
+ task :env do
23
+ ENV["ACTIVE_RECORD_ADAPTER"] = "sqlite3"
24
+ end
25
+ end
26
+ end
27
+
28
+ ADAPTERS = Rake.application.tasks.each_with_object([]) do |task, adapters|
29
+ match = task.name.match(/db:(.*):env/)
30
+ adapters.push(match[1]) if match
31
+ end.freeze
32
+
33
+ namespace :test do
34
+ ADAPTERS.each do |adapter|
35
+ Rake::TestTask.new(adapter => "db:#{adapter}:env") do |t|
36
+ t.libs << "test"
37
+ t.libs << "lib"
38
+ t.test_files = FileList["test/**/*_test.rb"]
39
+ end end
40
+ end
41
+
42
+ def run_without_aborting(*tasks)
43
+ errors = []
44
+
45
+ tasks.each do |task|
46
+ Rake::Task[task].invoke
47
+ rescue Exception
48
+ errors << task
49
+ end
50
+
51
+ abort "Errors running #{errors.join(', ')}" if errors.any?
52
+ end
53
+
54
+ desc "Run #{ADAPTERS.join(', ')} tests"
55
+ task :test do
56
+ tasks = ADAPTERS.map { |adapter| "test:#{adapter}" }
57
+ run_without_aborting(*tasks)
58
+ end
59
+
60
+ desc "Run #{ADAPTERS.join(', ')} tests by default"
61
+ task default: :test
@@ -0,0 +1,2 @@
1
+ ---
2
+ BUNDLE_RETRY: "1"
@@ -0,0 +1,9 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", "~> 4.2.0"
6
+ gem "sqlite3", "~> 1.3.6"
7
+ gem "pg", "~> 0.20"
8
+
9
+ gemspec path: "../"
@@ -0,0 +1,8 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", "~> 5.0.0"
6
+ gem "sqlite3", "~> 1.3.6"
7
+
8
+ gemspec path: "../"
@@ -0,0 +1,8 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", "~> 5.1.0"
6
+ gem "sqlite3", "~> 1.3.6"
7
+
8
+ gemspec path: "../"
@@ -0,0 +1,8 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", "~> 5.2.0"
6
+ gem "sqlite3", "~> 1.3.6"
7
+
8
+ gemspec path: "../"
@@ -0,0 +1,8 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rails", git: "https://github.com/rails/rails.git", branch: "master", require: "activerecord"
6
+ gem "sqlite3", "~> 1.4.0"
7
+
8
+ gemspec path: "../"
@@ -0,0 +1,6 @@
1
+ module PluckMap
2
+ class NullLogger
3
+ def info(*)
4
+ end
5
+ end
6
+ end
@@ -1,10 +1,16 @@
1
1
  require "pluck_map/version"
2
2
  require "pluck_map/attribute"
3
+ require "pluck_map/null_logger"
3
4
 
4
5
  module PluckMap
5
6
  class Presenter
6
7
  attr_reader :attributes
7
8
 
9
+ @logger = defined?(Rails) ? Rails.logger : PluckMap::NullLogger.new
10
+ class << self
11
+ attr_accessor :logger
12
+ end
13
+
8
14
  def initialize(&block)
9
15
  @attributes = []
10
16
  if block.arity == 1
@@ -44,7 +50,7 @@ module PluckMap
44
50
 
45
51
  def pluck(query)
46
52
  # puts "\e[95m#{query.select(*selects(query.table_name)).to_sql}\e[0m"
47
- results = benchmark("pluck(#{query.table_name})") { query.pluck(*selects(query.table_name)) }
53
+ results = benchmark("pluck(#{query.table_name})") { query.pluck(*selects(query.model)) }
48
54
  return results unless block_given?
49
55
  benchmark("map(#{query.table_name})") { yield results }
50
56
  end
@@ -52,21 +58,21 @@ module PluckMap
52
58
  def benchmark(title)
53
59
  result = nil
54
60
  ms = Benchmark.ms { result = yield }
55
- Rails.logger.info "\e[33m#{title}: \e[1m%.1fms\e[0m" % ms
61
+ self.class.logger.info "\e[33m#{title}: \e[1m%.1fms\e[0m" % ms
56
62
  result
57
63
  end
58
64
 
59
65
  private
60
66
  attr_reader :attributes_by_id, :keys
61
67
 
62
- def selects(table_name)
68
+ def selects(model)
63
69
  attributes.flat_map do |attribute|
64
70
  if attribute.selects.length != 1
65
71
  attribute.selects
66
72
  else
67
73
  select = attribute.selects[0]
68
- select = "\"#{table_name}\".\"#{select}\"" if select.is_a?(Symbol)
69
- "#{select} AS \"#{attribute.alias}\""
74
+ select = "#{model.quoted_table_name}.#{model.connection.quote_column_name(select)}" if select.is_a?(Symbol)
75
+ Arel.sql("#{select} AS \"#{attribute.alias}\"")
70
76
  end
71
77
  end.uniq
72
78
  end
@@ -1,3 +1,3 @@
1
1
  module PluckMapPresenter
2
- VERSION = "0.2.1"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -7,7 +7,7 @@ Gem::Specification.new do |spec|
7
7
  spec.name = "pluck_map"
8
8
  spec.version = PluckMapPresenter::VERSION
9
9
  spec.authors = ["Bob Lail"]
10
- spec.email = ["bob.lail@cph.org"]
10
+ spec.email = ["bob.lailfamily@gmail.com"]
11
11
 
12
12
  spec.summary = "A DSL for presenting ActiveRecord::Relations without instantiating ActiveRecord models"
13
13
  spec.homepage = "https://github.com/boblail/pluck_map"
@@ -17,6 +17,17 @@ Gem::Specification.new do |spec|
17
17
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
18
  spec.require_paths = ["lib"]
19
19
 
20
- spec.add_development_dependency "bundler", "~> 1.10"
20
+ spec.add_dependency "activerecord"
21
+
22
+ spec.add_development_dependency "appraisal"
23
+ spec.add_development_dependency "bundler"
21
24
  spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "minitest-reporters"
26
+ spec.add_development_dependency "minitest-reporters-turn_reporter"
27
+ spec.add_development_dependency "sqlite3"
28
+ spec.add_development_dependency "mysql2"
29
+ spec.add_development_dependency "pg"
30
+ spec.add_development_dependency "database_cleaner"
31
+ spec.add_development_dependency "shoulda-context"
32
+ spec.add_development_dependency "pry"
22
33
  end
metadata CHANGED
@@ -1,29 +1,57 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pluck_map
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bob Lail
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-03-29 00:00:00.000000000 Z
11
+ date: 2019-04-07 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
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: appraisal
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
13
41
  - !ruby/object:Gem::Dependency
14
42
  name: bundler
15
43
  requirement: !ruby/object:Gem::Requirement
16
44
  requirements:
17
- - - "~>"
45
+ - - ">="
18
46
  - !ruby/object:Gem::Version
19
- version: '1.10'
47
+ version: '0'
20
48
  type: :development
21
49
  prerelease: false
22
50
  version_requirements: !ruby/object:Gem::Requirement
23
51
  requirements:
24
- - - "~>"
52
+ - - ">="
25
53
  - !ruby/object:Gem::Version
26
- version: '1.10'
54
+ version: '0'
27
55
  - !ruby/object:Gem::Dependency
28
56
  name: rake
29
57
  requirement: !ruby/object:Gem::Requirement
@@ -38,22 +66,143 @@ dependencies:
38
66
  - - "~>"
39
67
  - !ruby/object:Gem::Version
40
68
  version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest-reporters
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: minitest-reporters-turn_reporter
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: sqlite3
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
+ - !ruby/object:Gem::Dependency
112
+ name: mysql2
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: pg
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: database_cleaner
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: shoulda-context
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ - !ruby/object:Gem::Dependency
168
+ name: pry
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
41
181
  description:
42
182
  email:
43
- - bob.lail@cph.org
183
+ - bob.lailfamily@gmail.com
44
184
  executables: []
45
185
  extensions: []
46
186
  extra_rdoc_files: []
47
187
  files:
48
188
  - ".gitignore"
49
189
  - ".travis.yml"
190
+ - Appraisals
191
+ - CHANGELOG.md
50
192
  - Gemfile
51
193
  - README.md
52
194
  - Rakefile
53
195
  - bin/console
54
196
  - bin/setup
197
+ - gemfiles/.bundle/config
198
+ - gemfiles/rails_4.2.gemfile
199
+ - gemfiles/rails_5.0.gemfile
200
+ - gemfiles/rails_5.1.gemfile
201
+ - gemfiles/rails_5.2.gemfile
202
+ - gemfiles/rails_edge.gemfile
55
203
  - lib/pluck_map.rb
56
204
  - lib/pluck_map/attribute.rb
205
+ - lib/pluck_map/null_logger.rb
57
206
  - lib/pluck_map/presenter.rb
58
207
  - lib/pluck_map/version.rb
59
208
  - pluck_map.gemspec
@@ -76,7 +225,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
76
225
  version: '0'
77
226
  requirements: []
78
227
  rubyforge_project:
79
- rubygems_version: 2.6.11
228
+ rubygems_version: 2.7.6
80
229
  signing_key:
81
230
  specification_version: 4
82
231
  summary: A DSL for presenting ActiveRecord::Relations without instantiating ActiveRecord