activekit 0.5.0.dev5 → 0.5.0.dev6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/active_kit/export/exportable.rb +2 -2
- data/lib/active_kit/export/exporter.rb +29 -32
- data/lib/active_kit/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4be51587fe2bc2ef391a14a432ab155a50489acd765845dd88212c56bfbd5328
|
4
|
+
data.tar.gz: 9839b9510d9610ca93573dda2e4e83b05b1cd1aa2c9902ff96f2dc83d2385af4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5769a0034e566bbe56280904e96cb240e32f309191f7b6e1d20368d8faaf67ea348b90bf19295e810214a845fe639fae2494629b2c0bf3444fdff45d77710b54
|
7
|
+
data.tar.gz: 0e025b165752e96fae3b839420134e111a9c1f264176bafdaff3d8174e3ea9514f59833873bd5f587f8db4a56497ec9be10416ee8112d7abf848d815a1a199de
|
@@ -14,11 +14,11 @@ module ActiveKit
|
|
14
14
|
end
|
15
15
|
|
16
16
|
def export_describer(name, **options)
|
17
|
-
exporter.
|
17
|
+
exporter.create_describer(name, options)
|
18
18
|
end
|
19
19
|
|
20
20
|
def export_attribute(name, **options)
|
21
|
-
exporter.
|
21
|
+
exporter.create_attribute(name, options)
|
22
22
|
end
|
23
23
|
end
|
24
24
|
end
|
@@ -6,23 +6,28 @@ module ActiveKit
|
|
6
6
|
@describers = {}
|
7
7
|
end
|
8
8
|
|
9
|
-
def
|
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
|
-
|
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)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
18
23
|
end
|
19
24
|
end
|
20
25
|
|
21
|
-
def
|
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,40 +38,32 @@ module ActiveKit
|
|
33
38
|
end
|
34
39
|
end
|
35
40
|
|
36
|
-
|
37
|
-
|
38
|
-
def define_describer_method(describer)
|
41
|
+
def describer_method(describer)
|
39
42
|
case describer.kind
|
40
43
|
when :csv
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
lines = exporting.lines_for(record: record)
|
58
|
-
lines.each { |line| yielder << CSV.generate_line(line) }
|
59
|
-
end
|
60
|
-
end
|
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) }
|
61
60
|
end
|
62
61
|
end
|
63
62
|
end
|
64
63
|
end
|
65
64
|
end
|
66
65
|
|
67
|
-
|
68
|
-
Exporting.new(describer: describer)
|
69
|
-
end
|
66
|
+
private
|
70
67
|
|
71
68
|
def find_describer_by(describer_name:)
|
72
69
|
describer_options = @describers.dig(describer_name)
|
data/lib/active_kit/version.rb
CHANGED