pluck_map 1.0.0 → 2.0.0.rc1

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
2
  SHA256:
3
- metadata.gz: d907de27fded6b459dbff8787f939019823dc589ab3922df031afbdc891a63c5
4
- data.tar.gz: 1282d91b22bebf374f39ac90f8cac489325c76b2abea949ce5c4c88343739228
3
+ metadata.gz: b67d33088429e33e5b52da0c68a9355dd0131d5404c9af3238cfc692b497dbad
4
+ data.tar.gz: 4af18c89e5e9fe10796cb8ee5cd3782e89e767f2104af016fc06a34edf0c5df9
5
5
  SHA512:
6
- metadata.gz: af29f7f130881691cd7ec5d49b6fbb781fb2f21c16bb4762682a77e00404098806f94b70e0fdce488966a52a6be2859a783fb3fdc4a94f2ed3912ec1b55dd220
7
- data.tar.gz: 9993639be1838a7653501b52df366f318b7b67032adb481e72d7ad675b0e470264af87fb4b16e834814d9e26856e8fd24582a0edf8429c243d27d3e4795fdfbe
6
+ metadata.gz: 7208cdebddc1813ead385a83fa08205868de2ba2b0b78c7470c7d9cd437b48867817c3c997ef24af117eb18e49a406f7a886221384dcaf8d5745ce7909ed97ec
7
+ data.tar.gz: 2f715ab9f2d5c687a42010d9176913201f9eaea23a795caf88fbc4b9e5448239ceacafc78632f197a632dfb376bff1b6afde0bad1326cc7bfefe384eea8df7ed
data/.travis.yml CHANGED
@@ -1,5 +1,5 @@
1
1
  language: ruby
2
- rvm: 2.5.5
2
+ rvm: 2.6.3
3
3
 
4
4
  # we need MySQL 5.7+ to support JSON aggregation
5
5
  dist: xenial
@@ -21,6 +21,7 @@ matrix:
21
21
  - gemfile: gemfiles/rails_5.0.gemfile
22
22
  - gemfile: gemfiles/rails_5.1.gemfile
23
23
  - gemfile: gemfiles/rails_5.2.gemfile
24
+ - gemfile: gemfiles/rails_6.0.gemfile
24
25
  - gemfile: gemfiles/rails_edge.gemfile
25
26
  allow_failures:
26
27
  - gemfile: gemfiles/rails_edge.gemfile
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## Unreleased
2
+
3
+ * BREAKING: `define` returns a subclass of `PluckMap::Presenter` instead of an instance (@boblail)
4
+ * FEATURE: `define` also creates structs to correspond to each presenter (@boblail)
5
+
1
6
  ## v1.0.0 (2019 Jul 17)
2
7
 
3
8
  * FIX: Respect default_scopes for relationships (@boblail)
@@ -37,6 +37,10 @@ module PluckMap
37
37
 
38
38
 
39
39
 
40
+ def ids
41
+ _attributes_by_id.keys
42
+ end
43
+
40
44
  def by_id
41
45
  _attributes_by_id
42
46
  end
@@ -1,4 +1,5 @@
1
1
  require "pluck_map/presenter"
2
+ require "pluck_map/struct"
2
3
 
3
4
  module PluckMap
4
5
  class ModelContext
@@ -8,7 +9,27 @@ module PluckMap
8
9
 
9
10
  def define(&block)
10
11
  attributes = PluckMap::AttributeBuilder.build(model: @model, &block)
11
- PluckMap::Presenter.new(@model, attributes)
12
+ define_class!(@model, attributes)
13
+ end
14
+
15
+ private
16
+
17
+ def define_class!(model, attributes)
18
+ # Create a new subclass of PluckMap::Presenter
19
+ klass = Class.new(PluckMap::Presenter)
20
+
21
+ # Partially apply initialize with the parameters passed to this method
22
+ klass.define_method(:initialize) do |query|
23
+ super(model, attributes, query)
24
+ end
25
+
26
+ # Generate a Struct constant in the namespace of the new subclass
27
+ struct = ::Struct.new(*attributes.ids, keyword_init: true)
28
+ struct.extend PluckMap::Struct::ClassMethods
29
+ struct.instance_variable_set :@presenter, klass
30
+ klass.const_set :Struct, struct
31
+
32
+ klass
12
33
  end
13
34
  end
14
35
  end
@@ -9,20 +9,21 @@ module PluckMap
9
9
  class Presenter
10
10
  include CsvPresenter, HashPresenter, JsonPresenter
11
11
 
12
- attr_reader :model, :attributes
12
+ attr_reader :model, :attributes, :query
13
+
14
+ def initialize(model, attributes, query)
15
+ unless query.model <= model
16
+ raise ArgumentError, "Query for #{query.model} but #{model} expected"
17
+ end
13
18
 
14
- def initialize(model, attributes)
15
19
  @model = model
16
20
  @attributes = attributes
21
+ @query = query
17
22
  end
18
23
 
19
24
  protected
20
25
 
21
- def pluck(query)
22
- unless query.model <= model
23
- raise ArgumentError, "Query for #{query.model} but #{model} expected"
24
- end
25
-
26
+ def pluck
26
27
  # puts "\e[95m#{query.select(*selects).to_sql}\e[0m"
27
28
  results = benchmark("pluck(#{query.table_name})") { query.pluck(*selects) }
28
29
  return results unless block_given?
@@ -70,6 +71,7 @@ module PluckMap
70
71
 
71
72
  # On Rails 4.2, `pluck` can't accept Arel nodes
72
73
  select = Arel.sql(select.to_sql) if ActiveRecord.version.segments.take(2) == [4,2] && select.respond_to?(:to_sql)
