og 0.11.0 → 0.12.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,53 @@
1
+ # * George Moschovitis <gm@navel.gr>
2
+ # (c) 2004-2005 Navel, all rights reserved.
3
+ # $Id: observer.rb 271 2005-03-07 17:56:45Z gmosx $
4
+
5
+ module Og
6
+
7
+ # Classes that include this module can be tracked by
8
+ # Observers. The observer mechanism utilizes duck typing
9
+ # so you can attach any class that responds to the
10
+ # Og lifycycle callback methods. However, classes extended
11
+ # from the Observer base class are typically used.
12
+
13
+ module Observable
14
+ def add_observer(*observer)
15
+ for o in observer.flatten
16
+ meta :og_observers, o
17
+ self.og_db.adapter.eval_lifecycle_methods(self, self.og_db)
18
+ end
19
+ end
20
+ end
21
+
22
+ # Observers are attached to managed classes to track their
23
+ # Lifecycle. This way, the 'polution' of the original class
24
+ # with excess responsibility is avoided.
25
+ #
26
+ # An observer can implement the standard Og lifecycle
27
+ # callbacks:
28
+ #
29
+ # * og_pre_read
30
+ # * og_post_read
31
+ # * og_pre_insert
32
+ # * og_post_insert
33
+ # * og_pre_update
34
+ # * og_post_update
35
+ # * og_pre_insert_update
36
+ # * og_post_insert_update
37
+ # * self.og_pre_delete
38
+ #
39
+ # Based on code from ActiveRecord (http://www.rubyonrails.com)
40
+
41
+ class Observer
42
+ include Singleton
43
+
44
+ # Attaches the observer to the supplied classes.
45
+
46
+ def self.observe(*classes)
47
+ for c in classes.flatten
48
+ c.meta :og_observers, self
49
+ end
50
+ end
51
+ end
52
+
53
+ end
@@ -0,0 +1,62 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), '..', '..', 'lib')
2
+
3
+ require 'test/unit'
4
+ require 'ostruct'
5
+
6
+ require 'og'
7
+
8
+ class TC_OgManyToMany < Test::Unit::TestCase # :nodoc: all
9
+ include N
10
+
11
+ class Attribute
12
+ property :name, String
13
+
14
+ def initialize(name = nil)
15
+ @name = name
16
+ end
17
+ end
18
+
19
+ class Klass
20
+ property :name, String
21
+ many_to_many :observed_attributes, Attribute, :linkback => :klass_observers
22
+ many_to_many :controlled_attributes, Attribute, :linkback => :klass_controllers
23
+
24
+ def initialize(name = nil)
25
+ @name = name
26
+ end
27
+ end
28
+
29
+ def test_all
30
+ config = {
31
+ :adapter => 'psql',
32
+ :database => 'test',
33
+ :user => 'postgres',
34
+ :password => 'navelrulez',
35
+ :connection_count => 2
36
+ }
37
+
38
+ Og::Database.drop_db!(config)
39
+ og = Og::Database.new(config)
40
+
41
+ og.get_connection
42
+
43
+ k = Klass.create('klass1')
44
+ a1 = Attribute.create('attr1')
45
+ a2 = Attribute.create('attr2')
46
+
47
+ k.add_observed_attribute(a1)
48
+ k.add_observed_attribute(a2)
49
+
50
+ assert_equal 2, k.observed_attributes.size
51
+ assert_equal 1, a1.klass_observers.size
52
+
53
+ k.add_controlled_attribute(a1)
54
+
55
+ assert_equal 1, k.controlled_attributes.size
56
+ assert_equal 1, a1.klass_controllers.size
57
+
58
+ og.put_connection
59
+ og.shutdown
60
+ end
61
+
62
+ end
@@ -0,0 +1,85 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), '..', '..', 'lib')
2
+
3
+ require 'test/unit'
4
+ require 'ostruct'
5
+
6
+ require 'og'
7
+
8
+ class TC_OgObserver < Test::Unit::TestCase # :nodoc: all
9
+ include N
10
+
11
+ class User
12
+ property :name
13
+ end
14
+
15
+ class Article
16
+ property :name, String
17
+ property :age, Fixnum
18
+
19
+ def initialize (name = nil, age = nil)
20
+ @name, @age = name, age
21
+ end
22
+ end
23
+
24
+ # Example of a simple class that acts as an observer.
25
+
26
+ class ArticleObserver
27
+ attr :count
28
+
29
+ def initialize
30
+ @count = 0
31
+ end
32
+
33
+ def og_pre_insert(conn, obj)
34
+ @count += 1
35
+ end
36
+ end
37
+
38
+ # Rails style observer, baaah...
39
+
40
+ class MultiObserver < Og::Observer
41
+ observe Article, User
42
+
43
+ attr :count
44
+
45
+ def initialize
46
+ @count = 0
47
+ end
48
+
49
+ def og_pre_insert(conn, obj)
50
+ @count += 1
51
+ end
52
+ end
53
+
54
+ def setup
55
+ end
56
+
57
+ def teardown
58
+ end
59
+
60
+ def test_all
61
+ config = {
62
+ :adapter => 'psql',
63
+ :database => 'test',
64
+ :user => 'postgres',
65
+ :password => 'navelrulez',
66
+ :connection_count => 2
67
+ }
68
+
69
+ Og::Database.drop_db!(config)
70
+ og = Og::Database.new(config)
71
+
72
+ og.get_connection
73
+
74
+ ao = ArticleObserver.new
75
+ Article.add_observer(ao)
76
+ Article.create('Hello', 5)
77
+
78
+ assert_equal 1, ao.count
79
+ assert_equal 1, MultiObserver.instance.count
80
+
81
+ og.put_connection
82
+ og.shutdown
83
+ end
84
+
85
+ end
@@ -11,7 +11,7 @@ module Test # :nodoc: all
11
11
  # bug: creates a table that fucks-up postgres if not
