theman 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -14,9 +14,9 @@ tmtags
14
14
  *.swp
15
15
 
16
16
  ## PROJECT::GENERAL
17
- coverage
18
- rdoc
19
- pkg
17
+ pkg/*
18
+ *.gem
19
+ .bundle
20
20
 
21
21
  ## PROJECT::SPECIFIC
22
22
  spec/database.yml
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source :gemcutter
2
+
3
+ # Specify your gem's dependencies in theman.gemspec
4
+ gemspec
data/Rakefile CHANGED
@@ -1,18 +1,2 @@
1
- require 'rubygems'
2
- require 'rake'
3
-
4
- begin
5
- require 'jeweler'
6
- Jeweler::Tasks.new do |gem|
7
- gem.name = "theman"
8
- gem.summary = %Q{PostgreSQL AR temporary table generator using PostgreSQL COPY}
9
- gem.description = %Q{longer description of your gem}
10
- gem.email = "rufuspost@gmail.com"
11
- gem.homepage = "http://github.com/mynameisrufus/theman"
12
- gem.authors = ["Rufus Post"]
13
- gem.add_development_dependency "rspec", ">= 2.0.0.beta.20"
14
- end
15
- Jeweler::GemcutterTasks.new
16
- rescue LoadError
17
- puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
18
- end
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -1,8 +1,15 @@
1
1
  module Theman
2
2
  class Agency
3
- attr_reader :instance, :table_proc, :null_replacements, :stream_location, :column_names
3
+ attr_reader :instance, :column_names, :null_replacements, :sed_commands
4
4
 
5
- def initialize(*args)
5
+ attr_writer :stream
6
+
7
+ def initialize(stream = nil, parent = ::ActiveRecord::Base)
8
+ # source of the data
9
+ @stream = stream
10
+
11
+ # create a new class that extends an active record model
12
+ # use instance_parent(klass) if not ActiveRecord::Base
6
13
  cabinet_id = "c#{10.times.map{rand(9)}.join}"
7
14
  @column_names = {}
8
15
  @instance = Class.new(parent) do
@@ -16,34 +23,24 @@ module Theman
16
23
  end
17
24
  EOV
18
25
  end
19
- yield self if block_given?
20
- unless args.first.nil?
21
- @stream_location = args.first
26
+
27
+ # if stream given table will be created
28
+ # other wise create_table and pipe_it will need to called
29
+ # proceduraly
30
+ if stream
31
+ if block_given?
32
+ yield self
33
+ end
22
34
  create_table
23
35
  pipe_it
24
36
  end
25
37
  end
26
-
27
- def instance_parent(klass)
28
- @parent = klass
29
- end
30
38
 
31
- def table(&block)
39
+ def table
32
40
  yield self if block_given?
33
41
  end
34
42
 
35
- def nulls(*args)
36
- @null_replacements = args
37
- end
38
-
39
- def seds(*args)
40
- @extra_seds = args
41
- end
42
-
43
- def symbolize(name)
44
- name.gsub(/ /,"_").gsub(/\W/, "").downcase.to_sym
45
- end
46
-
43
+ # overide ActiveRecord column types to be used in a block
47
44
  %w( string text integer float decimal datetime timestamp time date binary boolean ).each do |column_type|
48
45
  class_eval <<-EOV, __FILE__, __LINE__ + 1
49
46
  def #{column_type}(*args)
@@ -51,27 +48,14 @@ module Theman
51
48
  end
52
49
  EOV
53
50
  end
54
-
55
- protected
56
- def sed_to_s
57
- seds = []
58
- seds << "| sed #{nulls_to_sed.join(" ")}" unless @null_replacements.nil?
59
- seds << "| sed #{@extra_seds.join("| sed ")}" unless @extra_seds.nil?
60
- return seds.join(" ") unless seds.empty?
61
- end
62
51
 
63
- def nulls_to_sed
64
- @null_replacements.map do |null|
65
- "-e 's/#{null.source}//g'"
66
- end
67
- end
68
-
69
- def parent
70
- @parent ||= ::ActiveRecord::Base
52
+ # overides the default string type column
53
+ def column(name, type, options)
54
+ @column_names.merge! name.to_sym => [name, type, options]
71
55
  end
72
56
 
73
57
  def create_table
74
- f = File.open(stream_location, 'r')
58
+ f = File.open(@stream, 'r')
75
59
  instance.connection.create_table(instance.table_name, :temporary => true, :id => false) do |t|
76
60
  f.each_line do |line|
77
61
  line.split(/,/).each do |col|
@@ -87,14 +71,12 @@ module Theman
87
71
  end
88
72
  end
89
73
 
90
- def column(name, type, options)
91
- @column_names.merge! name.to_sym => [name, type, options]
92
- end
93
-
74
+ # use postgress COPY command using STDIN with CSV HEADER
75
+ # reads chunks of 8192 bytes to save memory
94
76
  def pipe_it(l = "")
95
77
  raw = instance.connection.raw_connection
96
78
  raw.query "COPY #{instance.table_name} FROM STDIN WITH CSV HEADER"
97
- command = "cat #{stream_location} #{sed_to_s}"
79
+ command = "cat #{@stream} #{seds_join}"
98
80
  f = IO.popen(command)
99
81
  begin
100
82
  while f.read(8192, l)
@@ -105,5 +87,34 @@ module Theman
105
87
  end
106
88
  raw.put_copy_end
107
89
  end
90
+
91
+ def nulls(*args)
92
+ @null_replacements = args
93
+ end
94
+
95
+ def seds(*args)
96
+ @sed_commands = args
97
+ end
98
+
99
+ def symbolize(name)
100
+ name.gsub(/ /,"_").gsub(/\W/, "").downcase.to_sym
101
+ end
102
+
103
+ # join together the sed commands to apply to stream
104
+ def seds_join(commands = [])
105
+ unless null_replacements.nil?
106
+ commands << "| sed #{nulls_to_sed.join(" ")}"
107
+ end
108
+ unless sed_commands.nil?
109
+ commands << "| sed #{sed_commands.join("| sed ")}"
110
+ end
111
+ commands.join(" ")
112
+ end
113
+
114
+ def nulls_to_sed
115
+ @null_replacements.map do |null|
116
+ "-e 's/#{null.source}//g'"
117
+ end
118
+ end
108
119
  end
109
120
  end
@@ -0,0 +1,3 @@
1
+ module Theman
2
+ VERSION = "0.0.3"
3
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,15 +1,16 @@
1
1
  $LOAD_PATH.unshift(File.dirname(__FILE__))
2
2
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
- require 'rails'
4
3
 
5
4
  require 'active_record'
6
- Rails.env = 'test'
5
+ require 'fileutils'
6
+ require 'logger'
7
+ require 'rspec'
8
+ require 'rspec/autorun'
9
+ require 'theman'
10
+
7
11
  ActiveRecord::Base.configurations = YAML.load_file(File.join("spec", "database.yml"))
8
12
  FileUtils.mkdir_p "#{Dir.pwd}/log"
9
- logfile= File.new("#{Dir.pwd}/log/database.log", "w")
13
+ logfile = "#{Dir.pwd}/log/database.log"
10
14
  ActiveRecord::Base.logger = Logger.new(File.open(logfile, 'w'))
11
- ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations.fetch(Rails.env))
15
+ ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations.fetch('test'))
12
16
 
13
- require 'theman'
14
- require 'rspec'
15
- require 'rspec/autorun'
data/theman.gemspec CHANGED
@@ -1,55 +1,24 @@
1
- # Generated by jeweler
2
- # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
1
  # -*- encoding: utf-8 -*-
2
+ require File.expand_path("../lib/theman/version", __FILE__)
5
3
 
6
4
  Gem::Specification.new do |s|
7
- s.name = %q{theman}
8
- s.version = "0.0.2"
5
+ s.name = "theman"
6
+ s.version = Theman::VERSION
7
+ s.platform = Gem::Platform::RUBY
8
+ s.authors = ["Rufus Post"]
9
+ s.email = ["rufuspost@gmail.com"]
10
+ s.homepage = "http://github.com/mynameisrufus/theman"
11
+ s.summary = "PostgreSQL AR temporary table generator using PostgreSQL COPY"
12
+ s.description = "FasterCSV is great and all but when you get to 100mb files it takes a while and you may only be looking for certain records that match some criteria, enter theman"
9
13
 
10
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = ["Rufus Post"]
12
- s.date = %q{2010-10-26}
13
- s.description = %q{longer description of your gem}
14
- s.email = %q{rufuspost@gmail.com}
15
- s.extra_rdoc_files = [
16
- "README.rdoc"
17
- ]
18
- s.files = [
19
- ".document",
20
- ".gitignore",
21
- "README.rdoc",
22
- "Rakefile",
23
- "VERSION",
24
- "lib/theman.rb",
25
- "lib/theman/themans_agency.rb",
26
- "spec/fixtures/temp_one.csv",
27
- "spec/fixtures/temp_two.csv",
28
- "spec/spec_helper.rb",
29
- "spec/theman_spec.rb",
30
- "theman.gemspec"
31
- ]
32
- s.homepage = %q{http://github.com/mynameisrufus/theman}
33
- s.rdoc_options = ["--charset=UTF-8"]
34
- s.require_paths = ["lib"]
35
- s.rubygems_version = %q{1.3.7}
36
- s.summary = %q{PostgreSQL AR temporary table generator using PostgreSQL COPY}
37
- s.test_files = [
38
- "spec/theman_spec.rb",
39
- "spec/spec_helper.rb"
40
- ]
14
+ s.required_rubygems_version = ">= 1.3.6"
15
+ s.rubyforge_project = "theman"
41
16
 
42
- if s.respond_to? :specification_version then
43
- current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
44
- s.specification_version = 3
17
+ s.add_development_dependency "bundler", ">= 1.0.0"
18
+ s.add_development_dependency "rspec", ">= 2.0.0"
19
+ s.add_development_dependency "activerecord", ">= 3.0.0"
45
20
 
46
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
47
- s.add_development_dependency(%q<rspec>, [">= 2.0.0.beta.20"])
48
- else
49
- s.add_dependency(%q<rspec>, [">= 2.0.0.beta.20"])
50
- end
51
- else
52
- s.add_dependency(%q<rspec>, [">= 2.0.0.beta.20"])
53
- end
21
+ s.files = `git ls-files`.split("\n")
22
+ s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
23
+ s.require_path = 'lib'
54
24
  end
55
-
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 2
9
- version: 0.0.2
8
+ - 3
9
+ version: 0.0.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - Rufus Post
@@ -14,11 +14,11 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-10-26 00:00:00 +11:00
17
+ date: 2010-10-28 00:00:00 +11:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
- name: rspec
21
+ name: bundler
22
22
  prerelease: false
23
23
  requirement: &id001 !ruby/object:Gem::Requirement
24
24
  none: false
@@ -26,30 +26,59 @@ dependencies:
26
26
  - - ">="
27
27
  - !ruby/object:Gem::Version
28
28
  segments:
29
- - 2
29
+ - 1
30
30
  - 0
31
31
  - 0
32
- - beta
33
- - 20
34
- version: 2.0.0.beta.20
32
+ version: 1.0.0
35
33
  type: :development
36
34
  version_requirements: *id001
37
- description: longer description of your gem
38
- email: rufuspost@gmail.com
35
+ - !ruby/object:Gem::Dependency
36
+ name: rspec
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ segments:
44
+ - 2
45
+ - 0
46
+ - 0
47
+ version: 2.0.0
48
+ type: :development
49
+ version_requirements: *id002
50
+ - !ruby/object:Gem::Dependency
51
+ name: activerecord
52
+ prerelease: false
53
+ requirement: &id003 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ segments:
59
+ - 3
60
+ - 0
61
+ - 0
62
+ version: 3.0.0
63
+ type: :development
64
+ version_requirements: *id003
65
+ description: FasterCSV is great and all but when you get to 100mb files it takes a while and you may only be looking for certain records that match some criteria, enter theman
66
+ email:
67
+ - rufuspost@gmail.com
39
68
  executables: []
40
69
 
41
70
  extensions: []
42
71
 
43
- extra_rdoc_files:
44
- - README.rdoc
72
+ extra_rdoc_files: []
73
+
45
74
  files:
46
- - .document
47
75
  - .gitignore
76
+ - Gemfile
48
77
  - README.rdoc
49
78
  - Rakefile
50
- - VERSION
51
79
  - lib/theman.rb
52
80
  - lib/theman/themans_agency.rb
81
+ - lib/theman/version.rb
53
82
  - spec/fixtures/temp_one.csv
54
83
  - spec/fixtures/temp_two.csv
55
84
  - spec/spec_helper.rb
@@ -60,8 +89,8 @@ homepage: http://github.com/mynameisrufus/theman
60
89
  licenses: []
61
90
 
62
91
  post_install_message:
63
- rdoc_options:
64
- - --charset=UTF-8
92
+ rdoc_options: []
93
+
65
94
  require_paths:
66
95
  - lib
67
96
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -78,15 +107,16 @@ required_rubygems_version: !ruby/object:Gem::Requirement
78
107
  - - ">="
79
108
  - !ruby/object:Gem::Version
80
109
  segments:
81
- - 0
82
- version: "0"
110
+ - 1
111
+ - 3
112
+ - 6
113
+ version: 1.3.6
83
114
  requirements: []
84
115
 
85
- rubyforge_project:
116
+ rubyforge_project: theman
86
117
  rubygems_version: 1.3.7
87
118
  signing_key:
88
119
  specification_version: 3
89
120
  summary: PostgreSQL AR temporary table generator using PostgreSQL COPY
90
- test_files:
91
- - spec/theman_spec.rb
92
- - spec/spec_helper.rb
121
+ test_files: []
122
+
data/.document DELETED
@@ -1,5 +0,0 @@
1
- README.rdoc
2
- lib/**/*.rb
3
- bin/*
4
- features/**/*.feature
5
- LICENSE
data/VERSION DELETED
@@ -1 +0,0 @@
1
- 0.0.2