relational_exporter 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +25 -1
- data/lib/relational_exporter.rb +17 -9
- data/lib/relational_exporter/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8c1b454011ca52b5ec7122722273f8503e1bd55a
|
4
|
+
data.tar.gz: 3c8f0d8b4371f5177d4b04b46debd9ef0df49287
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5ce81c194f3d0168cdf9dd88ba22f35e8a1909e269d925a2b560b302f3930054ca0c505d7f975ad50e52988c6cd7ea0880ddd1870e0a5f912a628428500e9890
|
7
|
+
data.tar.gz: 3db2e8d53cfe005eadf99f53034d50a325f0b1c4d477fd7999fae25f3019f77f5225f86854f9190c41398a21736b21af75d8c4ddee571dc36041659298ff0fa6
|
data/README.md
CHANGED
@@ -4,8 +4,8 @@ A gem to make it easy to export data from relational databases. RelationalExport
|
|
4
4
|
|
5
5
|
## TODO
|
6
6
|
|
7
|
+
* Add concurrency for faster exports
|
7
8
|
* Support multiple formats (currently only CSV)
|
8
|
-
* Integrate serializers of some sort
|
9
9
|
* Improve DSL
|
10
10
|
* Clean up the code
|
11
11
|
|
@@ -89,6 +89,30 @@ Or install it yourself as:
|
|
89
89
|
end
|
90
90
|
```
|
91
91
|
|
92
|
+
## Support for Active Model Serializers
|
93
|
+
|
94
|
+
Install the [`active_model_serializers`](https://github.com/rails-api/active_model_serializers) gem in your application to take advantage of easy serialization. For CSV exports, `relational_exporter` will look for a serializer when rendering the CSV row. If your serializer implements the `self.csv_header_prefix_for_key(key)` method, `relational_exporter` will use that method to determine the column header for a given model attribute.
|
95
|
+
|
96
|
+
Example:
|
97
|
+
|
98
|
+
```ruby
|
99
|
+
class PersonSerializer < ActiveModel::Serializer
|
100
|
+
attributes :id, :name, :brother_name
|
101
|
+
|
102
|
+
def self.csv_header_prefix_for_key(key)
|
103
|
+
key == :brother_name ? '' : 'Person'
|
104
|
+
end
|
105
|
+
end
|
106
|
+
```
|
107
|
+
|
108
|
+
Would generate a CSV header row like:
|
109
|
+
|
110
|
+
```
|
111
|
+
PersonId,PersonName,BrotherName
|
112
|
+
```
|
113
|
+
|
114
|
+
Notice how the 'BrotherName' is not prefixed with 'Person'
|
115
|
+
|
92
116
|
## Contributing
|
93
117
|
|
94
118
|
1. Fork it
|
data/lib/relational_exporter.rb
CHANGED
@@ -58,7 +58,7 @@ module RelationalExporter
|
|
58
58
|
|
59
59
|
# Add main record headers
|
60
60
|
serialized_attributes(single).each do |field, value|
|
61
|
-
header_row <<
|
61
|
+
header_row << csv_header_prefix_for_key(main_klass, field) if csv.header_row?
|
62
62
|
row << value
|
63
63
|
end
|
64
64
|
|
@@ -101,7 +101,7 @@ module RelationalExporter
|
|
101
101
|
|
102
102
|
max_associated.times do |i|
|
103
103
|
fields.each do |field|
|
104
|
-
header_row <<
|
104
|
+
header_row << csv_header_prefix_for_key(association_klass, field, i+1)
|
105
105
|
end
|
106
106
|
end
|
107
107
|
end
|
@@ -121,28 +121,36 @@ module RelationalExporter
|
|
121
121
|
|
122
122
|
private
|
123
123
|
|
124
|
+
def csv_header_prefix_for_key(klass, key, index=nil)
|
125
|
+
if klass.respond_to?(:active_model_serializer) && !klass.active_model_serializer.nil? && klass.active_model_serializer.respond_to?(:csv_header_prefix_for_key)
|
126
|
+
header_prefix = klass.active_model_serializer.csv_header_prefix_for_key key.to_sym
|
127
|
+
else
|
128
|
+
header_prefix = klass.to_s
|
129
|
+
end
|
130
|
+
|
131
|
+
header_prefix + index.to_s + key.to_s.classify
|
132
|
+
end
|
133
|
+
|
124
134
|
def serialized_attributes(object)
|
125
|
-
return
|
135
|
+
return {} if object.nil?
|
126
136
|
|
127
137
|
klass, model = object.is_a?(Class) ? [object, object.first] : [object.class, object]
|
128
138
|
|
129
139
|
return {} if model.nil?
|
130
140
|
|
131
141
|
if model.respond_to?(:active_model_serializer) && !model.active_model_serializer.nil?
|
132
|
-
serialized = model.active_model_serializer.new(model).as_json(root: false)
|
133
|
-
# elsif defined?(BaseSerializer)
|
134
|
-
# serialized = BaseSerializer.new(model).as_json(root: false) rescue nil
|
142
|
+
serialized = model.active_model_serializer.new(model).as_json(root: false)
|
135
143
|
end
|
136
144
|
|
137
145
|
serialized = model.attributes if serialized.nil?
|
138
146
|
serialized
|
139
147
|
end
|
140
148
|
|
141
|
-
def get_row_arr(records, fields, max_count
|
149
|
+
def get_row_arr(records, fields, max_count, &block)
|
142
150
|
max_count.times do |i|
|
143
|
-
record = serialized_attributes
|
151
|
+
record = records[i].nil? ? {} : serialized_attributes(records[i])
|
144
152
|
fields.each do |field|
|
145
|
-
val = record[field]
|
153
|
+
val = record[field]
|
146
154
|
yield val
|
147
155
|
end
|
148
156
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: relational_exporter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Hammond
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-02-
|
11
|
+
date: 2014-02-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: hashie
|