og 0.12.0 → 0.13.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (69) hide show
  1. data/CHANGELOG +27 -0
  2. data/INSTALL +56 -0
  3. data/{README.og → README} +3 -3
  4. data/Rakefile +7 -73
  5. data/benchmark/bench.rb +75 -0
  6. data/benchmark/sqlite-no-prepare.1.txt +13 -0
  7. data/benchmark/sqlite-no-prepare.2.txt +13 -0
  8. data/benchmark/sqlite-prepare.1.txt +13 -0
  9. data/benchmark/sqlite-prepare.2.txt +13 -0
  10. data/doc/AUTHORS +0 -9
  11. data/{RELEASES.og → doc/RELEASES} +15 -0
  12. data/doc/config.txt +35 -0
  13. data/doc/tutorial.txt +595 -0
  14. data/examples/{og/README → README} +1 -1
  15. data/examples/{og/mock_example.rb → mock_example.rb} +1 -1
  16. data/examples/{og/mysql_to_psql.rb → mysql_to_psql.rb} +1 -1
  17. data/examples/{og/run.rb → run.rb} +1 -1
  18. data/install.rb +8 -5
  19. data/lib/og.rb +13 -8
  20. data/lib/og/adapter.rb +1 -1
  21. data/lib/og/adapters/filesys.rb +1 -1
  22. data/lib/og/adapters/mysql.rb +4 -3
  23. data/lib/og/adapters/oracle.rb +1 -1
  24. data/lib/og/adapters/psql.rb +4 -3
  25. data/lib/og/adapters/sqlite.rb +1 -1
  26. data/lib/og/backend.rb +1 -1
  27. data/lib/og/connection.rb +1 -1
  28. data/lib/og/database.rb +1 -1
  29. data/lib/og/meta.rb +13 -2
  30. data/lib/og/observer.rb +1 -1
  31. data/lib/og/typemacros.rb +1 -1
  32. data/lib/og/validation.rb +81 -0
  33. data/test/og/tc_validation.rb +89 -0
  34. metadata +33 -65
  35. data/ChangeLog +0 -1549
  36. data/lib/glue.rb +0 -55
  37. data/lib/glue/array.rb +0 -61
  38. data/lib/glue/attribute.rb +0 -83
  39. data/lib/glue/cache.rb +0 -138
  40. data/lib/glue/flexob.rb +0 -12
  41. data/lib/glue/hash.rb +0 -122
  42. data/lib/glue/inflector.rb +0 -91
  43. data/lib/glue/logger.rb +0 -147
  44. data/lib/glue/misc.rb +0 -14
  45. data/lib/glue/mixins.rb +0 -36
  46. data/lib/glue/number.rb +0 -24
  47. data/lib/glue/object.rb +0 -32
  48. data/lib/glue/pool.rb +0 -60
  49. data/lib/glue/property.rb +0 -408
  50. data/lib/glue/string.rb +0 -162
  51. data/lib/glue/time.rb +0 -85
  52. data/lib/glue/validation.rb +0 -394
  53. data/vendor/extensions/_base.rb +0 -153
  54. data/vendor/extensions/_template.rb +0 -36
  55. data/vendor/extensions/all.rb +0 -21
  56. data/vendor/extensions/array.rb +0 -68
  57. data/vendor/extensions/binding.rb +0 -224
  58. data/vendor/extensions/class.rb +0 -50
  59. data/vendor/extensions/continuation.rb +0 -71
  60. data/vendor/extensions/enumerable.rb +0 -250
  61. data/vendor/extensions/hash.rb +0 -23
  62. data/vendor/extensions/io.rb +0 -58
  63. data/vendor/extensions/kernel.rb +0 -42
  64. data/vendor/extensions/module.rb +0 -114
  65. data/vendor/extensions/numeric.rb +0 -230
  66. data/vendor/extensions/object.rb +0 -164
  67. data/vendor/extensions/ostruct.rb +0 -41
  68. data/vendor/extensions/string.rb +0 -316
  69. data/vendor/extensions/symbol.rb +0 -28
@@ -1,4 +1,4 @@
1
- = Og Example
1
+ = Og Examples
2
2
 
3
3
  == run.rb
4
4
 
@@ -5,7 +5,7 @@
5
5
  #
6
6
  # * George Moschovitis <gm@navel.gr>
7
7
  # (c) 2004-2005 Navel, all rights reserved.
