christiank-turntable 0.6.3 → 0.6.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/bin/sqlite3-to-turntable +40 -0
  2. data/turntable.rb +32 -18
  3. metadata +4 -4
@@ -0,0 +1,40 @@
1
+ #!/usr/bin/ruby
2
+ #
3
+ # sqlite3-to-turntable
4
+ # Christian Koch <ckoch002@student.ucr.edu>
5
+ #
6
+
7
+ require 'rubygems'
8
+ require 'sqlite3'
9
+ require 'turntable'
10
+
11
+ # There must be 2 arguments
12
+ if ARGV.length != 2
13
+ puts 'Usage: sqlite3-to-turntable [sqlite3 database] [column name]'
14
+ exit 1
15
+ end
16
+
17
+ # Find out about the SQLite3 database
18
+ db_filename = ARGV[0].split('.').first
19
+ sqlite_db = SQLite3::Database.new ARGV[0]
20
+
21
+ sqlite_db_columns = sqlite_db.query("select * from #{ARGV[1]}") { |result| result.columns }.dup
22
+ sqlite_db_rows = sqlite_db.execute "select * from #{ARGV[1]}"
23
+
24
+ p sqlite_db_columns
25
+
26
+ # Turntable ids and SQL ids are different...
27
+ if sqlite_db_columns.index 'id'
28
+ sqlite_db_columns[sqlite_db_columns.index('id')] = 'old_id'
29
+ end
30
+
31
+ # Convert the remaining column names to symbols
32
+ sqlite_db_columns.collect! { |column| column.to_sym }
33
+
34
+ # Create the new Turntable
35
+ turntable_db = Turntable.new "#{db_filename}.turn", sqlite_db_columns
36
+
37
+ # Fill up the Turntable
38
+ sqlite_db_rows.each { |row| turntable_db.insert_array row }
39
+
40
+ puts "Created #{turntable_db.filename}"
data/turntable.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  #
2
- # turntable v0.6.3
2
+ # turntable v0.6.4
3
3
  # Christian Koch <ckoch002@student.ucr.edu>
4
4
  #
5
5
 
@@ -8,16 +8,33 @@ require 'ostruct'
8
8
 
9
9
  class Turntable < Array
10
10
 
11
- @@version = 'v0.6.3'
11
+ @@version = 'v0.6.4'
12
12
 
13
- # Creates a new Turntable database.
13
+ @@insert_proc = %q(
14
+ if args.length != @columns.length
15
+ raise ArgumentError, "wrong number of arguments (#{args.length} for #{@columns.length})"
16
+ end
17
+ row = Row.new
18
+ self.empty? ? (row.id = 0) : (row.id = self.last.id + 1)
19
+ @columns.each { |column| eval "row.#{column} = args.shift" }
20
+ self.push row
21
+ self.save_to @filename
22
+ row)
23
+
24
+ # Creates a new Turntable database. Accepts either a list of column names, or
25
+ # an array of symbols which contain the same thing.
14
26
  def initialize filename, *columns
15
27
  if columns.empty?
16
28
  raise ArgumentError, 'Turntable.new() requires a filename and at least one column name'
17
29
  end
18
30
 
31
+ if columns.length == 1 and columns[0].is_a?(Array) and columns[0][0].is_a?(Symbol)
32
+ @columns = columns[0]
33
+ else
34
+ @columns = columns
35
+ end
36
+
19
37
  @filename = File.expand_path(filename)
20
- @columns = columns
21
38
 
22
39
  self.save_to @filename
23
40
  end
@@ -61,20 +78,17 @@ class Turntable < Array
61
78
  end
62
79
 
63
80
  # Pushes self with a new row. Returns the newly created row. Requires the
64
- # number of arguments passed in equal the number of columns in the database.
81
+ # number of arguments passed in equal the number of columns in the database,
82
+ # or it can accept calls from Turntable#insert_array.
65
83
  def insert *args
66
- if args.length != @columns.length
67
- raise ArgumentError, "wrong number of arguments (#{args.length} for #{@columns.length})"
68
- end
69
-
70
- row = Row.new
71
- self.empty? ? (row.id = 0) : (row.id = self.last.id + 1)
72
-
73
- @columns.each { |column| eval "row.#{column} = args.shift" }
74
-
75
- self.push row
76
- self.save_to @filename
77
- row
84
+ eval @@insert_proc
85
+ end
86
+
87
+ # Same as Turntable#insert, except this method accepts only one array filled
88
+ # with values.
89
+ def insert_array args
90
+ raise ArgumentError, 'Turntable#insert_array requires an array' unless args.is_a?(Array)
91
+ eval @@insert_proc
78
92
  end
79
93
 
80
94
  # Custom inspect() makes working in IRB much cleaner.
@@ -104,7 +118,7 @@ class Turntable < Array
104
118
  f.close
105
119
  end
106
120
 
107
- # Users can't reassign @filename themselves.
121
+ # Users shouldn't reassign @filename themselves.
108
122
  attr_writer :filename
109
123
 
110
124
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: christiank-turntable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.3
4
+ version: 0.6.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christian Koch
@@ -9,14 +9,14 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-06-17 00:00:00 -07:00
12
+ date: 2009-06-18 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
16
16
  description:
17
17
  email: ckoch002@student.ucr.edu
18
- executables: []
19
-
18
+ executables:
19
+ - sqlite3-to-turntable
20
20
  extensions: []
21
21
 
22
22
  extra_rdoc_files: []