christiank-turntable 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/turntable.rb +140 -0
  2. metadata +53 -0
data/turntable.rb ADDED
@@ -0,0 +1,140 @@
1
+ #
2
+ # turntable v0.3.2
3
+ # Christian Koch < ckoch002@student.ucr.edu >
4
+ #
5
+ # This is free software. You are permitted to modify, implement, and otherwise
6
+ # use this source code freely (as in beer and freedom).
7
+ #
8
+
9
+ class Turntable
10
+ def initialize filename, *args
11
+ # there must be at least one column
12
+ raise "Turntable.new requires at least two arguments" if args.empty?
13
+
14
+ @columns = []
15
+ @table = []
16
+ @filename = filename
17
+
18
+ args.each do |column|
19
+ @columns.push column
20
+ end
21
+
22
+ self.save_to @filename
23
+ end
24
+
25
+ # Turntable.load: loads the file and Marshals back the data
26
+ def Turntable.load filename
27
+ raise "#{filename}: file does not exist" unless File.exists?(filename)
28
+
29
+ @filename = filename
30
+ f = File.open @filename, 'r'
31
+ return Marshal.load(f)
32
+ f.close
33
+ end
34
+
35
+ attr_reader :columns
36
+ attr_reader :filename
37
+
38
+ def delete hash
39
+ raise 'delete() requires :id => some_value' unless hash.has_key?(:id)
40
+
41
+ @table.delete_if { |row| row[:id] == hash[:id] }
42
+ self.save_to @filename
43
+ end
44
+
45
+ def insert *args
46
+ # the number of arguments must match the number of columns
47
+ unless args.length == @columns.length
48
+ raise "wrong number of arguments (#{args.length} for #{@columns.length})"
49
+ end
50
+
51
+ row = {}
52
+
53
+ # columns named :id are like AUTOINCREMENT fields,
54
+ # every other column is type-independent
55
+ @columns.each do |column_name|
56
+ if column_name == :id
57
+ if @table.empty?
58
+ row[:id] = 0
59
+ else
60
+ row[:id] = @table.last[:id] + 1
61
+ end
62
+
63
+ args.shift
64
+ else
65
+ row[column_name] = args.shift
66
+ end
67
+ end
68
+
69
+ # insert the data here:
70
+ @table.push row
71
+ self.save_to @filename
72
+
73
+ return row
74
+ end
75
+
76
+ # inspect: this makes dealing with Turntable in IRB much easier
77
+ def inspect
78
+ self.to_s
79
+ end
80
+
81
+ # puts_formatted: prints a "plain text" table
82
+ def puts_formatted
83
+ @table.each do |row|
84
+ @columns.each do |column|
85
+ print "|#{row[column]}"
86
+ end
87
+ puts "|"
88
+ end
89
+
90
+ return nil
91
+ end
92
+
93
+ # select: equivalent to SELECT * WHERE statements
94
+ def select hash
95
+ result_set = []
96
+
97
+ hash.each do |column, value|
98
+ this_set = @table.collect do |row|
99
+ row = row if row[column] == value
100
+ end
101
+
102
+ result_set += this_set.compact
103
+ end
104
+
105
+ return result_set
106
+ end
107
+
108
+ def select_all
109
+ return @table
110
+ end
111
+
112
+ # update: replace many columns at a time,
113
+ # but only on one row at a time
114
+ def update hash
115
+ raise 'update() requires at least :id => some_value' unless hash.has_key?(:id)
116
+
117
+ id = hash[:id]
118
+ hash.delete_if { |column, value| column == :id }
119
+
120
+ hash.each do |column, value|
121
+ @table[id][column] = value
122
+ end
123
+
124
+ self.save_to @filename
125
+ return @table[id]
126
+ end
127
+
128
+ protected
129
+ # save_to: marshals in the Turntable object
130
+ def save_to filename
131
+ if File.exists? filename
132
+ f = File.open filename, 'w'
133
+ else
134
+ f = File.new filename, 'w'
135
+ end
136
+
137
+ Marshal.dump self, f
138
+ f.close
139
+ end
140
+ end
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: christiank-turntable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.2
5
+ platform: ruby
6
+ authors:
7
+ - Christian Koch
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-21 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: ckoch002@student.ucr.edu
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - turntable.rb
26
+ has_rdoc: false
27
+ homepage: http://github.com/christiank/Turntable
28
+ post_install_message:
29
+ rdoc_options: []
30
+
31
+ require_paths:
32
+ - .
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: "0"
38
+ version:
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ requirements: []
46
+
47
+ rubyforge_project:
48
+ rubygems_version: 1.2.0
49
+ signing_key:
50
+ specification_version: 2
51
+ summary: Turntable is an alternative to relational databases for Ruby applications.
52
+ test_files: []
53
+