8
- # $Id: run.rb 185 2004-12-10 13:29:09Z gmosx $
8
+ # $Id: mock_example.rb 280 2005-03-10 11:39:23Z gmosx $
9
9
 
10
10
  require 'rubygems'
11
11
  require 'flexmock'
@@ -9,7 +9,7 @@
9
9
  #
10
10
  # * George Moschovitis <gm@navel.gr>
11
11
  # (c) 2004-2005 Navel, all rights reserved.
12
- # $Id$
12
+ # $Id: mysql_to_psql.rb 280 2005-03-10 11:39:23Z gmosx $
13
13
 
14
14
  raise 'WARNING, this example does not work yet, for the moment ' +
15
15
  'just have a look at the source code.'
@@ -2,7 +2,7 @@
2
2
  #
3
3
  # * George Moschovitis <gm@navel.gr>
4
4
  # (c) 2004 Navel, all rights reserved.
5
- # $Id: run.rb 263 2005-02-23 13:45:08Z gmosx $
5
+ # $Id: run.rb 280 2005-03-10 11:39:23Z gmosx $
6
6
 
7
7
  require 'og'
8
8
 
data/install.rb CHANGED
@@ -29,16 +29,19 @@ puts %{
29
29
 
30
30
 
31
31
  ---
32
- Congratulations, you have successfully installed Nitro + Og!
32
+ Congratulations, you have successfully installed Og!
33
33
  The libraries where installed in '#{dst_dir}'.
34
34
 
35
- To verify that everything works correctly, try to run the tiny
35
+ You probably need to install the Glue library
36
+ as well. (http://nitro.rubyforge.org)
37
+
38
+ To verify that everything works correctly, try to run an
36
39
  example by issuing:
37
40
 
38
- $ cd exampes/tiny
39
- $ ruby ctl
41
+ $ cd examples
42
+ $ ruby run.rb
40
43
 
41
44
  at the command line.
42
45
 
43
- Enjoy the magic of Nitro!
46
+ Enjoy the magic of Og!
44
47
  }
data/lib/og.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  # * George Moschovitis <gm@navel.gr>
2
2
  # (c) 2004-2005 Navel, all rights reserved.
3
- # $Id: og.rb 270 2005-03-07 17:52:16Z gmosx $
3
+ # $Id: og.rb 300 2005-03-16 13:23:10Z gmosx $
4
4
 
5
5
  require 'glue'
6
6
  require 'glue/logger'
@@ -21,7 +21,7 @@ require 'glue/validation'
21
21
  #
22
22
  # + Object-Relational mapping.
23
23
  # + Absolutely no configuration files.
24
- # + Multiple backends (PostgreSQL, MySQL, SQLite).
24
+ # + Multiple backends (PostgreSQL, MySQL, SQLite, Oracle).
25
25
  # + ActiveRecord-style meta language and db aware methods.
26
26
  # + Deserialize to Ruby Objects.
27
27
  # + Deserialize sql join queries to Ruby Objects (temporarily dissabled).
@@ -105,20 +105,24 @@ require 'glue/validation'
105
105
 
106
106
  module Og
107
107
 
108
- # The name.
109
-
110
- Name = 'ObjectGraph'
111
-
112
108
  # The version.
113
109
 
114
- Version = '0.12.0'
110
+ Version = '0.13.0'
115
111
 
116
112
  # Library path.
117
113
 
118
114
  LibPath = File.dirname(__FILE__)
119
115
 
116
+ # If true, check for implicti changes in the object
117
+ # graph. For example when you add an object to a parent
118
+ # the object might be removed from his previous parent.
119
+ # In this case Og emmits a warning.
120
+
121
+ mattr_accessor :check_implicit_graph_changes, false
122
+
120
123
  # If true, only allow reading from the database. Usefull
121
124
  # for maintainance.
125
+ # WARNING: not implemented yet.
122
126
 
123
127
  mattr_accessor :read_only_mode, false
124
128
 
@@ -154,7 +158,7 @@ module Og
154
158
  # for convienience.
155
159
 
156
160
  mattr_accessor :create_schema, true
157
-
161
+
158
162
  # The active database. Og allows you to access multiple
159
163
  # databases from a single application.
160
164
 
@@ -183,3 +187,4 @@ end
183
187
  # gmosx: leave this here.
184
188
 
185
189
  require 'og/database'
190
+ require 'og/validation'
@@ -1,6 +1,6 @@
1
1
  # * George Moschovitis <gm@navel.gr>
2
2
  # (c) 2004-2005 Navel, all rights reserved.
3
- # $Id: adapter.rb 270 2005-03-07 17:52:16Z gmosx $
3
+ # $Id: adapter.rb 281 2005-03-10 12:24:14Z gmosx $
4
4
 
5
5
  require 'yaml'
6
6
  require 'singleton'
@@ -1,6 +1,6 @@
1
1
  # * George Moschovitis <gm@navel.gr>
2
2
  # (c) 2004-2005 Navel, all rights reserved.
3
- # $Id: filesys.rb 264 2005-02-23 13:46:55Z gmosx $
3
+ # $Id: filesys.rb 281 2005-03-10 12:24:14Z gmosx $
4
4
 
5
5
  require 'fileutils'
6
6
 
@@ -1,6 +1,6 @@
1
1
  # * George Moschovitis <gm@navel.gr>
2
2
  # (c) 2004-2005 Navel, all rights reserved.
3
- # $Id: mysql.rb 270 2005-03-07 17:52:16Z gmosx $
3
+ # $Id: mysql.rb 281 2005-03-10 12:24:14Z gmosx $
4
4
 
5
5
  require 'mysql'
6
6
 
@@ -75,12 +75,13 @@ class MysqlAdapter < Adapter
75
75
  end
76
76
 
77
77
  def create_db(database, user = nil, password = nil)
78
- `mysqladmin -f --user=#{user} --password=#{password} create #{database}`
78
+ # gmosx: system is used to avoid shell expansion.
79
+ system 'mysqladmin', '-f', "--user=#{user}", "--password=#{password}", 'create', database
79
80
  super
80
81
  end
81
82
 
82
83
  def drop_db(database, user = nil, password = nil)
83
- `mysqladmin -f --user=#{user} --password=#{password} drop #{database}`
84
+ system 'mysqladmin', '-f', "--user=#{user}", "--password=#{password}", 'drop', database
84
85
  super
85
86
  end
86
87
 
@@ -1,7 +1,7 @@
1
1
  # * Matt Bowen <matt.bowen@farweststeel.com>
2
2
  # * George Moschovitis <gm@navel.gr>
3
3
  # (c) 2004-2005 Navel, all rights reserved.
4
- # $Id: oracle.rb 270 2005-03-07 17:52:16Z gmosx $
4
+ # $Id: oracle.rb 281 2005-03-10 12:24:14Z gmosx $
5
5
 
6
6
  require 'oracle'
7
7
 
@@ -1,6 +1,6 @@
1
1
  # * George Moschovitis <gm@navel.gr>
2
2
  # (c) 2004-2005 Navel, all rights reserved.
3
- # $Id: psql.rb 270 2005-03-07 17:52:16Z gmosx $
3
+ # $Id: psql.rb 281 2005-03-10 12:24:14Z gmosx $
4
4
 
5
5
  require 'postgres'
6
6
 
@@ -50,12 +50,13 @@ class PsqlAdapter < Adapter
50
50
  end
51
51
 
52
52
  def create_db(database, user = nil, password = nil)
53
- `createdb #{database} -U #{user}`
53
+ # gmosx: system is used to avoid shell expansion.
54
+ system 'createdb', database, '-U', user
54
55
  super
55
56
  end
56
57
 
57
58
  def drop_db(database, user = nil, password = nil)
58
- `dropdb #{database} -U #{user}`
59
+ system 'dropdb', database, '-U', user
59
60
  super
60
61
  end
61
62
 
@@ -1,6 +1,6 @@
1
1
  # * George Moschovitis <gm@navel.gr>
2
2
  # (c) 2004-2005 Navel, all rights reserved.
3
- # $Id: sqlite.rb 270 2005-03-07 17:52:16Z gmosx $
3
+ # $Id: sqlite.rb 281 2005-03-10 12:24:14Z gmosx $
4
4
 
5
5
  require 'sqlite3'
6
6
  require 'fileutils'
@@ -1,6 +1,6 @@
1
1
  # * George Moschovitis <gm@navel.gr>
2
2
  # (c) 2004-2005 Navel, all rights reserved.
3
- # $Id: backend.rb 263 2005-02-23 13:45:08Z gmosx $
3
+ # $Id: backend.rb 281 2005-03-10 12:24:14Z gmosx $
4
4
 
5
5
  require 'yaml'
6
6
 
@@ -1,6 +1,6 @@
1
1
  # * George Moschovitis <gm@navel.gr>
2
2
  # (c) 2004-2005 Navel, all rights reserved.
3
- # $Id: connection.rb 270 2005-03-07 17:52:16Z gmosx $
3
+ # $Id: connection.rb 281 2005-03-10 12:24:14Z gmosx $
4
4
 
5
5
  module Og;
6
6
 
@@ -1,6 +1,6 @@
1
1
  # * George Moschovitis <gm@navel.gr>
2
2
  # (c) 2004-2005 Navel, all rights reserved.
3
- # $Id: database.rb 270 2005-03-07 17:52:16Z gmosx $
3
+ # $Id: database.rb 281 2005-03-10 12:24:14Z gmosx $
4
4
 
5
5
  require 'glue/logger'
6
6
  require 'glue/attribute'
@@ -1,6 +1,6 @@
1
1
  # * George Moschovitis <gm@navel.gr>
2
2
  # (c) 2004-2005 Navel, all rights reserved.
3
- # $Id: meta.rb 270 2005-03-07 17:52:16Z gmosx $
3
+ # $Id: meta.rb 281 2005-03-10 12:24:14Z gmosx $
4
4
  #--
5
5
  # TODO:
6
6
  # - precreate the meta sql statements as much as possible to
@@ -183,7 +183,7 @@ module MetaLanguage
183
183
  meta :descendants, klass, linkback
184
184
  meta :props_and_relations, HasMany.new(name, klass, :linkback => linkback)
185
185
 
186
- module_eval %{
186
+ code = %{
187
187
  def #{name}(extrasql = nil)
188
188
  Og.db.select("SELECT * FROM #{Og::Adapter.table(klass)} WHERE #{linkback}=\#\@oid \#\{extrasql\}", #{klass})
189
189
  end
@@ -194,6 +194,15 @@ module MetaLanguage
194
194
 
195
195
  def add_#{name_s}(obj = nil)
196
196
  yield(obj = #{klass}.new) unless obj
197
+ }
198
+ if Og.check_implicit_graph_changes
199
+ code << %{
200
+ if obj.#{linkback}
201
+ Logger.warn "Implicit graph change for object #{obj.inspect}"
202
+ end
203
+ }
204
+ end
205
+ code << %{
197
206
  obj.#{linkback} = @oid
198
207
  obj.save!
199
208
  end
@@ -202,6 +211,8 @@ module MetaLanguage
202
211
  Og.db.exec("DELETE FROM #{Og::Adapter.table(klass)} WHERE #{linkback}=\#\@oid \#\{extrasql\}")
203
212
  end
204
213
  }
214
+
215
+ module_eval(code)
205
216
  end
206
217
 
207
218
  # Implements a 'many_to_many' relation.
@@ -1,6 +1,6 @@
1
1
  # * George Moschovitis <gm@navel.gr>
2
2
  # (c) 2004-2005 Navel, all rights reserved.
3
- # $Id: observer.rb 271 2005-03-07 17:56:45Z gmosx $
3
+ # $Id: observer.rb 281 2005-03-10 12:24:14Z gmosx $
4
4
 
5
5
  module Og
6
6
 
@@ -1,6 +1,6 @@
1
1
  # * George Moschovitis <gm@navel.gr>
2
2
  # (c) 2004-2005 Navel, all rights reserved.
3
- # $Id: typemacros.rb 267 2005-02-28 14:52:41Z gmosx $
3
+ # $Id: typemacros.rb 281 2005-03-10 12:24:14Z gmosx $
4
4
 
5
5
  module Og
6
6
 
@@ -0,0 +1,81 @@
1
+ # * George Moschovitis <gm@navel.gr>
2
+ # (c) 2004-2005 Navel, all rights reserved.
3
+ # $Id: validation.rb 301 2005-03-16 13:24:25Z gmosx $
4
+
5
+ require 'glue/validation'
6
+
7
+ module N
8
+
9
+ # Extend the Validation methods defined in glue/validation.rb with
10
+ # extra db related options.
11
+
12
+ module Validation
13
+
14
+ # Encapsulates a list of validation errors.
15
+
16
+ class Errors
17
+ cattr_accessor :not_unique, 'The value is already used'
18
+ cattr_accessor :invalid_relation, 'Invalid relations'
19
+ end
20
+
21
+ module MetaLanguage
22
+
23
+ # Validate the value of this attribute is unique
24
+ # for this class.
25
+ #
26
+ # The Og libraries are required for this methdod to
27
+ # work. You can override this method if you want to
28
+ # use another OR mapping library.
29
+ #
30
+ #--
31
+ # TODO: :unique should implicitly generate
32
+ # validate_unique.
33
+ #++
34
+
35
+ def validate_unique(*params)
36
+ c = {
37
+ :msg => N::Validation::Errors.not_unique,
38
+ :on => :save
39
+ }
40
+ c.update(params.pop) if params.last.is_a?(Hash)
41
+
42
+ for name in params
43
+ # FIXME: improve the generated code.
44
+ code = %{
45
+ others = obj.class.find_by_#{name}(obj.#{name})
46
+ unless (others.is_a?(Array) and others.empty?)
47
+ errors.add(:#{name}, '#{c[:msg]}')
48
+ end;
49
+ }
50
+
51
+ __meta[:validations] << [code, c[:on]]
52
+ end
53
+ end
54
+
55
+ # Validate the related object or objects. Works
56
+ # with all relations.
57
+
58
+ def validate_related(*params)
59
+ c = {
60
+ :msg => N::Validation::Errors.invalid_relation,
61
+ :on => :save
62
+ }
63
+ c.update(params.pop) if params.last.is_a?(Hash)
64
+
65
+ for name in params
66
+ code = %{
67
+ unless (obj.#{name}.is_a?(Array) ? obj.#{name} : [obj.#{name}]).inject(true) { |memo, obj| (obj.nil? or obj.valid?) and memo }
68
+ errors.add(:#{name}, '#{c[:msg]}')
69
+ end;
70
+ }
71
+
72
+ __meta[:validations] << [code, c[:on]]
73
+ end
74
+ end
75
+ alias_method :validate_associated, :validate_related
76
+
77
+ end
78
+
79
+ end
80
+
81
+ end
@@ -0,0 +1,89 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), '..', '..', 'lib')
2
+
3
+ require 'test/unit'
4
+
5
+ require 'og'
6
+ require 'og/validation'
7
+
8
+ N::Property.type_checking = false
9
+
10
+ class TC_OgValidation < Test::Unit::TestCase # :nodoc: all
11
+
12
+ class Group; end
13
+
14
+ class User
15
+ property :name, String
16
+ validate_unique :name
17
+
18
+ def initialize(name = nil)
19
+ @name = name
20
+ end
21
+ end
22
+
23
+ class Filler
24
+ property :name, String
25
+ validate_length :name, :min => 3
26
+
27
+ belongs_to :group, Group
28
+
29
+ def initialize(name = nil)
30
+ @name = name
31
+ end
32
+ end
33
+
34
+ class Group
35
+ property :name, String
36
+
37
+ has_many :fillers, Filler
38
+ validate_related :fillers
39
+ end
40
+
41
+ def test_all
42
+ config = {
43
+ :adapter => 'psql',
44
+ :database => 'test',
45
+ :user => 'postgres',
46
+ :password => 'navelrulez',
47
+ :connection_count => 2
48
+ }
49
+
50
+ Og::Database.drop_db!(config)
51
+ og = Og::Database.new(config)
52
+
53
+ og.get_connection
54
+
55
+ # validate_unique
56
+
57
+ u1 = User.create('gmosx')
58
+
59
+ u2 = User.new('gmosx')
60
+ assert !u2.valid?
61
+ assert_equal 1, u2.errors.count
62
+
63
+ u2.name = 'stella'
64
+ u2.valid?
65
+ assert u2.valid?
66
+
67
+ # validate_related
68
+
69
+ f1 = Filler.new('g')
70
+ f2 = Filler.new('ok')
71
+
72
+ g = Group.create
73
+ g.add_filler(f1)
74
+ g.add_filler(f2)
75
+
76
+ assert !g.valid?
77
+
78
+ f1.name = 'george'
79
+ f1.save
80
+ f2.name = 'stella'
81
+ f2.save
82
+
83
+ assert g.valid?
84
+
85
+ og.put_connection
86
+ og.shutdown
87
+ end
88
+
89
+ end