cheap_imports 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,7 +4,7 @@ require 'cheap_imports'
4
4
 
5
5
  ActiveRecord::Base.establish_connection(
6
6
  :adapter => 'sqlite3',
7
- :database => 'example.sqlite3'
7
+ :database => 'ar-example.sqlite3'
8
8
  )
9
9
 
10
10
  ActiveRecord::Schema.define :version => 0 do
File without changes
@@ -0,0 +1,34 @@
1
+ require 'rubygems'
2
+ require 'dm-core'
3
+ require 'dm-migrations'
4
+ require 'cheap_imports'
5
+
6
+ DataMapper.setup(:default, 'sqlite3:dm-example.sqlite3')
7
+
8
+ class Payment
9
+ include DataMapper::Resource
10
+ include CheapImports
11
+
12
+ property :id, Serial
13
+ property :name, String
14
+ property :paid_on, DateTime
15
+ property :price, Decimal
16
+
17
+ imports :tarp_transactions => {
18
+ :paid_on => 'Date',
19
+ :paid_on_format => '%Y-%m-%d',
20
+ :name => 'Name',
21
+ :price => 'Price Paid'
22
+ }
23
+ end
24
+
25
+ DataMapper.finalize
26
+ DataMapper.auto_migrate!
27
+
28
+ # Data downloaded from http://subsidyscope.com/bailout/tarp/
29
+ Import.import_from_file([Payment], "tarp_transactions.csv")
30
+
31
+ Payment.all.each do |p|
32
+ puts p.inspect
33
+ end
34
+
@@ -0,0 +1,3 @@
1
+ ruby ar-example.rb > ar-example.out
2
+ ruby dm-example.rb > dm-example.out
3
+
@@ -1,7 +1,7 @@
1
1
  module CheapImports
2
2
 
3
3
  # :stopdoc:
4
- VERSION = '0.0.3'
4
+ VERSION = '0.0.4'
5
5
  LIBPATH = ::File.expand_path(::File.dirname(__FILE__)) + ::File::SEPARATOR
6
6
  PATH = ::File.dirname(LIBPATH) + ::File::SEPARATOR
7
7
  # :startdoc:
@@ -33,6 +33,10 @@ module CheapImports
33
33
  raise "default hash is empty" if default_hash.empty?
34
34
  @recognizable_hashes ||= {:default => default_hash}
35
35
  end
36
+
37
+ def recognizable_hashes
38
+ @recognizable_hashes
39
+ end
36
40
 
37
41
  # Add a new import definition.
38
42
  #
@@ -87,7 +91,8 @@ module CheapImports
87
91
 
88
92
  value = nil
89
93
 
90
- type = c.type.to_s.downcase
94
+ # ActiveRecord uses type, DataMapper uses e.g. DataMapper::Resource::String
95
+ type = (c.type || c.class.name.split('::').last).to_s.downcase
91
96
  case type
92
97
  when "string", "text", "integer", "boolean"
93
98
  if raw_value === ""
@@ -100,6 +105,7 @@ module CheapImports
100
105
  date_format_string ||= @recognizable_hashes[style]["#{name.downcase}_#{column_name}_format".to_sym]
101
106
  raise "nil date_format_string for #{style} #{column_name}" if date_format_string.empty?
102
107
  begin
108
+ # TODO support for TimeWithZone, make date format string optional with smarter parsing
103
109
  value = Date.strptime(raw_value, date_format_string) if type === "date"
104
110
  value = DateTime.strptime(raw_value, date_format_string) if type === "datetime"
105
111
  value = Time.strptime(raw_value, date_format_string) if type === "time"
@@ -18,9 +18,10 @@ module Import
18
18
 
19
19
  # Clean up the data, get rid of leading/trailing spaces.
20
20
  row.collect {|str| str.to_s.strip!}
21
-
21
+
22
22
  h = Hash[*header_row.zip(row).flatten]
23
23
  klasses.each do |c|
24
+ c.init_recognizable_hashes if c.recognizable_hashes.nil?
24
25
  style = c.recognize_hash_style(h, debug)
25
26
  objects << c.import(h, args) if style
26
27
  end
@@ -1,6 +1,7 @@
1
1
  require File.join(File.dirname(__FILE__), '..', %w[spec_helper])
2
2
 
3
3
  require "dm-core"
4
+ require 'dm-migrations'
4
5
  DataMapper.setup(:default, 'sqlite3::memory:')
5
6
 
6
7
  class Cat
@@ -26,6 +27,7 @@ class Appointment
26
27
  }
27
28
  end
28
29
 
30
+ DataMapper.finalize
29
31
  DataMapper.auto_migrate!
30
32
 
31
33
  describe Cat, "importing default" do
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 3
9
- version: 0.0.3
8
+ - 4
9
+ version: 0.0.4
10
10
  platform: ruby
11
11
  authors:
12
12
  - Ana Nelson
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-06-17 00:00:00 +01:00
17
+ date: 2010-06-18 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -47,11 +47,16 @@ files:
47
47
  - Rakefile
48
48
  - bin/cheap_imports
49
49
  - clean-arch-install.sh
50
- - examples/active-record-tarp/.example.rb.swp
51
- - examples/active-record-tarp/example.out
52
- - examples/active-record-tarp/example.rb
53
- - examples/active-record-tarp/example.sqlite3
54
- - examples/active-record-tarp/tarp_transactions.csv
50
+ - examples/cats.rb
51
+ - examples/tarp/.example.rb.swp
52
+ - examples/tarp/ar-example.out
53
+ - examples/tarp/ar-example.rb
54
+ - examples/tarp/ar-example.sqlite3
55
+ - examples/tarp/dm-example.out
56
+ - examples/tarp/dm-example.rb
57
+ - examples/tarp/dm-example.sqlite3
58
+ - examples/tarp/run-examples.sh
59
+ - examples/tarp/tarp_transactions.csv
55
60
  - lib/cheap_imports.rb
56
61
  - lib/cheap_imports/cheap_imports.rb
57
62
  - lib/cheap_imports/import.rb