activekit 0.5.0.dev6 → 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: 4be51587fe2bc2ef391a14a432ab155a50489acd765845dd88212c56bfbd5328
4
- data.tar.gz: 9839b9510d9610ca93573dda2e4e83b05b1cd1aa2c9902ff96f2dc83d2385af4
3
+ metadata.gz: 2e3830a0b0d888ffcd7a7f921b282fbf27dc0f2bdde5322772a71ed04755add3
4
+ data.tar.gz: abb0bbead8a270129c04dfb0998e15999df84a6daeb7c28a3237dac161211f43
5
5
  SHA512:
6
- metadata.gz: 5769a0034e566bbe56280904e96cb240e32f309191f7b6e1d20368d8faaf67ea348b90bf19295e810214a845fe639fae2494629b2c0bf3444fdff45d77710b54
7
- data.tar.gz: 0e025b165752e96fae3b839420134e111a9c1f264176bafdaff3d8174e3ea9514f59833873bd5f587f8db4a56497ec9be10416ee8112d7abf848d815a1a199de
6
+ metadata.gz: a2f82ef282ec1226ac39d8ac42993f5ed621ea89407c22ebd6a0850d40157f32e20be968a946899c833bb041b2fe258914c64247c7746a968709f17da88db6af
7
+ data.tar.gz: baea0b85ab9b4adbd8e3c1f1eb1e37d62c673b83fc4858233a96cee9b7721a37f7985479a66a0848b16db33137dda1a48553a5d275da5ab0241aa85eee84ebd3
@@ -20,6 +20,31 @@ module ActiveKit
20
20
  def export_attribute(name, **options)
21
21
  exporter.create_attribute(name, options)
22
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
47
+ end
23
48
  end
24
49
  end
25
50
  end
@@ -13,10 +13,10 @@ module ActiveKit
13
13
  unless find_describer_by(describer_name: name)
14
14
  options.store(:attributes, {})
15
15
  @describers.store(name, options)
16
- if describer = find_describer_by(describer_name: name)
17
- @current_class.class_eval do
18
- define_singleton_method describer.name do
19
- exporter.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
20
  end
21
21
  end
22
22
  end
@@ -38,51 +38,27 @@ module ActiveKit
38
38
  end
39
39
  end
40
40
 
41
- def describer_method(describer)
42
- case describer.kind
43
- when :csv
44
- # The 'all' relation must be captured outside the Enumerator,
45
- # else it will get reset to all the records of the class.
46
- all_activerecord_relation = all.includes(describer.includes)
47
-
48
- Enumerator.new do |yielder|
49
- ActiveRecord::Base.connected_to(role: :writing, shard: describer.database.call) do
50
- exporting = Exporting.new(describer: describer)
51
-
52
- # Add the headings.
53
- yielder << CSV.generate_line(exporting.headings) if exporting.headings?
54
-
55
- # Add the values.
56
- # find_each will ignore any order if set earlier.
57
- all_activerecord_relation.find_each do |record|
58
- lines = exporting.lines_for(record: record)
59
- lines.each { |line| yielder << CSV.generate_line(line) }
60
- end
61
- end
62
- end
63
- end
64
- end
65
-
66
- private
67
-
68
- def find_describer_by(describer_name:)
69
- describer_options = @describers.dig(describer_name)
70
- return nil unless describer_options.present?
41
+ def find_describer_by(name:)
42
+ options = @describers.dig(name)
43
+ return nil unless options.present?
71
44
 
72
- describer_attributes = describer_options[:attributes]
73
- includes = describer_attributes.values.map { |options| options.dig(:includes) }.compact.flatten(1).uniq
74
- fields = build_describer_fields(describer_attributes)
75
45
  hash = {
76
- name: describer_name,
77
- kind: describer_options[:kind],
78
- database: describer_options[:database],
79
- attributes: describer_attributes,
80
- includes: includes,
81
- fields: fields
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])
82
52
  }
83
53
  OpenStruct.new(hash)
84
54
  end
85
55
 
56
+ def new_exporting(describer:)
57
+ Exporting.new(describer: describer)
58
+ end
59
+
60
+ private
61
+
86
62
  def build_describer_fields(describer_attributes)
87
63
  describer_attributes.inject({}) do |fields_hash, (name, options)|
88
64
  enclosed_attributes = Array(options.dig(:attributes))
@@ -1,3 +1,3 @@
1
1
  module ActiveKit
2
- VERSION = '0.5.0.dev6'
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.dev6
4
+ version: 0.5.0.dev7
5
5
  platform: ruby
6
6
  authors:
7
7
  - plainsource