relational_exporter 0.0.2 → 0.0.3

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
  SHA1:
3
- metadata.gz: 93369bce5ffa225e191684a1b1655bede21b40ce
4
- data.tar.gz: 4b811e535dedd706361921e85d204938436c8912
3
+ metadata.gz: 27528505c9ae963ac90a8dec1a21aadba17fecd7
4
+ data.tar.gz: e84bb8298c019cc46dca592d3da020bf03f6c564
5
5
  SHA512:
6
- metadata.gz: 9eae62da777df4f3991a206323d80351cced31ff9d86a2547d884ea1c92058c16198ded18819f877769fdd9983145b960ea0a9dd063001e2831e310c3506e83e
7
- data.tar.gz: 5c2d7546a87f69f34a481873769b619bab48cf6cbc751f5b75162f703ff88e1a9eb28928a8428832e5efa8af62a2e97b6bdf6ae185df4e9c97617ffc10ce625e
6
+ metadata.gz: 12fc7ca1f8478f07f1c76f23fa05e91a4d89620f9656ab72d133c768c2b5e5cc8fbe8397f3e2742a9f35b47ef3e7cf28d271205334df573c8aacbb8f7201ae01
7
+ data.tar.gz: 202c03846dcc21b7cd8f0c367d6ff841c346cc6cd5e8f6f79af0834966f0ee06a5e8d9fad5aa50e04f7d72ce22b0ea4a62b746863f4f022a96813e366a0e41a8
data/README.md CHANGED
@@ -78,9 +78,14 @@ Or install it yourself as:
78
78
  }
79
79
 
80
80
  # Run the export!
81
- r = RelationalExporter::Runner.new schema: my_schema, connection_config: my_conn
81
+ r = RelationalExporter::Runner.new schema: my_schema, connection_config: my_conn, logger: Logger.new(STDERR)
82
82
  r.export(my_output_config) do |record|
83
- # modify the record and it's models/associations
83
+ # examine the record and it's associations using any
84
+ # Active Record models that were defined in your schema!
85
+ puts record.class # => Person
86
+
87
+ # filter out the record if desired
88
+ record = nil if record.name == 'Bob'
84
89
  end
85
90
  ```
86
91
 
@@ -6,11 +6,10 @@ require 'relational_exporter/active_record_extension'
6
6
 
7
7
  module RelationalExporter
8
8
  class Runner
9
- attr_accessor :schema
9
+ attr_accessor :schema, :logger
10
10
 
11
11
  def initialize(options={})
12
- # TODO - disable when not byebugging!!!
13
- ActiveRecord::Base.logger = Logger.new(STDOUT)
12
+ @logger = options[:logger] ? options[:logger] : Logger.new(STDERR)
14
13
 
15
14
  @connection_config = options[:connection_config]
16
15
  begin
@@ -26,6 +25,8 @@ module RelationalExporter
26
25
  end
27
26
 
28
27
  def export(output_config, &block)
28
+ ActiveRecord::Base.logger = @logger
29
+
29
30
  output_config = Hashie::Mash.new output_config
30
31
 
31
32
  main_klass = output_config.output.model.to_s.classify.constantize
@@ -35,7 +36,16 @@ module RelationalExporter
35
36
  header_row = []
36
37
  max_associations = {}
37
38
 
38
- ::CSV.open('/tmp/test.csv', 'wb', headers: true) do |csv|
39
+ csv_options = {headers: true}
40
+ if output_config.file_path.blank?
41
+ csv_method = :instance
42
+ csv_args = [STDOUT, csv_options]
43
+ else
44
+ csv_method = :open
45
+ csv_args = [output_config.file_path, 'wb', csv_options]
46
+ end
47
+
48
+ ::CSV.send(csv_method, *csv_args) do |csv|
39
49
  main_klass.find_all_by_scope(output_config.output.scope.as_json).find_in_batches do |batch|
40
50
  batch.each do |single|
41
51
  if block_given?
@@ -47,15 +57,7 @@ module RelationalExporter
47
57
  row = []
48
58
 
49
59
  # Add main record headers
50
- if !main_klass.active_model_serializer.blank?
51
- main_attributes = main_klass.active_model_serializer.new(single).as_json(root: false) rescue nil
52
- elsif defined?(BaseSerializer)
53
- main_attributes = BaseSerializer.new(single).as_json(root: false) rescue nil
54
- end
55
-
56
- main_attributes = single.attributes if main_attributes.nil?
57
-
58
- main_attributes.each do |field, value|
60
+ serialized_attributes(single).each do |field, value|
59
61
  header_row << [main_klass.to_s.underscore, field].join('_').classify if csv.header_row?
60
62
  row << value
61
63
  end
@@ -77,7 +79,7 @@ module RelationalExporter
77
79
 
78
80
  foreign_key = main_klass.reflections[association_accessor].foreign_key rescue nil
79
81
 
80
- fields = association_klass.first.attributes.keys
82
+ fields = serialized_attributes(association_klass).keys
81
83
 
82
84
  fields.reject! {|v| v == foreign_key } if foreign_key
83
85
 
@@ -93,6 +95,8 @@ module RelationalExporter
93
95
  max_associated = 1
94
96
  end
95
97
 
98
+ max_associated = 0 if max_associated.nil?
99
+
96
100
  max_associations[association_accessor] = max_associated
97
101
 
98
102
  max_associated.times do |i|
@@ -107,7 +111,7 @@ module RelationalExporter
107
111
 
108
112
  csv << header_row if csv.header_row?
109
113
  if row.count != header_row.count
110
- puts "OH SHIT, this row is not right!"
114
+ @logger.error "Encountered invalid row, skipping."
111
115
  end
112
116
  csv << row
113
117
  end
@@ -117,10 +121,28 @@ module RelationalExporter
117
121
 
118
122
  private
119
123
 
124
+ def serialized_attributes(object)
125
+ return nil if object.nil?
126
+
127
+ klass, model = object.is_a?(Class) ? [object, object.first] : [object.class, object]
128
+
129
+ return {} if model.nil?
130
+
131
+ if model.respond_to?(:active_model_serializer) && !model.active_model_serializer.nil?
132
+ serialized = model.active_model_serializer.new(model).as_json(root: false) rescue nil
133
+ # elsif defined?(BaseSerializer)
134
+ # serialized = BaseSerializer.new(model).as_json(root: false) rescue nil
135
+ end
136
+
137
+ serialized = model.attributes if serialized.nil?
138
+ serialized
139
+ end
140
+
120
141
  def get_row_arr(records, fields, max_count=1, &block)
121
142
  max_count.times do |i|
143
+ record = serialized_attributes records[i]
122
144
  fields.each do |field|
123
- val = records[i][field] rescue nil
145
+ val = record[field] rescue nil
124
146
  yield val
125
147
  end
126
148
  end
@@ -1,3 +1,3 @@
1
1
  module RelationalExporter
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  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.2
4
+ version: 0.0.3
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-03 00:00:00.000000000 Z
11
+ date: 2014-02-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: hashie