table_maker 0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/Gemfile +19 -0
- data/Gemfile.lock +24 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +36 -0
- data/Rakefile +53 -0
- data/VERSION +1 -0
- data/lib/table_maker.rb +51 -0
- data/test/helper.rb +17 -0
- data/test/test_table_maker.rb +31 -0
- metadata +124 -0
data/.document
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
gem 'sequel'
|
6
|
+
|
7
|
+
# Add dependencies to develop your gem here.
|
8
|
+
# Include everything needed to run rake, tests, features, etc.
|
9
|
+
group :development do
|
10
|
+
gem "bundler", "~> 1.0.0"
|
11
|
+
gem "jeweler", "~> 1.5.2"
|
12
|
+
gem "rcov", ">= 0"
|
13
|
+
|
14
|
+
if RUBY_PLATFORM == 'java'
|
15
|
+
gem 'jdbc-sqlite3'
|
16
|
+
else
|
17
|
+
gem 'sqlite3'
|
18
|
+
end
|
19
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
git (1.2.5)
|
5
|
+
jdbc-sqlite3 (3.6.14.2.056-java)
|
6
|
+
jeweler (1.5.2)
|
7
|
+
bundler (~> 1.0.0)
|
8
|
+
git (>= 1.2.5)
|
9
|
+
rake
|
10
|
+
rake (0.8.7)
|
11
|
+
rcov (0.9.9)
|
12
|
+
rcov (0.9.9-java)
|
13
|
+
sequel (3.20.0)
|
14
|
+
|
15
|
+
PLATFORMS
|
16
|
+
java
|
17
|
+
ruby
|
18
|
+
|
19
|
+
DEPENDENCIES
|
20
|
+
bundler (~> 1.0.0)
|
21
|
+
jdbc-sqlite3
|
22
|
+
jeweler (~> 1.5.2)
|
23
|
+
rcov
|
24
|
+
sequel
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Jeremy Stephens
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
= table_maker
|
2
|
+
|
3
|
+
Simple ASCII table importer for Sequel.
|
4
|
+
|
5
|
+
Example use:
|
6
|
+
require 'sequel'
|
7
|
+
require 'table_maker'
|
8
|
+
|
9
|
+
db = Sequel.sqlite
|
10
|
+
TableMaker.new(db, :foo, <<-EOF)
|
11
|
+
+-------------+-------------+--------------+
|
12
|
+
| foo(String) | bar(String) | baz(Integer) |
|
13
|
+
+=============+=============+==============+
|
14
|
+
| abc | def | 123 |
|
15
|
+
| ghi | jkl | 456 |
|
16
|
+
| mno | pqr | 789 |
|
17
|
+
| stu | vwx | 000 |
|
18
|
+
+-------------+-------------+--------------+
|
19
|
+
EOF
|
20
|
+
db[:foo].all #=> [{:id=>1, :foo=>"abc", :bar=>"def", :baz=>123}, ...]
|
21
|
+
|
22
|
+
== Contributing to table_maker
|
23
|
+
|
24
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
25
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
26
|
+
* Fork the project
|
27
|
+
* Start a feature/bugfix branch
|
28
|
+
* Commit and push until you are happy with your contribution
|
29
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
30
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
31
|
+
|
32
|
+
== Copyright
|
33
|
+
|
34
|
+
Copyright (c) 2011 Jeremy Stephens. See LICENSE.txt for
|
35
|
+
further details.
|
36
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'rake'
|
11
|
+
|
12
|
+
require 'jeweler'
|
13
|
+
Jeweler::Tasks.new do |gem|
|
14
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
15
|
+
gem.name = "table_maker"
|
16
|
+
gem.homepage = "http://github.com/viking/table_maker"
|
17
|
+
gem.license = "MIT"
|
18
|
+
gem.summary = %Q{Simple ASCII table importer for Sequel}
|
19
|
+
gem.description = %Q{Simple ASCII table importer for Sequel}
|
20
|
+
gem.email = "viking@pillageandplunder.net"
|
21
|
+
gem.authors = ["Jeremy Stephens"]
|
22
|
+
# Include your dependencies below. Runtime dependencies are required when using your gem,
|
23
|
+
# and development dependencies are only needed for development (ie running rake tasks, tests, etc)
|
24
|
+
# gem.add_runtime_dependency 'jabber4r', '> 0.1'
|
25
|
+
# gem.add_development_dependency 'rspec', '> 1.2.3'
|
26
|
+
end
|
27
|
+
Jeweler::RubygemsDotOrgTasks.new
|
28
|
+
|
29
|
+
require 'rake/testtask'
|
30
|
+
Rake::TestTask.new(:test) do |test|
|
31
|
+
test.libs << 'lib' << 'test'
|
32
|
+
test.pattern = 'test/**/test_*.rb'
|
33
|
+
test.verbose = true
|
34
|
+
end
|
35
|
+
|
36
|
+
require 'rcov/rcovtask'
|
37
|
+
Rcov::RcovTask.new do |test|
|
38
|
+
test.libs << 'test'
|
39
|
+
test.pattern = 'test/**/test_*.rb'
|
40
|
+
test.verbose = true
|
41
|
+
end
|
42
|
+
|
43
|
+
task :default => :test
|
44
|
+
|
45
|
+
require 'rake/rdoctask'
|
46
|
+
Rake::RDocTask.new do |rdoc|
|
47
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
48
|
+
|
49
|
+
rdoc.rdoc_dir = 'rdoc'
|
50
|
+
rdoc.title = "table_maker #{version}"
|
51
|
+
rdoc.rdoc_files.include('README*')
|
52
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
53
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.0
|
data/lib/table_maker.rb
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'sequel'
|
2
|
+
|
3
|
+
class TableMaker
|
4
|
+
def initialize(db, table_name, layout)
|
5
|
+
@db = db
|
6
|
+
@table_name = table_name.to_sym
|
7
|
+
@column_info = []
|
8
|
+
@column_names = []
|
9
|
+
@data = []
|
10
|
+
parse_layout(layout)
|
11
|
+
create_table
|
12
|
+
import_data
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
def parse_layout(layout)
|
17
|
+
layout.each_line do |line|
|
18
|
+
line.chomp!
|
19
|
+
line.strip!
|
20
|
+
next if line =~ /^[-+|=]*$/
|
21
|
+
|
22
|
+
parts = line.split(/\s*\|\s*/)
|
23
|
+
parts.shift
|
24
|
+
|
25
|
+
if @column_info.empty?
|
26
|
+
parts.each do |str|
|
27
|
+
md = str.match(/(\w+)\((\w+)\)/)
|
28
|
+
name = md[1].to_sym
|
29
|
+
type = Object.module_eval("::#{md[2]}", __FILE__, __LINE__)
|
30
|
+
@column_info << { :name => name, :type => type }
|
31
|
+
@column_names << name
|
32
|
+
end
|
33
|
+
else
|
34
|
+
@data << parts
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def create_table
|
40
|
+
column_info = @column_info
|
41
|
+
@db.create_table(@table_name) do
|
42
|
+
primary_key :id
|
43
|
+
@columns = column_info
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def import_data
|
48
|
+
ds = @db[@table_name]
|
49
|
+
ds.import(@column_names, @data)
|
50
|
+
end
|
51
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'test/unit'
|
11
|
+
|
12
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
13
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
14
|
+
require 'table_maker'
|
15
|
+
|
16
|
+
class Test::Unit::TestCase
|
17
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestTableMaker < Test::Unit::TestCase
|
4
|
+
def test_creates_simple_table_with_auto_id
|
5
|
+
db = Sequel.connect("#{RUBY_PLATFORM == 'java' ? "jdbc:" : ""}sqlite::memory:")
|
6
|
+
TableMaker.new(db, :foo, <<-EOF)
|
7
|
+
+-------------+-------------+--------------+
|
8
|
+
| foo(String) | bar(String) | baz(Integer) |
|
9
|
+
+=============+=============+==============+
|
10
|
+
| abc | def | 123 |
|
11
|
+
| ghi | jkl | 456 |
|
12
|
+
| mno | pqr | 789 |
|
13
|
+
| stu | vwx | 000 |
|
14
|
+
+-------------+-------------+--------------+
|
15
|
+
EOF
|
16
|
+
assert db.tables.include?(:foo)
|
17
|
+
schema = db.schema(:foo)
|
18
|
+
assert_equal [:id, :foo, :bar, :baz], schema.collect(&:first)
|
19
|
+
assert_equal [:integer, :string, :string, :integer], schema.collect { |c| c[1][:type] }
|
20
|
+
assert schema.assoc(:id)[1][:primary_key]
|
21
|
+
|
22
|
+
ds = db[:foo]
|
23
|
+
expected = [
|
24
|
+
{:id => 1, :foo => 'abc', :bar => 'def', :baz => 123},
|
25
|
+
{:id => 2, :foo => 'ghi', :bar => 'jkl', :baz => 456},
|
26
|
+
{:id => 3, :foo => 'mno', :bar => 'pqr', :baz => 789},
|
27
|
+
{:id => 4, :foo => 'stu', :bar => 'vwx', :baz => 000}
|
28
|
+
]
|
29
|
+
assert_equal expected, ds.select(:id, :foo, :bar, :baz).order(:id).all
|
30
|
+
end
|
31
|
+
end
|
metadata
ADDED
@@ -0,0 +1,124 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: table_maker
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.0
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jeremy Stephens
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-03-01 00:00:00 -06:00
|
14
|
+
default_executable:
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: sequel
|
18
|
+
version_requirements: &id001 !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
requirement: *id001
|
25
|
+
prerelease: false
|
26
|
+
type: :runtime
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
version_requirements: &id002 !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ~>
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: 1.0.0
|
35
|
+
requirement: *id002
|
36
|
+
prerelease: false
|
37
|
+
type: :development
|
38
|
+
- !ruby/object:Gem::Dependency
|
39
|
+
name: jeweler
|
40
|
+
version_requirements: &id003 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 1.5.2
|
46
|
+
requirement: *id003
|
47
|
+
prerelease: false
|
48
|
+
type: :development
|
49
|
+
- !ruby/object:Gem::Dependency
|
50
|
+
name: rcov
|
51
|
+
version_requirements: &id004 !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: "0"
|
57
|
+
requirement: *id004
|
58
|
+
prerelease: false
|
59
|
+
type: :development
|
60
|
+
- !ruby/object:Gem::Dependency
|
61
|
+
name: jdbc-sqlite3
|
62
|
+
version_requirements: &id005 !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: "0"
|
68
|
+
requirement: *id005
|
69
|
+
prerelease: false
|
70
|
+
type: :development
|
71
|
+
description: Simple ASCII table importer for Sequel
|
72
|
+
email: viking@pillageandplunder.net
|
73
|
+
executables: []
|
74
|
+
|
75
|
+
extensions: []
|
76
|
+
|
77
|
+
extra_rdoc_files:
|
78
|
+
- LICENSE.txt
|
79
|
+
- README.rdoc
|
80
|
+
files:
|
81
|
+
- .document
|
82
|
+
- Gemfile
|
83
|
+
- Gemfile.lock
|
84
|
+
- LICENSE.txt
|
85
|
+
- README.rdoc
|
86
|
+
- Rakefile
|
87
|
+
- VERSION
|
88
|
+
- lib/table_maker.rb
|
89
|
+
- test/helper.rb
|
90
|
+
- test/test_table_maker.rb
|
91
|
+
has_rdoc: true
|
92
|
+
homepage: http://github.com/viking/table_maker
|
93
|
+
licenses:
|
94
|
+
- MIT
|
95
|
+
post_install_message:
|
96
|
+
rdoc_options: []
|
97
|
+
|
98
|
+
require_paths:
|
99
|
+
- lib
|
100
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
101
|
+
none: false
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
hash: 2
|
106
|
+
segments:
|
107
|
+
- 0
|
108
|
+
version: "0"
|
109
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
110
|
+
none: false
|
111
|
+
requirements:
|
112
|
+
- - ">="
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: "0"
|
115
|
+
requirements: []
|
116
|
+
|
117
|
+
rubyforge_project:
|
118
|
+
rubygems_version: 1.5.0
|
119
|
+
signing_key:
|
120
|
+
specification_version: 3
|
121
|
+
summary: Simple ASCII table importer for Sequel
|
122
|
+
test_files:
|
123
|
+
- test/helper.rb
|
124
|
+
- test/test_table_maker.rb
|