og 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,139 @@
1
+ # code:
2
+ # * George Moschovitis <gm@navel.gr>
3
+ #
4
+ # (c) 2004 Navel, all rights reserved.
5
+ # $Id: meta.rb 159 2004-11-18 10:18:30Z gmosx $
6
+
7
+ require 'og/backend'
8
+
9
+ module Og
10
+
11
+ # = MetaUtils
12
+ #
13
+ # Some useful meta-language utilities.
14
+ #
15
+ module MetaUtils # :nodoc: all
16
+
17
+ # Conver the klass to a string representation
18
+ # The leading module if available is removed.
19
+ #
20
+ def self.expand(klass)
21
+ return klass.name.gsub(/^.*::/, '').gsub(/::/, '_').downcase
22
+ end
23
+
24
+ end
25
+
26
+ # = MetaLanguage
27
+ #
28
+ # Implements a meta-language for manipulating og-managed objects
29
+ # and defining their relationships. The original idea comes
30
+ # from the excellent ActiveRecord library.
31
+ #
32
+ # Many more useful relations will be available soon.
33
+ #
34
+ module MetaLanguage
35
+
36
+ # Defines an SQL index.
37
+ #
38
+ def sql_index(index, options = {})
39
+ meta :sql_index, [index, options]
40
+ end
41
+
42
+ # Implements a 'belongs_to' relation.
43
+ # Automatically enchants the calling class with helper methods.
44
+ #
45
+ # Example:
46
+ #
47
+ # class MyObject
48
+ # belongs_to AnotherObject, :parent
49
+ # end
50
+ #
51
+ # creates the code:
52
+ #
53
+ # prop_accessor Fixnum, :parent_oid
54
+ # def parent; ... end
55
+ # def parent=(obj_or_oid); ... end
56
+ #
57
+ def belongs_to(name, klass, options = {})
58
+ module_eval %{
59
+ prop_accessor Fixnum, :#{name}_oid
60
+
61
+ def #{name}
62
+ $og.load_by_oid(@#{name}_oid, #{klass})
63
+ end
64
+
65
+ def #{name}=(obj_or_oid)
66
+ @#{name}_oid = obj_or_oid.to_i
67
+ end
68
+ }
69
+ end
70
+
71
+ # Implements a 'has_many' relation.
72
+ # Automatically enchants the calling class with helper methods.
73
+ #
74
+ # NOT IMPLEMENTED.
75
+ #
76
+ # Example:
77
+ #
78
+ # class MyObject
79
+ # has_many AnotherObject, :children
80
+ # end
81
+ #
82
+ # creates the code:
83
+ #
84
+ # def children; ... end
85
+ #
86
+ def has_one(klass, name, options = {})
87
+ module_eval %{
88
+ def #{name}(extrasql = nil)
89
+ $og.select_one("article_oid=\#\@oid \#\{extrasql\}", #{klass})
90
+ end
91
+ }
92
+ end
93
+
94
+ # Implements a 'has_many' relation.
95
+ # Automatically enchants the calling class with helper methods.
96
+ #
97
+ # Example:
98
+ #
99
+ # class MyObject
100
+ # has_many AnotherObject, :children
101
+ # end
102
+ #
103
+ # creates the code:
104
+ #
105
+ # def children; ... end
106
+ #
107
+ def has_many(name, klass, options = {})
108
+ # linkback is the property of the child object that 'links back'
109
+ # to this object.
110
+ linkback = options[:linkback] || "#{MetaUtils.expand(self)}_oid"
111
+
112
+ module_eval %{
113
+ @@og_descendants ||= {}
114
+ @@og_descendants[#{klass}] = :#{linkback}
115
+
116
+ unless defined?(og_descendants)
117
+ def self.og_descendants
118
+ @@og_descendants
119
+ end
120
+ end
121
+
122
+ def #{name}(extrasql = nil)
123
+ $og.select("SELECT * FROM #{Utils.table(klass)} WHERE #{linkback}=\#\@oid \#\{extrasql\}", #{klass})
124
+ end
125
+
126
+ def #{name}_count(extrasql = nil)
127
+ $og.count("SELECT COUNT(*) FROM #{Utils.table(klass)} WHERE #{linkback}=\#\@oid \#\{extrasql\}")
128
+ end
129
+ }
130
+ end
131
+
132
+ end
133
+
134
+ end # module
135
+
136
+ class Module # :nodoc: all
137
+ include Og::MetaLanguage
138
+ end
139
+
@@ -0,0 +1,8 @@
1
+ # code:
2
+ # * George Moschovitis <gm@navel.gr>
3
+ #
4
+ # (c) 2004 Navel, all rights reserved.
5
+ # $Id$
6
+
7
+ # The version of Og.
8
+ $og_version = '0.5.0'
@@ -0,0 +1,179 @@
1
+ require "test/unit"
2
+
3
+ require "glue/logger"; $log = Logger.new(STDERR) unless $log
4
+ require "og"
5
+
6
+ module Test # :nodoc: all
7
+
8
+ # bug: creates a table that fucks-up postgres if not
9
+ # correctly escaped.
10
+ class User
11
+ prop_accessor :name, String
12
+
13
+ def initialize(name = nil)
14
+ @name = name
15
+ end
16
+ end
17
+
18
+ class Comment; end
19
+
20
+ class Article
21
+ prop_accessor :title, String
22
+ prop_accessor :body, String
23
+ prop_accessor :owner_oid, Fixnum, :sql_index => true
24
+ prop_accessor :another_oid, Fixnum
25
+ sql_index 'owner_oid, another_oid'
26
+ prop_accessor :options, Hash
27
+ has_many :comments, Test::Comment
28
+
29
+ def initialize(title = nil, body = nil)
30
+ @title = title
31
+ @body = body
32
+ @options = {"hello" => "world"}
33
+ end
34
+
35
+ def og_pre_insert(conn)
36
+ puts "-- PRE INSERT CALLED FOR ARTICLE"
37
+ end
38
+
39
+ def og_post_insert_update(conn)
40
+ puts "-- POST INSERT UPDATE CALLED FOR ARTICLE"
41
+ end
42
+
43
+ def og_post_update(conn)
44
+ puts "-- POST UPDATE CALLED FOR ARTICLE"
45
+ end
46
+ end
47
+
48
+ class Comment
49
+ belongs_to :article, Test::Article
50
+ belongs_to :author, Test::User
51
+ prop_accessor :body, String
52
+ prop_accessor :create_time, Time
53
+
54
+ def initialize(body = nil)
55
+ @create_time = Time.now
56
+ @body = body
57
+ end
58
+
59
+ def og_pre_insert(conn)
60
+ puts "-- PRE INSERT CALLED FOR COMMENT"
61
+ end
62
+
63
+ def og_post_insert(conn)
64
+ puts "-- POST INSERT CALLED FOR COMMENT"
65
+ end
66
+ end
67
+
68
+ class TC_N_OG < Test::Unit::TestCase
69
+
70
+ def setup
71
+ psql = true
72
+
73
+ if psql
74
+ config = {
75
+ :backend => "psql",
76
+ :address => "localhost",
77
+ :database => "test",
78
+ :user => "postgres",
79
+ :password => "navelrulez",
80
+ :connection_count => 1
81
+ }
82
+ else
83
+ config = {
84
+ :backend => "mysql",
85
+ :address => "localhost",
86
+ :database => "test",
87
+ :user => "root",
88
+ :password => "navelrulez",
89
+ :connection_count => 1
90
+ }
91
+ end
92
+
93
+ Og::Database.drop_db!(config)
94
+ $og = Og::Database.new(config)
95
+ end
96
+
97
+ def teardown
98
+ $og.shutdown()
99
+ end
100
+
101
+ # gmosx: hmm, implemented in one method to enforce order.
102
+ #
103
+ def test_all
104
+ $DBG = true
105
+
106
+ $og.get_connection()
107
+
108
+ article = Article.new("Title", "Here comes the body")
109
+ $og << article
110
+
111
+ article.title = "Changed"
112
+ article.save!
113
+
114
+ $og.pupdate("body='Hello'", article)
115
+
116
+ article.update_properties "body='Hello'"
117
+
118
+ another = Article[1]
119
+ assert_equal(1, another.oid)
120
+ # bug: yaml load problem.
121
+ assert_equal("world", another.options["hello"])
122
+
123
+ # bug: YAMLing nil property
124
+ another.options = nil
125
+ another.save!
126
+
127
+ assert_equal(nil, $og.load(30000, Article))
128
+
129
+ articles = $og.load_all(Article)
130
+
131
+ # p articles
132
+ # p Article[23]
133
+
134
+ user = User.new("gmosx")
135
+ user.save!
136
+
137
+ user = User["gmosx"]
138
+
139
+ assert_equal("gmosx", user.name)
140
+
141
+ users1 = $og.select("name='gmosx' ORDER BY oid", User)
142
+
143
+ users = $og.select("SELECT * FROM #{User::DBTABLE} WHERE name='gmosx' ORDER BY oid", User)
144
+
145
+ users2 = User.select "name='gmosx' ORDER BY oid"
146
+
147
+ assert_equal(users1.size, users2.size)
148
+
149
+ article = Article.new("Title", "Body")
150
+ article.save!
151
+
152
+ comment = Comment.new("This is a comment")
153
+ comment.article = article
154
+ comment.author = User["gmosx"]
155
+ comment.save!
156
+
157
+ comment = Comment.new("This is another comment")
158
+ comment.article = article.oid
159
+ comment.author = User["gmosx"]
160
+ comment.save!
161
+
162
+ assert_equal(article.oid, comment.article_oid)
163
+
164
+ comments = article.comments
165
+
166
+ assert_equal(2, comments.size)
167
+
168
+ assert_equal("gmosx", comments[0].author.name)
169
+
170
+ Article.delete(article)
171
+
172
+ assert_equal(nil, Comment.all)
173
+
174
+ comment.delete!
175
+ end
176
+
177
+ end
178
+
179
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.8.1
3
+ specification_version: 1
4
+ name: og
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.5.0
7
+ date: 2004-11-23
8
+ summary: Og (ObjectGraph)
9
+ require_paths:
10
+ - lib
11
+ author: George Moschovitis
12
+ email: gm@navel.gr
13
+ homepage: http://www.navel.gr/og
14
+ rubyforge_project: og-rml
15
+ description: An efficient and transparent Object-Relational mapping library
16
+ autorequire: og
17
+ default_executable:
18
+ bindir: bin
19
+ has_rdoc: true
20
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
21
+ requirements:
22
+ -
23
+ - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: 1.8.1
26
+ version:
27
+ platform: ruby
28
+ files:
29
+ - README.og
30
+ - RELEASES.og
31
+ - LICENSE
32
+ - AUTHORS
33
+ - examples/og/run.rb
34
+ - examples/og/README
35
+ - lib/glue.rb
36
+ - lib/glue/pool.rb
37
+ - lib/glue/time.rb
38
+ - lib/glue/macro.rb
39
+ - lib/glue/inflector.rb
40
+ - lib/glue/array.rb
41
+ - lib/glue/number.rb
42
+ - lib/glue/cache.rb
43
+ - lib/glue/mixins.rb
44
+ - lib/glue/property.rb
45
+ - lib/glue/string.rb
46
+ - lib/glue/hash.rb
47
+ - lib/glue/logger.rb
48
+ - lib/og/backends
49
+ - lib/og/connection.rb
50
+ - lib/og/backend.rb
51
+ - lib/og/meta.rb
52
+ - lib/og/version.rb
53
+ - lib/og/backends/psql.rb
54
+ - lib/og/backends/mysql.rb
55
+ - lib/og.rb
56
+ - test/tc_og.rb
57
+ test_files: []
58
+ rdoc_options:
59
+ - "--main"
60
+ - README.og
61
+ - "--title"
62
+ - Og Documentation
63
+ extra_rdoc_files:
64
+ - README.og
65
+ - RELEASES.og
66
+ - LICENSE
67
+ - AUTHORS
68
+ executables: []
69
+ extensions: []
70
+ requirements: []
71
+ dependencies: []