require 'og'
$DBG = true
class Comment
attr_accessor :body, String
def initialize(body = nil)
@body = body
end
def to_s
return @body
end
end
class User
attr_accessor :name, String, :uniq => true
has_many :comments, UserComment
def initialize(name = nil)
@name = name
end
def to_s
return @name
end
end
class Article
attr_accessor :title, String
attr_accessor :body, String
attr_accessor :level, Fixnum, :sql => "smallint DEFAULT 1"
attr_accessor :options, Hash
prop :create_time, Time
has_many :comments, ArticleComment
has_many :parts, Part
many_to_many Category
belongs_to :author, User
attr_accessor :other_options
def initialize(title = nil, body = nil)
@title, @body = title, body
@create_time = Time.now
@options = {}
@other_options = {}
end
def to_s
return "#@title: #@body"
end
end
class Category
attr_accessor :title, String
attr_accessor :body, String
many_to_many Article
def initialize(title = nil)
@title = title
end
end
class ArticleComment < Comment
belongs_to Article
end
class UserComment < Comment
belongs_to :author, User
end
class Part
attr_accessor :name, String
belongs_to Article
def initialize(name = nil)
@name = name
end
def to_s
return @name
end
end
config = {
:destroy => true,
:store => :sqlite,
:name => 'test',
:user => "postgres",
:password => "gmrulez"
}
db = Og.setup(config)
a1 = Article.new('Title1', 'Body1')
a1.save
a2 = Article.create('Title2', 'Body2')
puts "\n\n"
puts "* Get and print all articles:"
articles = Article.all
articles.each { |a| puts a }
c1 = ArticleComment.new('Comment 1')
c1.article = a1
c1.save
c2 = ArticleComment.new('Comment 2')
c2.article_oid = a1.oid
db.store << c2
c3 = ArticleComment.new('Comment 3')
a1.comments << c3
puts "\n\n"
puts "* Print all all comments for article 1:"
a1.comments.each { |c| puts c }
puts "\n\n"
puts "* comments with sql finetunings:"
a1.comments(:limit => 2).each { |c| puts c }
a1.title = 'Changed Title'
a1.save!
puts "\n\n"
Article.all.each { |a| puts a }
a2.title = 'A specific title'
a2.update(:properties => [:title])
puts "\n\n"
Article.all.each { |a| puts a }
puts '-----------------1'
ArticleComment.delete(c3)
puts '-----------------2'
puts "\n\n"
ArticleComment.all.each { |a| puts a }
a1.options = { :k1 => 'val1', :k2 => 'val2' }
a1.save!
article = Article[a1.oid]
puts "\n\n"
puts article.options.inspect
u = User.new('gmosx')
u.save!
article = Article[1]
article.author = User.find_by_name('gmosx')
article.save!
part = Part.new('admin')
part.article = article
part.save!
article.parts.each { |pa| puts pa }
puts "\n\n"
puts '---'
c1 = Category.create('Category1')
c2 = Category.create('Category2')
article.categories << c1
article.categories << c2
puts '---'
article.categories.each { |c| puts c.title }
puts '---'
c2.articles.each { |a| puts a.title }
article.categories.delete(c1)
puts '---'
article.categories.each { |c| puts c.title }
article = Article.create('title', 'body')
puts '--', article.oid
Latest changes
All changes