christiank-turntable 0.6.1 → 0.6.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 +21 -10
  2. metadata +2 -2
data/turntable.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  #
2
- # turntable v0.6.1
2
+ # turntable v0.6.2
3
3
  # Christian Koch <ckoch002@student.ucr.edu>
4
4
  #
5
5
 
@@ -8,7 +8,7 @@ require 'ostruct'
8
8
 
9
9
  class Turntable < Array
10
10
 
11
- @@version = 'v0.6.1'
11
+ @@version = 'v0.6.2'
12
12
 
13
13
  # Creates a new Turntable database.
14
14
  def initialize filename, *columns
@@ -22,18 +22,26 @@ class Turntable < Array
22
22
  self.save_to @filename
23
23
  end
24
24
 
25
- # Loads an existing Turntable.
26
- # TODO: @filename isn't being set to the new filename because it's
27
- # Marshalling back the old @filename.
25
+ # Loads an existing Turntable. You can't just Marshal.load() the database
26
+ # because that also marshals back the old @filename, in case the database is
27
+ # moved or renamed. Instead, we load the old database and then reassign
28
+ # its @filename. However, I don't want @filename to be writeable by the user.
29
+ # So, I'm exploiting the Object#send hack, which may or may not be a good
30
+ # idea. There's got to be a non-controversial way of doing this.
28
31
  def Turntable.load filename
29
32
  @filename = File.expand_path(filename)
33
+
30
34
  f = File.open @filename
31
- Marshal.load f
35
+ turn = Marshal.load f
36
+ f.close
37
+
38
+ turn.send :filename=, @filename
39
+ turn
32
40
  end
33
41
 
34
42
  # Reader methods for this Turntable's metadata.
35
43
  def Turntable.version; @@version; end
36
- attr_reader :filename, :columns
44
+ attr_reader :columns, :filename
37
45
 
38
46
  # Inserts a new column to the database. Fills all the pre-existing rows with
39
47
  # nil. Returns the new database.
@@ -49,7 +57,7 @@ class Turntable < Array
49
57
  before_table = self.dup
50
58
  what_wasnt_deleted = self.delete_if { |row| row.id == id }
51
59
  self.save_to @filename
52
- return (before_table - what_wasnt_deleted).first
60
+ (before_table - what_wasnt_deleted).first
53
61
  end
54
62
 
55
63
  # Pushes self with a new row. Returns the newly created row. Requires the
@@ -66,7 +74,7 @@ class Turntable < Array
66
74
 
67
75
  self.push row
68
76
  self.save_to @filename
69
- return row
77
+ row
70
78
  end
71
79
 
72
80
  # Prints the entire database to STDOUT. It's just a wrapper for
@@ -91,6 +99,9 @@ class Turntable < Array
91
99
  f.close
92
100
  end
93
101
 
102
+ # Users can't reassign @filename themselves.
103
+ attr_writer :filename
104
+
94
105
  end
95
106
 
96
107
  class Array
@@ -103,7 +114,7 @@ class Array
103
114
  end
104
115
  puts '|'
105
116
  end
106
- return nil
117
+ nil
107
118
  end
108
119
 
109
120
  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.1
4
+ version: 0.6.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christian Koch
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-06-15 00:00:00 -07:00
12
+ date: 2009-06-16 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15