[Nitro] Idiomatic use for concise Ogging?

Aleksi Niemela Aleksi.Niemela at cs.helsinki.fi
Sat Jun 18 07:16:38 EDT 2005

Actually I had only one question but ended up writing three points.


What would be the idiomatic way to write this kind of code (at the end 
of the mail), so that code would be clean and concise and it would work 
as supposed. I don't like the fact I have to write '.first'.

Also writing 4 lines to get hold of an object (regardless if it does or 
doesn't exist in database) seems too much. And when the field I'm 
identifying the object uniquely isn't 'name' to find_by_name the code 
gets a bit more complicated and even more non-uniform.


Anyways, I'd like to share you a "trick" to create assosiations in a 
concise way when creating new managed objects (at Bar.initialize). 
Please note that the three other versions to initialize the association 
don't work (and leave it to value nil in database) but self.<name of 
association>=(value) form works.


Also, auto-destroying database doesn't work for me, printing this on cygwin:

  $ ruby test_data.rb
  dropdb: could not connect to database template1: could not connect to 
server: No such file or directory
          Is the server running locally and accepting
          connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
  DEBUG: Og manageable classes: [Foo, Bar]
   INFO: Created table 'ogfoo'.
   INFO: Created table 'ogbar'.
  DEBUG: SELECT * FROM ogfoo WHERE name='foo'
  DEBUG: SELECT * FROM ogbar WHERE name='bar'

This is due to the fact a command "dropdb tilannetallenne -U aleksi" is 
issued, but command like

  dropdb -U aleksi -W password -h localhost tilannetallenne

should have been issued (both password and host real location are 
important). I didn't write a patch since this approach would not work 
either for me. Problem is that postgres user aleksi isn't owner of the 
database so he can't drop it. Better approach would be to find out all 
tables that were created for managing objects and execute sql to drop those.

    - Aleksi

require 'rubygems'
require_gem 'postgres-pr'
require_gem 'og'

$DBG = true

class Foo
  property :name, String
  def initialize(name)
    @name = name

class Bar
  property :name, String
  belongs_to :foo, Foo
  def initialize(name, foo)
    @name = name
#    foo=foo
#    @foo=foo
#    foo=(foo)
    self.foo=(foo)   # this does create the association

config = {
  :address => "localhost",
  :destroy => true,

  :name => "tilannetallenne",

  :store => "psql",
  :user => "aleksi",
  :password => "password",
  :connection_count => 5

db = Og::setup(config)

f = Foo.find_by_name("foo").first
unless f
  f = Foo.create("foo")

b = Bar.find_by_name("bar").first
unless b
  Bar.create("bar", f)

More information about the Nitro-general mailing list