74
+
73
75
  select
74
76
  }
75
77
  end
@@ -3,13 +3,19 @@ require "pluck_map/errors"
3
3
  module PluckMap
4
4
  module CsvPresenter
5
5
 
6
- def to_csv(query)
6
+ def self.included(base)
7
+ def base.to_csv(query, **kargs)
8
+ new(query).to_csv(**kargs)
9
+ end
10
+ end
11
+
12
+ def to_csv
7
13
  if attributes.nested?
8
14
  raise PluckMap::UnsupportedAttributeError, "to_csv can not be used to present nested attributes"
9
15
  end
10
16
 
11
17
  define_to_csv!
12
- to_csv(query)
18
+ to_csv
13
19
  end
14
20
 
15
21
  private def define_to_csv!
@@ -17,8 +23,8 @@ module PluckMap
17
23
 
18
24
  headers = CSV.generate_line(attributes.map(&:name))
19
25
  ruby = <<-RUBY
20
- def to_csv(query)
21
- pluck(query) do |results|
26
+ def to_csv
27
+ pluck do |results|
22
28
  rows = [#{headers.inspect}]
23
29
  results.each_with_object(rows) do |values, rows|
24
30
  values = Array(values)
@@ -1,15 +1,21 @@
1
1
  module PluckMap
2
2
  module HashPresenter
3
3
 
4
- def to_h(query)
4
+ def self.included(base)
5
+ def base.to_h(query, **kargs)
6
+ new(query).to_h(**kargs)
7
+ end
8
+ end
9
+
10
+ def to_h
5
11
  define_to_h!
6
- to_h(query)
12
+ to_h
7
13
  end
8
14
 
9
15
  private def define_to_h!
10
16
  ruby = <<-RUBY
11
- def to_h(query)
12
- pluck(query) do |results|
17
+ def to_h
18
+ pluck do |results|
13
19
  results.map { |values| values = Array(values); { #{attributes.map { |attribute| "#{attribute.name.inspect} => #{attribute.to_ruby}" }.join(", ")} } }
14
20
  end
15
21
  end
@@ -3,30 +3,36 @@ require "json"
3
3
  module PluckMap
4
4
  module JsonPresenter
5
5
 
6
- def to_json(query, json: default_json, **)
6
+ def self.included(base)
7
+ def base.to_json(query, **kargs)
8
+ new(query).to_json(**kargs)
9
+ end
10
+ end
11
+
12
+ def to_json(json: default_json, **)
7
13
  if attributes.will_map?
8
- to_json__default(query, json: json)
14
+ to_json__default(json: json)
9
15
  else
10
- to_json__optimized(query)
16
+ to_json__optimized
11
17
  end
12
18
  end
13
19
 
14
20
  private
15
21
 
16
- def to_json__default(query, json: default_json, **)
17
- json.dump(to_h(query))
22
+ def to_json__default(json: default_json, **)
23
+ json.dump(to_h)
18
24
  end
19
25
 
20
- def to_json__optimized(query, **)
26
+ def to_json__optimized(**)
21
27
  define_to_json__optimized!
22
- to_json__optimized(query)
28
+ to_json__optimized
23
29
  end
24
30
 
25
31
  def define_to_json__optimized!
26
32
  sql = compile(to_json_object(attributes).as("object"))
27
33
 
28
34
  ruby = <<-RUBY
29
- private def to_json__optimized(query, **)
35
+ private def to_json__optimized(**)
30
36
  sql = wrap_aggregate(query.select(Arel.sql(#{sql.inspect})))
31
37
  query.connection.select_value(sql)
32
38
  end
@@ -0,0 +1,13 @@
1
+ module PluckMap
2
+ module Struct
3
+ module ClassMethods
4
+ def presenter
5
+ @presenter || superclass.presenter
6
+ end
7
+
8
+ def load(relation)
9
+ presenter.to_h(relation).map { |values| new(**values) }
10
+ end
11
+ end
12
+ end
13
+ end
@@ -1,3 +1,3 @@
1
1
  module PluckMapPresenter
2
- VERSION = "1.0.0"
2
+ VERSION = "2.0.0.rc1"
3
3
  end
data/pluck_map.gemspec CHANGED
@@ -17,6 +17,9 @@ 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
+ # uses `keyword_init` argument of `Struct.new`
21
+ spec.required_ruby_version = '>= 2.5.0'
22
+
20
23
  spec.add_dependency "activerecord", ">= 4.2"
21
24
 
22
25
  spec.add_development_dependency "appraisal"
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: 1.0.0
4
+ version: 2.0.0.rc1
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-07-17 00:00:00.000000000 Z
11
+ date: 2019-11-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -276,6 +276,7 @@ files:
276
276
  - lib/pluck_map/relationships/many.rb
277
277
  - lib/pluck_map/relationships/one.rb
278
278
  - lib/pluck_map/relationships/polymorphic_one.rb
279
+ - lib/pluck_map/struct.rb
279
280
  - lib/pluck_map/structured_attribute.rb
280
281
  - lib/pluck_map/version.rb
281
282
  - lib/pluck_map/visitors.rb
@@ -294,12 +295,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
294
295
  requirements:
295
296
  - - ">="
296
297
  - !ruby/object:Gem::Version
297
- version: '0'
298
+ version: 2.5.0
298
299
  required_rubygems_version: !ruby/object:Gem::Requirement
299
300
  requirements:
300
- - - ">="
301
+ - - ">"
301
302
  - !ruby/object:Gem::Version
302
- version: '0'
303
+ version: 1.3.1
303
304
  requirements: []
304
305
  rubyforge_project:
305
306
  rubygems_version: 2.7.6