activekit 0.5.0.dev5 → 0.5.0.dev7

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: '068662b0f0144c92e082fc2a3e5002695a090dc1a4547a79889a945e739b7593'
4
- data.tar.gz: a782fef4b69dfd9df1869c78e5826c8699adaf5339ca066ca48531ba391f66c5
3
+ metadata.gz: 2e3830a0b0d888ffcd7a7f921b282fbf27dc0f2bdde5322772a71ed04755add3
4
+ data.tar.gz: abb0bbead8a270129c04dfb0998e15999df84a6daeb7c28a3237dac161211f43
5
5
  SHA512:
6
- metadata.gz: c0b3d0c3bd09b6d4de855c802ca0e4e47485a3fa727331088ff9b0ed3f21bf701485f6f15addd0d4b16cabad7442cb5d8d3f256e5b9d524ea36a0c0b2e58ce70
7
- data.tar.gz: 6d74e3e1775bd54f86022f40815593f3e0c60d6fdbb3fed1d14054c633f4af2b1456d1ad03018aaf2b9698fd0aadc123c3884edcd996af3f1ad7405238dcc5aa
6
+ metadata.gz: a2f82ef282ec1226ac39d8ac42993f5ed621ea89407c22ebd6a0850d40157f32e20be968a946899c833bb041b2fe258914c64247c7746a968709f17da88db6af
7
+ data.tar.gz: baea0b85ab9b4adbd8e3c1f1eb1e37d62c673b83fc4858233a96cee9b7721a37f7985479a66a0848b16db33137dda1a48553a5d275da5ab0241aa85eee84ebd3
@@ -14,11 +14,36 @@ module ActiveKit
14
14
  end
15
15
 
16
16
  def export_describer(name, **options)
17
- exporter.create_export_describer(name, options)
17
+ exporter.create_describer(name, options)
18
18
  end
19
19
 
20
20
  def export_attribute(name, **options)
21
- exporter.create_export_attribute(name, options)
21
+ exporter.create_attribute(name, options)
22
+ end
23
+
24
+ def export_describer_method(describer)
25
+ case describer.kind
26
+ when :csv
27
+ # The 'all' relation must be captured outside the Enumerator,
28
+ # else it will get reset to all the records of the class.
29
+ all_activerecord_relation = all.includes(describer.includes)
30
+
31
+ Enumerator.new do |yielder|
32
+ ActiveRecord::Base.connected_to(role: :writing, shard: describer.database.call) do
33
+ exporting = exporter.new_exporting(describer: describer)
34
+
35
+ # Add the headings.
36
+ yielder << CSV.generate_line(exporting.headings) if exporting.headings?
37
+
38
+ # Add the values.
39
+ # find_each will ignore any order if set earlier.
40
+ all_activerecord_relation.find_each do |record|
41
+ lines = exporting.lines_for(record: record)
42
+ lines.each { |line| yielder << CSV.generate_line(line) }
43
+ end
44
+ end
45
+ end
46
+ end
22
47
  end
23
48
  end
24
49
  end
@@ -6,23 +6,28 @@ module ActiveKit
6
6
  @describers = {}
7
7
  end
8
8
 
9
- def create_export_describer(name, options)
9
+ def create_describer(name, options)
10
10
  name = name.to_sym
11
11
  options.deep_symbolize_keys!
12
12
 
13
13
  unless find_describer_by(describer_name: name)
14
14
  options.store(:attributes, {})
15
15
  @describers.store(name, options)
16
- describer = find_describer_by(describer_name: name)
17
- define_describer_method(describer)
16
+ @current_class.class_eval do
17
+ define_singleton_method name do
18
+ if describer = exporter.find_describer_by(describer_name: name)
19
+ export_describer_method(describer)
20
+ end
21
+ end
22
+ end
18
23
  end
19
24
  end
20
25
 
21
- def create_export_attribute(name, options)
22
- create_export_describer(:to_csv, kind: :csv, database: -> { ActiveRecord::Base.connection_db_config.database.to_sym }) unless @describers.present?
23
-
26
+ def create_attribute(name, options)
24
27
  options.deep_symbolize_keys!
25
28
 
29
+ create_describer(:to_csv, kind: :csv, database: -> { ActiveRecord::Base.connection_db_config.database.to_sym }) unless @describers.present?
30
+
26
31
  describer_names = Array(options.delete(:describers))
27
32
  describer_names = @describers.keys if describer_names.blank?
28
33
 
@@ -33,58 +38,26 @@ module ActiveKit
33
38
  end
34
39
  end
35
40
 
36
- private
41
+ def find_describer_by(name:)
42
+ options = @describers.dig(name)
43
+ return nil unless options.present?
37
44
 
38
- def define_describer_method(describer)
39
- case describer.kind
40
- when :csv
41
- @current_class.class_eval do
42
- define_singleton_method describer.name do
43
- # The 'all' relation must be captured outside the Enumerator,
44
- # else it will get reset to all the records of the class.
45
- all_activerecord_relation = all.includes(describer.includes)
46
-
47
- Enumerator.new do |yielder|
48
- ActiveRecord::Base.connected_to(role: :writing, shard: describer.database.call) do
49
- exporting = exporter.new_exporting(describer: describer)
50
-
51
- # Add the headings.
52
- yielder << CSV.generate_line(exporting.headings) if exporting.headings?
53
-
54
- # Add the values.
55
- # find_each will ignore any order if set earlier.
56
- all_activerecord_relation.find_each do |record|
57
- lines = exporting.lines_for(record: record)
58
- lines.each { |line| yielder << CSV.generate_line(line) }
59
- end
60
- end
61
- end
62
- end
63
- end
64
- end
45
+ hash = {
46
+ name: name,
47
+ kind: options[:kind],
48
+ database: options[:database],
49
+ attributes: options[:attributes],
50
+ includes: options[:attributes].values.map { |options| options.dig(:includes) }.compact.flatten(1).uniq,
51
+ fields: build_describer_fields(options[:attributes])
52
+ }
53
+ OpenStruct.new(hash)
65
54
  end
66
55
 
67
56
  def new_exporting(describer:)
68
57
  Exporting.new(describer: describer)
69
58
  end
70
59
 
71
- def find_describer_by(describer_name:)
72
- describer_options = @describers.dig(describer_name)
73
- return nil unless describer_options.present?
74
-
75
- describer_attributes = describer_options[:attributes]
76
- includes = describer_attributes.values.map { |options| options.dig(:includes) }.compact.flatten(1).uniq
77
- fields = build_describer_fields(describer_attributes)
78
- hash = {
79
- name: describer_name,
80
- kind: describer_options[:kind],
81
- database: describer_options[:database],
82
- attributes: describer_attributes,
83
- includes: includes,
84
- fields: fields
85
- }
86
- OpenStruct.new(hash)
87
- end
60
+ private
88
61
 
89
62
  def build_describer_fields(describer_attributes)
90
63
  describer_attributes.inject({}) do |fields_hash, (name, options)|
@@ -1,3 +1,3 @@
1
1
  module ActiveKit
2
- VERSION = '0.5.0.dev5'
2
+ VERSION = '0.5.0.dev7'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activekit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0.dev5
4
+ version: 0.5.0.dev7
5
5
  platform: ruby
6
6
  authors:
7
7
  - plainsource