data_resurrection 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -2,6 +2,14 @@
2
2
 
3
3
  Bring your data, buried in decrepit formats, back to life! Convert data from old formats to modern ones. Currently supports DBF.
4
4
 
5
+
6
+ == How to install
7
+
8
+ Just run:
9
+
10
+ $ gem install data_resurrection
11
+
12
+
5
13
  == How to use
6
14
 
7
15
  If you have a PostgreSQL database and a DBF file called 'decrepit.dbf', you must call:
@@ -7,25 +7,30 @@ module DataResurrection
7
7
  module Adapters
8
8
  module DBF
9
9
  def resurrect(origin_table, options)
10
- target_table_name, from, to = options[:target], options[:from], options[:to]
10
+ target_table_name = options[:target]
11
+ from, to = options[:from], options[:to]
11
12
  field_types = options[:field_types]
12
13
  table = ::DBF::Table.new(origin_table)
13
- data = get_data(table, {from: from, to: to}, reserved_words)
14
+ encodings = from.present? ? {from: from, to: to} : nil
15
+ data = get_data(table, encodings, reserved_words)
14
16
  create_table(table, target_table_name, data, field_types)
15
17
  copy_data(target_table_name, data)
16
18
  end
17
19
 
18
20
  def get_data(table, encodings=nil, reserved_words=[])
19
21
  result = get_raw_data(table, reserved_words)
20
- result = handle_encodings(result, encodings) if encodings
22
+ result = handle_encodings(result, encodings) if encodings.present?
21
23
  result
22
24
  end
23
25
 
24
26
  private
25
27
 
26
28
  def create_table(table, table_name, data, field_types)
27
- schema = mark_name_clashed_fields(table.schema, data)
28
- schema = replace_types(schema, field_types) if field_types
29
+ schema = table.schema
30
+ mark_name_clashed_fields(schema,
31
+ table.columns.map {|c| generated_field_name(c.name.downcase, reserved_words) })
32
+ replace_table_name(schema, table_name)
33
+ replace_types(schema, field_types) if field_types
29
34
  eval(schema)
30
35
  end
31
36
 
@@ -70,13 +75,18 @@ module DataResurrection
70
75
  end
71
76
  end
72
77
 
73
- def mark_name_clashed_fields(schema, data)
74
- data.first.keys.each do |field|
78
+ def mark_name_clashed_fields(schema, field_names)
79
+ field_names.each do |field|
75
80
  if !schema.include?('column "%s"' % field)
76
81
  schema['column "%s"' % field.chop] = 'column "%s"' % field
77
82
  end
78
83
  end
79
- schema
84
+ end
85
+
86
+ def replace_table_name(schema, new_table_name)
87
+ schema =~ /create_table "(.+)"/
88
+ old_table_name = $1
89
+ schema['create_table "%s"' % old_table_name] = 'create_table "%s"' % new_table_name
80
90
  end
81
91
 
82
92
  def replace_types(schema, field_types)
@@ -88,7 +98,6 @@ module DataResurrection
88
98
  schema['column "%s", :%s%s' % [field, old_type, tail]] =
89
99
  'column "%s", :%s%s' % [field, new_type, tail]
90
100
  end
91
- schema
92
101
  end
93
102
 
94
103
  def all_valid?(string)
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.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,12 +9,12 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-08-27 00:00:00.000000000 -03:00
12
+ date: 2011-08-30 00:00:00.000000000 -03:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activerecord
17
- requirement: &19880520 !ruby/object:Gem::Requirement
17
+ requirement: &7166360 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ~>
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: 3.0.0
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *19880520
25
+ version_requirements: *7166360
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: dbf
28
- requirement: &19880060 !ruby/object:Gem::Requirement
28
+ requirement: &7165900 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ~>
@@ -33,10 +33,10 @@ dependencies:
33
33
  version: 1.5.0
34
34
  type: :runtime
35
35
  prerelease: false
36
- version_requirements: *19880060
36
+ version_requirements: *7165900
37
37
  - !ruby/object:Gem::Dependency
38
38
  name: sqlite3
39
- requirement: &19879600 !ruby/object:Gem::Requirement
39
+ requirement: &7205600 !ruby/object:Gem::Requirement
40
40
  none: false
41
41
  requirements:
42
42
  - - ~>
@@ -44,10 +44,10 @@ dependencies:
44
44
  version: 1.3.0
45
45
  type: :development
46
46
  prerelease: false
47
- version_requirements: *19879600
47
+ version_requirements: *7205600
48
48
  - !ruby/object:Gem::Dependency
49
49
  name: rspec
50
- requirement: &19918680 !ruby/object:Gem::Requirement
50
+ requirement: &7205140 !ruby/object:Gem::Requirement
51
51
  none: false
52
52
  requirements:
53
53
  - - ~>
@@ -55,7 +55,7 @@ dependencies:
55
55
  version: 2.6.0
56
56
  type: :development
57
57
  prerelease: false
58
- version_requirements: *19918680
58
+ version_requirements: *7205140
59
59
  description: Converts DBF to modern formats.
60
60
  email: rmanhaes@gmail.com
61
61
  executables: []