og 0.31.0 → 0.40.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/doc/{AUTHORS → CONTRIBUTORS} +26 -10
- data/doc/LICENSE +2 -3
- data/doc/RELEASES +56 -7
- data/doc/tutorial.txt +15 -15
- data/lib/glue/cacheable.rb +2 -5
- data/lib/glue/hierarchical.rb +1 -4
- data/lib/glue/optimistic_locking.rb +0 -2
- data/lib/glue/orderable.rb +79 -75
- data/lib/glue/revisable.rb +19 -24
- data/lib/glue/searchable.rb +0 -2
- data/lib/glue/taggable.rb +31 -29
- data/lib/glue/timestamped.rb +4 -2
- data/lib/og.rb +50 -29
- data/lib/og/adapter.rb +19 -0
- data/lib/og/adapter/mysql.rb +212 -0
- data/lib/og/adapter/mysql/override.rb +34 -0
- data/lib/og/adapter/mysql/script.rb +15 -0
- data/lib/og/adapter/mysql/utils.rb +40 -0
- data/lib/og/adapter/postgresql.rb +231 -0
- data/lib/og/adapter/postgresql/override.rb +117 -0
- data/lib/og/adapter/postgresql/script.rb +15 -0
- data/lib/og/adapter/postgresql/utils.rb +35 -0
- data/lib/og/adapter/sqlite.rb +132 -0
- data/lib/og/adapter/sqlite/override.rb +33 -0
- data/lib/og/adapter/sqlite/script.rb +15 -0
- data/lib/og/collection.rb +35 -7
- data/lib/og/{evolution.rb → dump.rb} +4 -5
- data/lib/og/entity.rb +102 -173
- data/lib/og/entity/clone.rb +119 -0
- data/lib/og/errors.rb +0 -2
- data/lib/og/manager.rb +85 -37
- data/lib/og/relation.rb +52 -34
- data/lib/og/relation/belongs_to.rb +0 -2
- data/lib/og/relation/has_many.rb +27 -4
- data/lib/og/relation/joins_many.rb +41 -14
- data/lib/og/relation/many_to_many.rb +10 -0
- data/lib/og/relation/refers_to.rb +22 -5
- data/lib/og/store.rb +80 -86
- data/lib/og/store/sql.rb +710 -713
- data/lib/og/store/sql/evolution.rb +119 -0
- data/lib/og/store/sql/join.rb +155 -0
- data/lib/og/store/sql/utils.rb +149 -0
- data/lib/og/test/assertions.rb +1 -3
- data/lib/og/test/testcase.rb +0 -2
- data/lib/og/types.rb +2 -5
- data/lib/og/validation.rb +6 -9
- data/test/{og/mixin → glue}/tc_hierarchical.rb +3 -13
- data/test/glue/tc_og_paginate.rb +47 -0
- data/test/{og/mixin → glue}/tc_optimistic_locking.rb +2 -12
- data/test/{og/mixin → glue}/tc_orderable.rb +15 -23
- data/test/glue/tc_orderable2.rb +47 -0
- data/test/glue/tc_revisable.rb +3 -3
- data/test/{og/mixin → glue}/tc_taggable.rb +20 -10
- data/test/{og/mixin → glue}/tc_timestamped.rb +2 -12
- data/test/glue/tc_webfile.rb +36 -0
- data/test/og/CONFIG.rb +8 -11
- data/test/og/multi_validations_model.rb +14 -0
- data/test/og/store/tc_filesys.rb +3 -1
- data/test/og/store/tc_kirby.rb +16 -13
- data/test/og/store/tc_sti.rb +11 -11
- data/test/og/store/tc_sti2.rb +79 -0
- data/test/og/tc_build.rb +41 -0
- data/test/og/tc_cacheable.rb +3 -2
- data/test/og/tc_has_many.rb +96 -0
- data/test/og/tc_inheritance.rb +6 -4
- data/test/og/tc_joins_many.rb +93 -0
- data/test/og/tc_multi_validations.rb +5 -7
- data/test/og/tc_multiple.rb +7 -6
- data/test/og/tc_override.rb +13 -7
- data/test/og/tc_primary_key.rb +30 -0
- data/test/og/tc_relation.rb +8 -14
- data/test/og/tc_reldelete.rb +163 -0
- data/test/og/tc_reverse.rb +17 -14
- data/test/og/tc_scoped.rb +3 -11
- data/test/og/tc_setup.rb +13 -11
- data/test/og/tc_store.rb +21 -28
- data/test/og/tc_validation2.rb +2 -2
- data/test/og/tc_validation_loop.rb +17 -15
- metadata +109 -103
- data/INSTALL +0 -91
- data/ProjectInfo +0 -51
- data/README +0 -177
- data/doc/config.txt +0 -28
- data/examples/README +0 -23
- data/examples/mysql_to_psql.rb +0 -71
- data/examples/run.rb +0 -271
- data/lib/glue/tree.rb +0 -218
- data/lib/og/store/alpha/filesys.rb +0 -110
- data/lib/og/store/alpha/memory.rb +0 -295
- data/lib/og/store/alpha/sqlserver.rb +0 -256
- data/lib/og/store/kirby.rb +0 -490
- data/lib/og/store/mysql.rb +0 -415
- data/lib/og/store/psql.rb +0 -875
- data/lib/og/store/sqlite.rb +0 -348
- data/lib/og/store/sqlite2.rb +0 -241
- data/setup.rb +0 -1585
- data/test/og/tc_sti_find.rb +0 -35
data/ProjectInfo
DELETED
@@ -1,51 +0,0 @@
|
|
1
|
-
--- %YAML:1.0
|
2
|
-
|
3
|
-
TITLE : &title Og
|
4
|
-
NAME : &pkg og
|
5
|
-
VERSION : '0.31.0'
|
6
|
-
STATUS : beta
|
7
|
-
|
8
|
-
AUTHOR : George Moschovitis
|
9
|
-
EMAIL : &email gm@navel.gr
|
10
|
-
HOMEPAGE : "http://www.nitroproject.org"
|
11
|
-
|
12
|
-
SUMMARY: State of the art object-relational mapping system.
|
13
|
-
|
14
|
-
DESCRIPTION: >
|
15
|
-
Object Graph (Og) is a state of the art ORM system.
|
16
|
-
Og serializes standard Ruby objects to Mysql, Postgres, Sqlite,
|
17
|
-
KirbyBase, Filesystem and more.
|
18
|
-
|
19
|
-
RUBYFORGE:
|
20
|
-
PROJECT: 'nitro'
|
21
|
-
USERNAME: 'gmosx'
|
22
|
-
|
23
|
-
DEPENDENCIES:
|
24
|
-
- [ glue, '= 0.31.0' ]
|
25
|
-
|
26
|
-
PACKAGE: !!package
|
27
|
-
distribute: [ gem, tgz, zip ]
|
28
|
-
|
29
|
-
RDOC: !!rdoc
|
30
|
-
dir: rdoc
|
31
|
-
options: ['--all', '--inline-source']
|
32
|
-
include:
|
33
|
-
- 'lib/og/**/*'
|
34
|
-
- '[A-Z]*'
|
35
|
-
|
36
|
-
TEST: !!test
|
37
|
-
|
38
|
-
ANNOUNCE: !!announce
|
39
|
-
to: george.moschovitis@gmail.com
|
40
|
-
from: gm@navel.gr
|
41
|
-
domain: navel.gr
|
42
|
-
server: mail
|
43
|
-
port: 25 #587
|
44
|
-
account: gm@navel.gr
|
45
|
-
authtype: login #cram_md5 #plain
|
46
|
-
sectype: tls # ~, tls, ssl (tls is broke)
|
47
|
-
file: ANN
|
48
|
-
slogan: Og (ObjectGraph)
|
49
|
-
links:
|
50
|
-
- http://www.nitroproject.org
|
51
|
-
|
data/README
DELETED
@@ -1,177 +0,0 @@
|
|
1
|
-
= Og 0.30.0 README
|
2
|
-
|
3
|
-
Og (ObjectGraph) is a powerful and elegant object-relational mapping
|
4
|
-
library. Og manages the lifecycle of Ruby objects and provides
|
5
|
-
transparent serialization of object graphs to stores (typically
|
6
|
-
RDBM systems)/.
|
7
|
-
|
8
|
-
Unlike other similar solutions Og maps standard Ruby objects
|
9
|
-
to SQL tables and not vice versa. Og provides a domain specific
|
10
|
-
language to describe the relations between objects, a flexible
|
11
|
-
and intuitive api for querieng the database, raw access to the
|
12
|
-
SQL language if needed (for example to fine tune the automatically
|
13
|
-
generated SQL tables, or for custom queries), provides automatic
|
14
|
-
validation, suports deserialization to Ruby objects or tuples,
|
15
|
-
automatically generates join tables for many_to_many relations
|
16
|
-
and provides a collection of usefull Mixins to synthesize
|
17
|
-
common Entities.
|
18
|
-
|
19
|
-
Adapters for PostgreSQL, MySQL, SQLite3, KirbyBase, Memory,
|
20
|
-
Filesystem, Oracle and SQL Server are included.
|
21
|
-
|
22
|
-
Og is part of the Nitro project, released as a stand-alone library
|
23
|
-
due to popular demand. You can find the ChangeLog in the Nitro
|
24
|
-
distribution (http://www.nitroproject.org).
|
25
|
-
|
26
|
-
|
27
|
-
== Features
|
28
|
-
|
29
|
-
The library provides the following features:
|
30
|
-
|
31
|
-
* Object-Relational mapping, automatically maps standard
|
32
|
-
Ruby objects to sql schemas.
|
33
|
-
* Absolutely no configuration files.
|
34
|
-
* Multiple stores (PostgreSQL, MySQL, SQLite, Oraclei, SqlServer, ..).
|
35
|
-
* Supports non SQL stores (KirbyBase, Memory, filesystem, ..).
|
36
|
-
* Can 'reverse engineer' legacy database schemase.
|
37
|
-
* Fine-grained or High-level customization of the generated
|
38
|
-
schema.
|
39
|
-
* ActiveRecord-style domain specific language and db synchronized
|
40
|
-
collections.
|
41
|
-
* Scoped queries on collections.
|
42
|
-
* Transforms resultsets from arbitrary sql queries to Ruby objects.
|
43
|
-
* Independent store for each object class, can support multiple
|
44
|
-
stores in the same application.
|
45
|
-
* Deserialize to Ruby Objects.
|
46
|
-
* Deserialize sql join queries to Ruby Objects.
|
47
|
-
* Can optionally use Ruby as a query language.
|
48
|
-
* Supports model caching (even on distribbuted environments)
|
49
|
-
* Eager associations.
|
50
|
-
* Serialize arbitrary ruby object graphs through YAML.
|
51
|
-
* Connection pooling.
|
52
|
-
* Thread safety.
|
53
|
-
* SQL transactions.
|
54
|
-
* Aspect oriented constructs allow interception of lifecycle callbacks.
|
55
|
-
* Transparent support for cascading deletes for all backends.
|
56
|
-
* Can annotate and manage existing Ruby classes.
|
57
|
-
* Hierarchical structures (nested sets)
|
58
|
-
* Works safely as part of distributed application.
|
59
|
-
* Optimistic locking.
|
60
|
-
* Dynamic finder methods.
|
61
|
-
* Simple implementation.
|
62
|
-
|
63
|
-
|
64
|
-
== What's new
|
65
|
-
|
66
|
-
For information about the latest changes please consult the
|
67
|
-
file
|
68
|
-
|
69
|
-
doc/RELEASES
|
70
|
-
|
71
|
-
|
72
|
-
== Download
|
73
|
-
|
74
|
-
The latest version of Og can be found at
|
75
|
-
|
76
|
-
* http://www.nitroproject.org
|
77
|
-
|
78
|
-
== Documentation
|
79
|
-
|
80
|
-
Documentation for Og can be found at
|
81
|
-
|
82
|
-
* http://www.nitroproject.org
|
83
|
-
|
84
|
-
Don't forget to read the file doc/RELEASES for usefull
|
85
|
-
documentation bits. Also, have a look at the test cases in
|
86
|
-
the test directory for examples of usage. Additional examples
|
87
|
-
of Og usage can be found at the example distribution of the
|
88
|
-
Nitro Web Framework (http://www.nitroproject.org)
|
89
|
-
|
90
|
-
You can find a nice tutorial at www.rubygarden.com. Be warned
|
91
|
-
that this tutorial describes an earlier version of Og. A *LOT*
|
92
|
-
of new features have been added in the meantime.
|
93
|
-
|
94
|
-
|
95
|
-
== Requirements
|
96
|
-
|
97
|
-
Og requires the following applications or libraries:
|
98
|
-
|
99
|
-
* Ruby 1.8.1 and greater
|
100
|
-
http://www.ruby-lang.org
|
101
|
-
Version 1.8.2 is recomended.
|
102
|
-
|
103
|
-
* Ruby-psql
|
104
|
-
http://www.postgresql.jp/interfaces/ruby/archive/ruby-postgres-0.7.1.tar.gz
|
105
|
-
Ruby interface to the PostgreSQL RDBMS.
|
106
|
-
|
107
|
-
* Ruby-mysql
|
108
|
-
http://tmtm.org/ja/ruby/mysql/README_en.html
|
109
|
-
Ruby interface to the MySQL RDBMS.
|
110
|
-
Pure Ruby Mysql interface:
|
111
|
-
http://www.tmtm.org/ruby/mysql/
|
112
|
-
|
113
|
-
* PostgreSQL
|
114
|
-
http://www.postgres.org
|
115
|
-
Used for the Database Backend.
|
116
|
-
|
117
|
-
* MySQL
|
118
|
-
http://www.mysql.org
|
119
|
-
Used for the Database Backend.
|
120
|
-
|
121
|
-
* SQLite
|
122
|
-
http://www.sqlite.org/download.html
|
123
|
-
WARNING: you need sqlite3-ruby > 1.1.0
|
124
|
-
|
125
|
-
Please install the required applications and libraries before continuing
|
126
|
-
with the installation of Og. Only install the libraries needed for
|
127
|
-
the backend you plan to use.
|
128
|
-
|
129
|
-
|
130
|
-
== Instalation
|
131
|
-
|
132
|
-
Og is distributed as a RubyGem. First of all make sure you have
|
133
|
-
installed RubyGems on your system. Then run the following command:
|
134
|
-
|
135
|
-
gem install og
|
136
|
-
|
137
|
-
(make sure you have the latest stable versions of Ruby and RubyGems
|
138
|
-
installed)
|
139
|
-
|
140
|
-
Then try to run the examples in the examples directory.
|
141
|
-
|
142
|
-
A tar.gz distribution is also available on http://www.rubyforge.com/projects/nitro.
|
143
|
-
|
144
|
-
|
145
|
-
== Examples:
|
146
|
-
|
147
|
-
Some examples can be found in the examples directory. You are also
|
148
|
-
encouraged to check out the unit tests. Especially the test:
|
149
|
-
|
150
|
-
test/og/tc_store.rb
|
151
|
-
|
152
|
-
demonstrates some advanced features of Og.
|
153
|
-
|
154
|
-
|
155
|
-
== Support
|
156
|
-
|
157
|
-
For any questions regarding Og, feel free to ask on the ruby-talk
|
158
|
-
mailing list (which is mirrored to comp.lang.ruby) or contact
|
159
|
-
mailto:gm@navel.gr.
|
160
|
-
|
161
|
-
An Og specific mailing list is also available. Please subscribe
|
162
|
-
to nitro-general@rubyforge.com. The homepage for this list
|
163
|
-
is available here:
|
164
|
-
|
165
|
-
http://rubyforge.org/mailman/listinfo/nitro-general
|
166
|
-
|
167
|
-
|
168
|
-
== Licence
|
169
|
-
|
170
|
-
Copyright (c) 2004-2006, George 'gmosx' Moschovitis (http://www.gmosx.com).
|
171
|
-
Copyright (c) 2004-2006, Navel Ltd (http://www.navel.gr)
|
172
|
-
|
173
|
-
Og (http://www.nitroproject.org) is copyrighted free
|
174
|
-
software created and maintained by George Moschovitis (mailto:gm@navel.gr)
|
175
|
-
and released under the standard BSD Licence. For details consult
|
176
|
-
the file LICENCE.
|
177
|
-
|
data/doc/config.txt
DELETED
@@ -1,28 +0,0 @@
|
|
1
|
-
= Og Configuration parameters
|
2
|
-
|
3
|
-
This file presents a complete list of Og configuration
|
4
|
-
parameters.
|
5
|
-
|
6
|
-
=== Og.table_prefix = nil
|
7
|
-
|
8
|
-
Attach the given prefix to all generated SQL table names.
|
9
|
-
Usefull on hosting scenarios where you have to run multiple
|
10
|
-
applications/sites on a single database.
|
11
|
-
|
12
|
-
=== Og.auto_manage_classes = true
|
13
|
-
|
14
|
-
If true, use Ruby's advanced introspection capabilities to
|
15
|
-
automatically manage classes that define properties.
|
16
|
-
|
17
|
-
=== Og.create_schema = true
|
18
|
-
|
19
|
-
If set to true, Og attempts to recreate the database schema
|
20
|
-
to store the managed objects. If the table for an object class
|
21
|
-
exists, Og does not recreate it. It is useful to get Og to
|
22
|
-
automatically create the schema for your application on setup,
|
23
|
-
or when you add a new managed class to your application. On
|
24
|
-
the downside, it inflicts a longer startup time on the
|
25
|
-
application, so it is better to set this to false in
|
26
|
-
production / live environments.
|
27
|
-
|
28
|
-
This option is by default true to facilitate easy development.
|
data/examples/README
DELETED
@@ -1,23 +0,0 @@
|
|
1
|
-
= Og Examples
|
2
|
-
|
3
|
-
Here you can find some simple Og examples. For more examples
|
4
|
-
please download the Nitro examples tarball. Nitro is a Ruby
|
5
|
-
Web Framework that uses Og.
|
6
|
-
|
7
|
-
|
8
|
-
== run.rb
|
9
|
-
|
10
|
-
A simple example that demonstrates some Og features. The example
|
11
|
-
automatically creates a 'test' database.
|
12
|
-
|
13
|
-
|
14
|
-
== mock_example.rb
|
15
|
-
|
16
|
-
Demonstrates how easily the Og infrastructure can be mocked,
|
17
|
-
for easy test unit writing.
|
18
|
-
|
19
|
-
|
20
|
-
== mysql_to_psql.rb
|
21
|
-
|
22
|
-
Demonstrates how easy it is to migrate a mysql database
|
23
|
-
to postgres using Og.
|
data/examples/mysql_to_psql.rb
DELETED
@@ -1,71 +0,0 @@
|
|
1
|
-
# = Mysql to PostgreSQL migration example.
|
2
|
-
#
|
3
|
-
# A simple example to demonstrate the flexibility of
|
4
|
-
# Og. Two connections to different databases are
|
5
|
-
# created and data is copied from a MySQL database
|
6
|
-
# to a PostgreSQL database.
|
7
|
-
#
|
8
|
-
# Og makes it easier to switch to a REAL database :)
|
9
|
-
|
10
|
-
require 'og'
|
11
|
-
|
12
|
-
# Configure databases.
|
13
|
-
|
14
|
-
psql_config = {
|
15
|
-
:destroy => true,
|
16
|
-
:name => 'test',
|
17
|
-
:store => 'psql',
|
18
|
-
:user => 'postgres',
|
19
|
-
:password => 'navelrulez'
|
20
|
-
}
|
21
|
-
|
22
|
-
mysql_config = {
|
23
|
-
:destroy => true,
|
24
|
-
:name => 'test',
|
25
|
-
:store => 'mysql',
|
26
|
-
:user => 'root',
|
27
|
-
:password => 'navelrulez'
|
28
|
-
}
|
29
|
-
|
30
|
-
# Initialize Og.
|
31
|
-
|
32
|
-
psql = Og.connect(psql_config)
|
33
|
-
mysql = Og.connect(mysql_config)
|
34
|
-
|
35
|
-
# An example managed object.
|
36
|
-
# Looks like an ordinary Ruby object.
|
37
|
-
|
38
|
-
class Article
|
39
|
-
property :name, :body, String
|
40
|
-
|
41
|
-
def initialize(name = nil, body = nil)
|
42
|
-
@name, @body = name, body
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
# First populate the mysql database.
|
47
|
-
|
48
|
-
mysql.manage(Article)
|
49
|
-
|
50
|
-
a1 = Article.create('name1', 'body1')
|
51
|
-
a1 = Article.create('name1', 'body1')
|
52
|
-
a1 = Article.create('name1', 'body1')
|
53
|
-
|
54
|
-
# Read all articles from Mysql.
|
55
|
-
|
56
|
-
articles = Article.all
|
57
|
-
|
58
|
-
# Switch to PostgreSQL.
|
59
|
-
|
60
|
-
psql.manage(Article)
|
61
|
-
|
62
|
-
# Store all articles.
|
63
|
-
|
64
|
-
for article in articles
|
65
|
-
article.insert
|
66
|
-
end
|
67
|
-
|
68
|
-
# Fetch an article from PostgreSQL
|
69
|
-
# as an example. Lookup by name.
|
70
|
-
|
71
|
-
article = Article.find_by_name('name1')
|
data/examples/run.rb
DELETED
@@ -1,271 +0,0 @@
|
|
1
|
-
# A simple example to demonstrate the Og library.
|
2
|
-
|
3
|
-
require 'og'
|
4
|
-
|
5
|
-
# Full debug information.
|
6
|
-
|
7
|
-
$DBG = true
|
8
|
-
|
9
|
-
# A child class.
|
10
|
-
|
11
|
-
class Comment
|
12
|
-
property :body, String
|
13
|
-
|
14
|
-
def initialize(body = nil)
|
15
|
-
@body = body
|
16
|
-
end
|
17
|
-
|
18
|
-
def to_s
|
19
|
-
return @body
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
# = A Parent class.
|
24
|
-
|
25
|
-
class User
|
26
|
-
property :name, String, :uniq => true
|
27
|
-
has_many :comments, UserComment
|
28
|
-
|
29
|
-
def initialize(name = nil)
|
30
|
-
@name = name
|
31
|
-
end
|
32
|
-
|
33
|
-
def to_s
|
34
|
-
return @name
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
|
39
|
-
# A parent class.
|
40
|
-
|
41
|
-
class Article
|
42
|
-
property :title, String
|
43
|
-
property :body, String
|
44
|
-
|
45
|
-
# override the default O->R mapping
|
46
|
-
|
47
|
-
property :level, Fixnum, :sql => "smallint DEFAULT 1"
|
48
|
-
|
49
|
-
# store a Ruby Hash in the Database. YAML
|
50
|
-
# is used for serializing the attribute.
|
51
|
-
# no need to define the class, but you can if you want.
|
52
|
-
|
53
|
-
property :options, Hash
|
54
|
-
|
55
|
-
# exactly like the standard Ruby attr creates only the reader.
|
56
|
-
|
57
|
-
prop :create_time, Time
|
58
|
-
|
59
|
-
# define comment relation:
|
60
|
-
|
61
|
-
has_many :comments, ArticleComment
|
62
|
-
|
63
|
-
has_many :parts, Part
|
64
|
-
|
65
|
-
# many to many relation.
|
66
|
-
|
67
|
-
many_to_many Category
|
68
|
-
|
69
|
-
# define author relation:
|
70
|
-
|
71
|
-
belongs_to :author, User
|
72
|
-
|
73
|
-
# this attribute is NOT stored in the db.
|
74
|
-
|
75
|
-
attr_accessor :other_options
|
76
|
-
|
77
|
-
# Managed object constructors with no args, take *args
|
78
|
-
# as parameter to allow for Mixin chaining.
|
79
|
-
|
80
|
-
def initialize(title = nil, body = nil)
|
81
|
-
@title, @body = title, body
|
82
|
-
@create_time = Time.now
|
83
|
-
@options = {}
|
84
|
-
@other_options = {}
|
85
|
-
end
|
86
|
-
|
87
|
-
def to_s
|
88
|
-
return "#@title: #@body"
|
89
|
-
end
|
90
|
-
end
|
91
|
-
|
92
|
-
# A parent class.
|
93
|
-
|
94
|
-
class Category
|
95
|
-
property :title, String
|
96
|
-
property :body, String
|
97
|
-
|
98
|
-
# define a 'many to many' relation.
|
99
|
-
|
100
|
-
many_to_many Article
|
101
|
-
|
102
|
-
def initialize(title = nil)
|
103
|
-
@title = title
|
104
|
-
end
|
105
|
-
end
|
106
|
-
|
107
|
-
|
108
|
-
# Article comment.
|
109
|
-
|
110
|
-
class ArticleComment < Comment
|
111
|
-
belongs_to Article
|
112
|
-
end
|
113
|
-
|
114
|
-
# User comment.
|
115
|
-
|
116
|
-
class UserComment < Comment
|
117
|
-
belongs_to :author, User
|
118
|
-
end
|
119
|
-
|
120
|
-
# Another child class.
|
121
|
-
|
122
|
-
class Part
|
123
|
-
property :name, String
|
124
|
-
belongs_to Article
|
125
|
-
|
126
|
-
def initialize(name = nil)
|
127
|
-
@name = name
|
128
|
-
end
|
129
|
-
|
130
|
-
def to_s
|
131
|
-
return @name
|
132
|
-
end
|
133
|
-
end
|
134
|
-
|
135
|
-
# Og configuration.
|
136
|
-
|
137
|
-
config = {
|
138
|
-
:destroy => true, # destroy table created from earlier runs.
|
139
|
-
:store => :sqlite,
|
140
|
-
:name => 'test',
|
141
|
-
:user => "postgres",
|
142
|
-
:password => "navelrulez"
|
143
|
-
}
|
144
|
-
|
145
|
-
# Initialize Og
|
146
|
-
|
147
|
-
db = Og.setup(config)
|
148
|
-
|
149
|
-
# Create some articles
|
150
|
-
|
151
|
-
a1 = Article.new('Title1', 'Body1')
|
152
|
-
a1.save
|
153
|
-
|
154
|
-
# shortcut
|
155
|
-
|
156
|
-
a2 = Article.create('Title2', 'Body2')
|
157
|
-
|
158
|
-
puts "\n\n"
|
159
|
-
puts "* Get and print all articles:"
|
160
|
-
articles = Article.all
|
161
|
-
articles.each { |a| puts a }
|
162
|
-
|
163
|
-
# Create some comments
|
164
|
-
|
165
|
-
c1 = ArticleComment.new('Comment 1')
|
166
|
-
c1.article = a1
|
167
|
-
c1.save
|
168
|
-
|
169
|
-
c2 = ArticleComment.new('Comment 2')
|
170
|
-
# alternative way to set the parent.
|
171
|
-
c2.article_oid = a1.oid
|
172
|
-
# an alternative way to save
|
173
|
-
db.store << c2
|
174
|
-
|
175
|
-
# an alternative (easier and cooler) way to add children in a
|
176
|
-
# has_many relation:
|
177
|
-
c3 = ArticleComment.new('Comment 3')
|
178
|
-
# add_comment is automatically added by Og.
|
179
|
-
a1.comments << c3
|
180
|
-
|
181
|
-
puts "\n\n"
|
182
|
-
puts "* Print all all comments for article 1:"
|
183
|
-
a1.comments.each { |c| puts c }
|
184
|
-
|
185
|
-
# Most Og commands allow you to fine-tune the low level
|
186
|
-
# SQL code by passing extra_sql parameters, here is an
|
187
|
-
# example
|
188
|
-
puts "\n\n"
|
189
|
-
puts "* comments with sql finetunings:"
|
190
|
-
# use a standard SQL limit clause
|
191
|
-
a1.comments(:limit => 2).each { |c| puts c }
|
192
|
-
|
193
|
-
|
194
|
-
# Change a managed object
|
195
|
-
a1.title = 'Changed Title'
|
196
|
-
# Og knows that this is a managed object and executes
|
197
|
-
# an SQL UPDATE instead of an SQL INSERT
|
198
|
-
a1.save!
|
199
|
-
|
200
|
-
puts "\n\n"
|
201
|
-
Article.all.each { |a| puts a }
|
202
|
-
|
203
|
-
# The previous command updates the whole object. It is used
|
204
|
-
# when there are many updates or you dont care about speed.
|
205
|
-
# You can also update specific fields
|
206
|
-
a2.title = 'A specific title'
|
207
|
-
a2.update(:properties => [:title])
|
208
|
-
|
209
|
-
puts "\n\n"
|
210
|
-
Article.all.each { |a| puts a }
|
211
|
-
|
212
|
-
# delete an object
|
213
|
-
puts '-----------------1'
|
214
|
-
ArticleComment.delete(c3)
|
215
|
-
puts '-----------------2'
|
216
|
-
|
217
|
-
puts "\n\n"
|
218
|
-
ArticleComment.all.each { |a| puts a }
|
219
|
-
|
220
|
-
|
221
|
-
# Serialize a hash
|
222
|
-
a1.options = { :k1 => 'val1', :k2 => 'val2' }
|
223
|
-
a1.save!
|
224
|
-
|
225
|
-
# lookup an object
|
226
|
-
article = Article[a1.oid]
|
227
|
-
|
228
|
-
puts "\n\n"
|
229
|
-
puts article.options.inspect
|
230
|
-
|
231
|
-
u = User.new('gmosx')
|
232
|
-
u.save!
|
233
|
-
|
234
|
-
article = Article[1]
|
235
|
-
# you can also lookup by the name property.
|
236
|
-
article.author = User.find_by_name('gmosx')
|
237
|
-
article.save!
|
238
|
-
|
239
|
-
part = Part.new('admin')
|
240
|
-
part.article = article
|
241
|
-
part.save!
|
242
|
-
|
243
|
-
article.parts.each { |pa| puts pa }
|
244
|
-
|
245
|
-
puts "\n\n"
|
246
|
-
puts '---'
|
247
|
-
|
248
|
-
c1 = Category.create('Category1')
|
249
|
-
c2 = Category.create('Category2')
|
250
|
-
|
251
|
-
article.categories << c1
|
252
|
-
article.categories << c2
|
253
|
-
|
254
|
-
puts '---'
|
255
|
-
|
256
|
-
article.categories.each { |c| puts c.title }
|
257
|
-
|
258
|
-
puts '---'
|
259
|
-
|
260
|
-
c2.articles.each { |a| puts a.title }
|
261
|
-
|
262
|
-
article.categories.delete(c1)
|
263
|
-
|
264
|
-
puts '---'
|
265
|
-
|
266
|
-
article.categories.each { |c| puts c.title }
|
267
|
-
|
268
|
-
# create and save the article in one step.
|
269
|
-
article = Article.create('title', 'body')
|
270
|
-
|
271
|
-
puts '--', article.oid
|