mongoose 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,43 @@
1
+ # Credit for these utility methods goes completely to Logan Capaldo.
2
+
3
+ module Mongoose
4
+
5
+ module Util
6
+ # converts from things like user_name to things like UserName
7
+ def self.us_case_to_class_case(name)
8
+ name.to_s.split(/_/).map do |word|
9
+ word.capitalize
10
+ end.join
11
+ end
12
+
13
+ # converts from things like UserName to things like user_name
14
+ def self.class_case_to_us_case(name)
15
+ decapitalized = name.to_s.sub(/^./) { |match| match.downcase }
16
+ decapitalized.gsub(/[A-Z]/) { |match| "_" + match.downcase }
17
+ end
18
+
19
+ # Adds an s
20
+ def self.pluralize(name)
21
+ name.to_s + 's'
22
+ end
23
+
24
+ # chops an s
25
+ def self.singularize(name)
26
+ if md = name.to_s.match(/^(.+?)s\z/)
27
+ md[1]
28
+ else
29
+ raise RuntimeError, "#{name} is not plural"
30
+ end
31
+ end
32
+
33
+ def self.col_name_for_class(name)
34
+ class_case_to_us_case(name) + "_id"
35
+ end
36
+
37
+ def self.class_name_for_col(name)
38
+ us_case_to_class_case(name.sub(/_id\z/, ''))
39
+ end
40
+
41
+ end
42
+
43
+ end
@@ -0,0 +1,27 @@
1
+ require 'test/unit'
2
+ require 'tmpdir'
3
+
4
+ begin
5
+ require 'rubygems'
6
+ require_gem 'Mongoose'
7
+ rescue LoadError
8
+ require 'mongoose'
9
+ end
10
+
11
+ class TestDatabase < Test::Unit::TestCase
12
+ def setup
13
+ @db = Mongoose::Database.new(:path => Dir.tmpdir)
14
+ end
15
+
16
+ def teardown
17
+ @db.close
18
+ end
19
+
20
+ def test_db_created
21
+ assert(@db.is_a?(Mongoose::Database))
22
+ end
23
+
24
+ def test_path
25
+ assert_equal(Dir.tmpdir, @db.path)
26
+ end
27
+ end
@@ -0,0 +1,167 @@
1
+ require 'test/unit'
2
+ require 'tmpdir'
3
+
4
+ begin
5
+ require 'rubygems'
6
+ require_gem 'Mongoose'
7
+ rescue LoadError
8
+ require 'mongoose'
9
+ end
10
+
11
+ class Plane < Mongoose::Table
12
+ end
13
+
14
+ class TestTable < Test::Unit::TestCase
15
+ def setup
16
+ @db = Mongoose::Database.new(:path => Dir.tmpdir)
17
+
18
+ @db.create_table(:plane) do |tbl|
19
+ tbl.add_indexed_column(:name, :string)
20
+ tbl.add_column(:country, :string)
21
+ tbl.add_indexed_column(:speed, :integer)
22
+ tbl.add_column(:range, :integer)
23
+ end
24
+
25
+ rec = Plane.new
26
+ rec.name = 'P-51'
27
+ rec.country = 'USA'
28
+ rec.speed = 402
29
+ rec.range = 1205
30
+ rec.save
31
+
32
+ rec = Plane.new
33
+ rec.name = 'Spitfire'
34
+ rec.country = 'Great Britain'
35
+ rec.speed = 333
36
+ rec.range = 454
37
+ rec.save
38
+
39
+ rec = Plane.new
40
+ rec.name = 'ME-109'
41
+ rec.country = 'Germany'
42
+ rec.speed = 351
43
+ rec.range = 501
44
+ rec.save
45
+
46
+ rec = Plane.new
47
+ rec.name = 'P-38'
48
+ rec.country = 'USA'
49
+ rec.speed = 401
50
+ rec.range = 999
51
+ rec.save
52
+ end
53
+
54
+ def teardown
55
+ @db.drop_table(:plane)
56
+ @db.close
57
+ end
58
+
59
+ def test_table_exists
60
+ assert(Object.const_defined?('Plane'))
61
+ assert(File.exists?(File.join(Dir.tmpdir, 'plane.mgh')))
62
+ assert(File.exists?(File.join(Dir.tmpdir, 'plane.mgt')))
63
+ assert_equal(5, Plane.columns.size)
64
+ end
65
+
66
+ def test_add_record
67
+ rec = Plane.find(1)
68
+
69
+ assert(rec.is_a?(Plane))
70
+ assert_equal(1, rec.id)
71
+ assert_equal('P-51', rec.name)
72
+ assert_equal('USA', rec.country)
73
+ assert_equal(402, rec.speed)
74
+ assert_equal(1205, rec.range)
75
+ end
76
+
77
+ def test_update_record
78
+ rec = Plane.find(1)
79
+ rec.speed = 405
80
+ rec.save
81
+
82
+ rec = Plane.find(1)
83
+
84
+ assert_equal(rec.speed, 405)
85
+ end
86
+
87
+ def test_find_001
88
+ rec = Plane.find(:first) { |plane| plane.name == 'P-51' }
89
+
90
+ assert(rec.is_a?(Plane))
91
+ assert_equal(rec.name, 'P-51')
92
+ end
93
+
94
+ def test_find_002
95
+ recs = Plane.find { |plane| plane.name == 'P-51' }
96
+
97
+ assert_equal(recs.size, 1)
98
+ assert(recs.first.is_a?(Plane))
99
+ assert_equal(recs.first.name, 'P-51')
100
+
101
+ recs = Plane.find
102
+
103
+ assert_equal(recs.size, 4)
104
+ assert(recs[1].is_a?(Plane))
105
+ assert_equal(recs[1].name, 'Spitfire')
106
+ end
107
+
108
+ def test_find_003
109
+ recs = Plane.find do |plane|
110
+ plane.any do
111
+ plane.country == 'USA'
112
+ plane.country == 'Great Britain'
113
+ end
114
+ end
115
+ assert_equal(recs.size, 3)
116
+
117
+ recs = Plane.find do |plane|
118
+ plane.any do
119
+ plane.country == 'USA'
120
+ plane.country == 'Great Britain'
121
+ end
122
+ plane.speed > 400
123
+ end
124
+ assert_equal(recs.size, 2)
125
+
126
+ recs = Plane.find do |plane|
127
+ plane.any do
128
+ plane.country == 'USA'
129
+ plane.country == 'Great Britain'
130
+ end
131
+ plane.speed > 400
132
+ plane.range > 1000
133
+ end
134
+ assert_equal(recs.size, 1)
135
+ assert_equal(recs.first.name, 'P-51')
136
+ end
137
+
138
+ def test_find_004
139
+ recs = Plane.find { |plane| plane.name.one_of('P-51', 'ME-109') }
140
+ assert_equal(recs.size, 2)
141
+
142
+ recs = Plane.find { |plane| plane.country.one_of('USA', 'Germany') }
143
+ assert_equal(recs.size, 3)
144
+
145
+ recs = Plane.find { |plane| plane.speed.between(351, 400) }
146
+ assert_equal(0, recs.size)
147
+
148
+ recs = Plane.find { |plane| plane.speed.between(351, 400, true) }
149
+ assert_equal(1, recs.size)
150
+ assert_equal('ME-109', recs.first.name)
151
+
152
+ recs = Plane.find { |plane| plane.range.between(450, 501) }
153
+ assert_equal(1, recs.size)
154
+
155
+ recs = Plane.find { |plane| plane.range.between(450, 501, false, true) }
156
+ assert_equal(2, recs.size)
157
+ end
158
+
159
+ def test_destroy
160
+ rec = Plane.find(:first) { |plane| plane.name == 'P-51' }
161
+ rec.destroy
162
+
163
+ recs = Plane.find
164
+
165
+ assert_equal(recs.size, 3)
166
+ end
167
+ end
@@ -0,0 +1,3 @@
1
+ require 'test/unit'
2
+ require 'tc_database'
3
+ require 'tc_table'
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.8.11
3
+ specification_version: 1
4
+ name: mongoose
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.1.0
7
+ date: 2006-07-19 00:00:00 -04:00
8
+ summary: Mongoose is a pure_ruby database management system.
9
+ require_paths:
10
+ - lib
11
+ email: jcribbs@netpromi.com
12
+ homepage: http://rubyforge.org/projects/mongoose/
13
+ rubyforge_project: mongoose
14
+ description: Mongoose is a module that allows you to create and databases. It has an ActiveRecord-like interface, uses Skiplists for its indexing, and uses Marshal to serializeits data.
15
+ autorequire: mongoose
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: false
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ authors:
29
+ - Jamey Cribbs
30
+ files:
31
+ - lib/mongoose.rb
32
+ - lib/mongoose
33
+ - lib/mongoose/database.rb
34
+ - lib/mongoose/linear_search.rb
35
+ - lib/mongoose/skiplist.rb
36
+ - lib/mongoose/table.rb
37
+ - lib/mongoose/util.rb
38
+ - lib/mongoose/column.rb
39
+ - README
40
+ - changes.txt
41
+ - test/tc_database.rb
42
+ - test/tc_table.rb
43
+ - test/ts_mongoose.rb
44
+ - example/relation_examples.rb
45
+ - example/simple_examples.rb
46
+ test_files:
47
+ - test/ts_mongoose.rb
48
+ rdoc_options: []
49
+
50
+ extra_rdoc_files: []
51
+
52
+ executables: []
53
+
54
+ extensions: []
55
+
56
+ requirements:
57
+ - none
58
+ dependencies: []
59
+