theman 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +7 -13
- data/lib/theman/agency.rb +10 -10
- data/lib/theman/agency/columns.rb +5 -5
- data/lib/theman/object.rb +8 -0
- data/lib/theman/version.rb +1 -1
- metadata +13 -5
data/README.rdoc
CHANGED
@@ -20,8 +20,6 @@ Say we have a csv file called <tt>sample.csv</tt> with 220 rows:
|
|
20
20
|
res = conn.exec("SELECT count(*) FROM #{agent.table_name}")
|
21
21
|
res.getvalue(0,0)
|
22
22
|
|
23
|
-
=> 220
|
24
|
-
|
25
23
|
== Basic usage with Active Record and a simple object
|
26
24
|
|
27
25
|
conn = ActiveRecord::Base.connection.raw_connection
|
@@ -29,22 +27,20 @@ Say we have a csv file called <tt>sample.csv</tt> with 220 rows:
|
|
29
27
|
agent = Theman::Agency.new(conn, 'sample.csv')
|
30
28
|
agent.create!
|
31
29
|
|
32
|
-
model = Theman::Object(agent.table_name, ActiveRecord::Base)
|
30
|
+
model = Theman::Object.new(agent.table_name, ActiveRecord::Base)
|
33
31
|
model.count
|
34
32
|
|
35
|
-
=> 220
|
36
|
-
|
37
33
|
== Advanced usage with Active Record and an existing model
|
38
34
|
|
39
35
|
Theman will call the <tt>create!</tt> method if you pass in a block.
|
40
36
|
|
41
37
|
conn = ActiveRecord::Base.connection.raw_connection
|
42
38
|
|
43
|
-
agent = Theman::Agency.new conn, 'ugly.csv' |
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
39
|
+
agent = Theman::Agency.new conn, 'ugly.csv' do |smith|
|
40
|
+
smith.nulls /"N"/, /"UNKNOWN"/, /""/
|
41
|
+
smith.seds "-n -e :a -e '1,15!{P;N;D;};N;ba'"
|
42
|
+
smith.delimiter "|"
|
43
|
+
smith.table do |t|
|
48
44
|
t.string :name, :limit => 50
|
49
45
|
t.date :date
|
50
46
|
t.integer :ext_id
|
@@ -56,8 +52,6 @@ Theman will call the <tt>create!</tt> method if you pass in a block.
|
|
56
52
|
MyModel.table_name = agent.table_name
|
57
53
|
MyModel.where(:exited => true).count
|
58
54
|
|
59
|
-
=> 220
|
60
|
-
|
61
55
|
In the above example we omitted the last 15 rows, made some things null and
|
62
56
|
specified some column data types.
|
63
57
|
|
@@ -65,7 +59,7 @@ If you do not provide a table block your columns will be VARCHAR(255); you
|
|
65
59
|
can cherry pick the columns that you want to change the data types for.
|
66
60
|
|
67
61
|
The temp table has no id column by default, but you can add one by calling
|
68
|
-
|
62
|
+
<tt>add_primary_key!</tt>, this will add the <tt>agents_pkey</tt> column.
|
69
63
|
|
70
64
|
== Drop on commit
|
71
65
|
|
data/lib/theman/agency.rb
CHANGED
@@ -11,7 +11,7 @@ module Theman
|
|
11
11
|
# * +options+ - Additional options are <tt>:temporary</tt>,
|
12
12
|
# <tt>:on_commit</tt> and <tt>:headers</tt>
|
13
13
|
#
|
14
|
-
# ====
|
14
|
+
# ==== Example
|
15
15
|
# # Update all customers with the given attributes
|
16
16
|
# conn = PGconn.open(:dbname => 'test')
|
17
17
|
# agent = Theman::Agency.new(conn, 'sample.csv')
|
@@ -40,14 +40,14 @@ module Theman
|
|
40
40
|
connection.exec "COMMIT;"
|
41
41
|
end
|
42
42
|
|
43
|
-
def create_stream_columns #:nodoc
|
43
|
+
def create_stream_columns #:nodoc:
|
44
44
|
@stream_columns_set = true
|
45
45
|
headers.split(delimiter_regexp).each do |column|
|
46
46
|
@columns.string column
|
47
47
|
end
|
48
48
|
end
|
49
49
|
|
50
|
-
def headers #:nodoc
|
50
|
+
def headers #:nodoc:
|
51
51
|
File.open(@stream, "r"){ |infile| infile.gets }
|
52
52
|
end
|
53
53
|
|
@@ -83,7 +83,7 @@ module Theman
|
|
83
83
|
@delimiter = arg
|
84
84
|
end
|
85
85
|
|
86
|
-
def psql_copy(psql = []) #:nodoc
|
86
|
+
def psql_copy(psql = []) #:nodoc:
|
87
87
|
psql << "COPY #{table_name} FROM STDIN WITH"
|
88
88
|
psql << "DELIMITER '#{@delimiter}'" unless @delimiter.nil?
|
89
89
|
psql << "CSV"
|
@@ -91,25 +91,25 @@ module Theman
|
|
91
91
|
psql
|
92
92
|
end
|
93
93
|
|
94
|
-
def psql_command(psql = []) #:nodoc
|
94
|
+
def psql_command(psql = []) #:nodoc:
|
95
95
|
psql << "SET DATESTYLE TO #{@datestyle}" unless @datestyle.nil?
|
96
96
|
psql << psql_copy.join(" ")
|
97
97
|
psql
|
98
98
|
end
|
99
99
|
|
100
|
-
def sed_command(sed = []) #:nodoc
|
100
|
+
def sed_command(sed = []) #:nodoc:
|
101
101
|
sed << nulls_to_sed unless @nulls.nil?
|
102
102
|
sed << @seds unless @seds.nil?
|
103
103
|
sed
|
104
104
|
end
|
105
105
|
|
106
|
-
def nulls_to_sed #:nodoc
|
106
|
+
def nulls_to_sed #:nodoc:
|
107
107
|
@nulls.map do |regex|
|
108
108
|
"-e 's/#{regex.source}//g'"
|
109
109
|
end
|
110
110
|
end
|
111
111
|
|
112
|
-
def delimiter_regexp #:nodoc
|
112
|
+
def delimiter_regexp #:nodoc:
|
113
113
|
@delimiter_regexp ||= Regexp.new(@delimiter.nil? ? "," : "\\#{@delimiter}")
|
114
114
|
end
|
115
115
|
|
@@ -143,7 +143,7 @@ module Theman
|
|
143
143
|
@table_name = nil
|
144
144
|
end
|
145
145
|
|
146
|
-
def system_command #:nodoc
|
146
|
+
def system_command #:nodoc:
|
147
147
|
unless sed_command.empty?
|
148
148
|
"cat #{@stream} | sed #{sed_command.join(" | sed ")}"
|
149
149
|
else
|
@@ -151,7 +151,7 @@ module Theman
|
|
151
151
|
end
|
152
152
|
end
|
153
153
|
|
154
|
-
def pipe_it(l = "") #:nodoc
|
154
|
+
def pipe_it(l = "") #:nodoc:
|
155
155
|
connection.exec psql_command.join("; ")
|
156
156
|
f = IO.popen(system_command)
|
157
157
|
begin
|
@@ -9,7 +9,7 @@ module Theman
|
|
9
9
|
@columns = []
|
10
10
|
end
|
11
11
|
|
12
|
-
def to_sql #:nodoc
|
12
|
+
def to_sql #:nodoc:
|
13
13
|
@columns.map{|column| column_to_sql(*column)}.join(', ')
|
14
14
|
end
|
15
15
|
|
@@ -21,11 +21,11 @@ module Theman
|
|
21
21
|
EOV
|
22
22
|
end
|
23
23
|
|
24
|
-
def symbolize(name) #:nodoc
|
24
|
+
def symbolize(name) #:nodoc:
|
25
25
|
name.is_a?(Symbol) ? name : name.gsub(/ /,"_").gsub(/\W/, "").downcase.to_sym
|
26
26
|
end
|
27
27
|
|
28
|
-
def column(name, type, *args) #:nodoc
|
28
|
+
def column(name, type, *args) #:nodoc:
|
29
29
|
sym_col = symbolize(name)
|
30
30
|
@columns.each_with_index do |column, index|
|
31
31
|
if column[0] == sym_col
|
@@ -40,7 +40,7 @@ module Theman
|
|
40
40
|
@columns.map{|column| column[0] }.include?(sym_col)
|
41
41
|
end
|
42
42
|
|
43
|
-
def column_to_sql(name, type, options = {}) #:nodoc
|
43
|
+
def column_to_sql(name, type, options = {}) #:nodoc:
|
44
44
|
sql = [quote_column_name(name)]
|
45
45
|
case type
|
46
46
|
when 'integer'
|
@@ -91,7 +91,7 @@ module Theman
|
|
91
91
|
sql.join(' ')
|
92
92
|
end
|
93
93
|
|
94
|
-
def quote_column_name(name) #:nodoc
|
94
|
+
def quote_column_name(name) #:nodoc:
|
95
95
|
@connection.quote_ident(name.to_s)
|
96
96
|
end
|
97
97
|
end
|
data/lib/theman/object.rb
CHANGED
@@ -1,5 +1,13 @@
|
|
1
1
|
module Theman
|
2
2
|
class Object
|
3
|
+
# create a new basic model object
|
4
|
+
# ==== Parameters
|
5
|
+
# * +table_name+ - the name of the table created by Theman::Agency
|
6
|
+
# * +parent+ - optional parent object for the new basic model object
|
7
|
+
# usually ActiveRecord::Base
|
8
|
+
# * +conn+ - optional pg connection
|
9
|
+
# ==== Example
|
10
|
+
# my_model = Theman::Object.new(agent.table_name, ActiveRecord::Base)
|
3
11
|
def self.new(table_name, parent = ::Object, conn = nil)
|
4
12
|
Class.new(parent) do
|
5
13
|
unless conn.nil?
|
data/lib/theman/version.rb
CHANGED
metadata
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: theman
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
hash: 31
|
5
|
+
prerelease:
|
5
6
|
segments:
|
6
7
|
- 0
|
7
8
|
- 1
|
8
|
-
-
|
9
|
-
version: 0.1.
|
9
|
+
- 2
|
10
|
+
version: 0.1.2
|
10
11
|
platform: ruby
|
11
12
|
authors:
|
12
13
|
- Rufus Post
|
@@ -14,7 +15,7 @@ autorequire:
|
|
14
15
|
bindir: bin
|
15
16
|
cert_chain: []
|
16
17
|
|
17
|
-
date: 2011-02-
|
18
|
+
date: 2011-02-28 00:00:00 +11:00
|
18
19
|
default_executable:
|
19
20
|
dependencies:
|
20
21
|
- !ruby/object:Gem::Dependency
|
@@ -25,6 +26,7 @@ dependencies:
|
|
25
26
|
requirements:
|
26
27
|
- - ">="
|
27
28
|
- !ruby/object:Gem::Version
|
29
|
+
hash: 23
|
28
30
|
segments:
|
29
31
|
- 1
|
30
32
|
- 0
|
@@ -40,6 +42,7 @@ dependencies:
|
|
40
42
|
requirements:
|
41
43
|
- - ">="
|
42
44
|
- !ruby/object:Gem::Version
|
45
|
+
hash: 15
|
43
46
|
segments:
|
44
47
|
- 2
|
45
48
|
- 0
|
@@ -55,6 +58,7 @@ dependencies:
|
|
55
58
|
requirements:
|
56
59
|
- - ">="
|
57
60
|
- !ruby/object:Gem::Version
|
61
|
+
hash: 7
|
58
62
|
segments:
|
59
63
|
- 3
|
60
64
|
- 0
|
@@ -70,6 +74,7 @@ dependencies:
|
|
70
74
|
requirements:
|
71
75
|
- - ">="
|
72
76
|
- !ruby/object:Gem::Version
|
77
|
+
hash: 3
|
73
78
|
segments:
|
74
79
|
- 0
|
75
80
|
version: "0"
|
@@ -83,6 +88,7 @@ dependencies:
|
|
83
88
|
requirements:
|
84
89
|
- - ">="
|
85
90
|
- !ruby/object:Gem::Version
|
91
|
+
hash: 3
|
86
92
|
segments:
|
87
93
|
- 0
|
88
94
|
version: "0"
|
@@ -137,6 +143,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
137
143
|
requirements:
|
138
144
|
- - ">="
|
139
145
|
- !ruby/object:Gem::Version
|
146
|
+
hash: 3
|
140
147
|
segments:
|
141
148
|
- 0
|
142
149
|
version: "0"
|
@@ -145,6 +152,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
145
152
|
requirements:
|
146
153
|
- - ">="
|
147
154
|
- !ruby/object:Gem::Version
|
155
|
+
hash: 23
|
148
156
|
segments:
|
149
157
|
- 1
|
150
158
|
- 3
|
@@ -153,7 +161,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
153
161
|
requirements: []
|
154
162
|
|
155
163
|
rubyforge_project: theman
|
156
|
-
rubygems_version: 1.
|
164
|
+
rubygems_version: 1.5.2
|
157
165
|
signing_key:
|
158
166
|
specification_version: 3
|
159
167
|
summary: PostgreSQL AR temporary table generator using PostgreSQL COPY
|