12
12
  # correctly escaped.
13
13
  class User
14
- prop_accessor :name, String
14
+ prop_accessor :name, String, :unique => true
15
15
  prop_accessor :banned, TrueClass
16
16
 
17
17
  def initialize(name = nil)
@@ -212,6 +212,20 @@ class TC_N_OG < Test::Unit::TestCase
212
212
  article = Article.new("Title", "Body")
213
213
  article.save!
214
214
 
215
+ # advanced finder
216
+
217
+ users3 = User.find_by_banned(true)
218
+ assert users3.empty?
219
+
220
+ user3 = User.find_by_name('gmosx')
221
+ assert_equal 'gmosx', user3.name
222
+
223
+ user3.banned = true
224
+ user3.save
225
+
226
+ users3 = User.find_by_banned(true)
227
+ assert !users3.empty?
228
+
215
229
  comment = Comment.new("This is a comment")
216
230
  comment.article = article
217
231
  comment.author = User["gmosx"]
@@ -330,7 +344,7 @@ class TC_N_OG < Test::Unit::TestCase
330
344
 
331
345
  og = Og::Database.new(config)
332
346
 
333
- user = User.new("gmosx")
347
+ user = User.new("kokoriko")
334
348
  user.save!
335
349
 
336
350
  og.shutdown
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.6
3
3
  specification_version: 1
4
4
  name: og
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.11.0
7
- date: 2005-02-28
6
+ version: 0.12.0
7
+ date: 2005-03-07
8
8
  summary: Og (ObjectGraph)
9
9
  require_paths:
10
10
  - lib
@@ -29,8 +29,8 @@ authors:
29
29
  files:
30
30
  - README.og
31
31
  - RELEASES.og
32
- - LICENSE
33
- - AUTHORS
32
+ - doc/LICENSE
33
+ - doc/AUTHORS
34
34
  - Rakefile
35
35
  - ChangeLog
36
36
  - install.rb
@@ -59,6 +59,7 @@ files:
59
59
  - lib/og/adapters
60
60
  - lib/og/enchant.rb
61
61
  - lib/og/connection.rb
62
+ - lib/og/observer.rb
62
63
  - lib/og/database.rb
63
64
  - lib/og/mock.rb
64
65
  - lib/og/meta.rb
@@ -72,8 +73,10 @@ files:
72
73
  - lib/og.rb
73
74
  - test/tc_og.rb
74
75
  - test/og/tc_sqlite.rb
76
+ - test/og/tc_observer.rb
75
77
  - test/og/tc_filesys.rb
76
78
  - test/og/tc_meta.rb
79
+ - test/og/tc_many_to_many.rb
77
80
  - test/og/tc_lifecycle.rb
78
81
  - vendor/extensions/enumerable.rb
79
82
  - vendor/extensions/all.rb
@@ -103,8 +106,8 @@ rdoc_options:
103
106
  extra_rdoc_files:
104
107
  - README.og
105
108
  - RELEASES.og
106
- - LICENSE
107
- - AUTHORS
109
+ - doc/LICENSE
110
+ - doc/AUTHORS
108
111
  executables: []
109
112
  extensions: []
110
113
  requirements: []