data_resurrection 0.1.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -28,11 +28,16 @@ In some cases, field types in original tables are not compatible with ones in th
28
28
 
29
29
  @data_resurrection.resurrect(@dbf_file_path, :target => 'nationality',
30
30
  :from => ['WINDOWS-1252', 'CP850'], :to => 'UTF-8',
31
- :field_types => {:nr => :string })
31
+ :field_types => {:nr => :string },
32
+ :replacement => { '╟' => 'Ã' })
32
33
 
33
34
  The option :field_types is a hash in which each key is the field name and the value is the field type in the target table.
34
35
 
35
- If a field name equals to SQL reserved words or core Ruby methods like "class", the field name are appended with an underscore in the new table.
36
+ The option :replacement allows defining arbitrary character replacement, *after*
37
+ running all encodings. In the example, all existing weird "╟" in all strings
38
+ will be replaced by "Ã".
39
+
40
+ If a field name equals to SQL reserved words or core Ruby methods like "class", the field name is appended with an underscore in the new table.
36
41
 
37
42
 
38
43
  == But gem dbf does this work!
@@ -1,4 +1,6 @@
1
1
  require 'active_record'
2
- data_resurrection_folder = File.join(File.dirname(__FILE__), 'data_resurrection', '*.rb')
3
- Dir.glob(data_resurrection_folder).each {|f| require f }
4
2
 
3
+ module DataResurrection
4
+ autoload :Resuscitator, 'data_resurrection/resuscitator'
5
+ autoload :Adapter, 'data_resurrection/adapter'
6
+ end
@@ -0,0 +1,8 @@
1
+ # coding: utf-8
2
+
3
+ module DataResurrection
4
+ module Adapter
5
+ end
6
+ end
7
+
8
+ Dir.glob(File.dirname(__FILE__) + '/adapter/*.rb') {|f| require f }
@@ -4,7 +4,7 @@ require 'dbf'
4
4
  require 'iconv'
5
5
 
6
6
  module DataResurrection
7
- module Adapters
7
+ module Adapter
8
8
  module DBF
9
9
  def resurrect(origin_table, options)
10
10
  target_table_name = options[:target]
@@ -12,14 +12,15 @@ module DataResurrection
12
12
  field_types = options[:field_types]
13
13
  table = ::DBF::Table.new(origin_table)
14
14
  encodings = from.present? ? {from: from, to: to} : nil
15
- data = get_data(table, encodings, reserved_words)
15
+ data = get_data(table, encodings, reserved_words, options[:replacement])
16
16
  create_table(table, target_table_name, data, field_types)
17
17
  copy_data(target_table_name, data)
18
18
  end
19
19
 
20
- def get_data(table, encodings=nil, reserved_words=[])
20
+ def get_data(table, encodings=nil, reserved_words=[], replacement = {})
21
21
  result = get_raw_data(table, reserved_words)
22
22
  result = handle_encodings(result, encodings) if encodings.present?
23
+ result = apply_replacements(result, replacement) if replacement
23
24
  result
24
25
  end
25
26
 
@@ -76,6 +77,18 @@ module DataResurrection
76
77
  end
77
78
  end
78
79
 
80
+ def apply_replacements(data, replacements)
81
+ data.each do |record|
82
+ record.each do |k, v|
83
+ replacements.each do |source, target|
84
+ if v.kind_of? String
85
+ record[k] = v.gsub(source, target) if v.include?(source)
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end
91
+
79
92
  def mark_name_clashed_fields(schema, field_names)
80
93
  field_names.each do |field|
81
94
  if !schema.include?('column "%s"' % field)
@@ -116,6 +129,11 @@ module DataResurrection
116
129
  reserved_words.include?(field_name.upcase) ? "#{field_name}_" : field_name
117
130
  end
118
131
 
132
+ def reserved_words
133
+ @reserved_words ||= File.read(File.expand_path(File.join(File.dirname(__FILE__), 'dbf_reserved_words'))).
134
+ each_line.map(&:chomp)
135
+ end
136
+
119
137
  class ARObject < ActiveRecord::Base
120
138
  end
121
139
 
@@ -126,5 +144,4 @@ module DataResurrection
126
144
  VALID_CHARS = [REGULAR_LETTERS, DIGITS, ACCENTED_LETTERS, SYMBOLS].join
127
145
  end
128
146
  end
129
- end
130
-
147
+ end
@@ -0,0 +1,19 @@
1
+ module DataResurrection
2
+ class Resuscitator
3
+ def initialize(adapter_key, active_record_settings)
4
+ extend find_adapter(adapter_key)
5
+ ActiveRecord::Base.establish_connection(active_record_settings)
6
+ @connection = ActiveRecord::Base.connection
7
+ end
8
+
9
+ private
10
+
11
+ def find_adapter(adapter_key)
12
+ adapter = DataResurrection::Adapter
13
+ adapter.const_get(adapter.constants.
14
+ select {|const_name| adapter.const_get(const_name).is_a? Module }.
15
+ find {|a| a.to_s.underscore.to_sym == adapter_key })
16
+ end
17
+ end
18
+ end
19
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: data_resurrection
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-21 00:00:00.000000000 Z
12
+ date: 2013-04-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
@@ -81,12 +81,13 @@ executables: []
81
81
  extensions: []
82
82
  extra_rdoc_files: []
83
83
  files:
84
- - lib/data_resurrection/adapters.rb
85
- - lib/data_resurrection/resurrector.rb
84
+ - lib/data_resurrection/adapter.rb
85
+ - lib/data_resurrection/resuscitator.rb
86
+ - lib/data_resurrection/adapter/dbf.rb
86
87
  - lib/data_resurrection.rb
87
88
  - README.rdoc
88
89
  - LICENSE.txt
89
- - lib/data_resurrection/reserved_words
90
+ - lib/data_resurrection/adapter/dbf_reserved_words
90
91
  homepage: https://github.com/rodrigomanhaes/data_resurrection
91
92
  licenses: []
92
93
  post_install_message:
@@ -108,7 +109,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
108
109
  version: '0'
109
110
  requirements: []
110
111
  rubyforge_project:
111
- rubygems_version: 1.8.24
112
+ rubygems_version: 1.8.23
112
113
  signing_key:
113
114
  specification_version: 3
114
115
  summary: Bring your data, buried in decrepit formats, back to life! Convert data from
@@ -1,21 +0,0 @@
1
- module DataResurrection
2
- class Resuscitator
3
- def initialize(adapter, active_record_settings)
4
- extend adapters[adapter]
5
- ActiveRecord::Base.establish_connection(active_record_settings)
6
- @connection = ActiveRecord::Base.connection
7
- end
8
-
9
- private
10
-
11
- def adapters
12
- @adapters ||= {:dbf => DataResurrection::Adapters::DBF }
13
- end
14
-
15
- def reserved_words
16
- @reserved_words ||= File.read(File.expand_path(File.join(File.dirname(__FILE__), 'reserved_words'))).
17
- each_line.map(&:chomp)
18
- end
19
- end
20
- end
21
-