Rubernate

{Dynamic Persistence
for Dynamic Language}

#
# This example demonstrates basic operations on persistent objects
#
require 'ex0_config'

class SomeClass
  persistent :value  # Create persistent attribute.

  def print_value
    puts "value = #{value}" # Use method 'value' not field '@value'.
  end
end

pk = nil

# Begin session
Rubernate.session do
  # Create instance of +SomeClass+ and attach it to session.
  o = SomeClass.new.attach

  # Since object has been attached to session its primary_key can be accessed.
  pk = o.primary_key
  puts "pk = #{pk}"  

  # Set persistent property
  o.value = 'hello'
end # Transaction commited, object is stored.


Rubernate.session do
  # Find object by primary key
  o = Rubernate.find_by_pk pk
  puts "object pk: #{o.primary_key} has been found";

  # Print stored value
  o.print_value
      
  # Delete objects found.
  puts "delete object: pk = #{o.primary_key}"
  o.remove!
end # Commit transaction.


# Check if object has been deleted
Rubernate.session do
  begin
    o = Rubernate.find_by_pk pk    
    puts "If you can see this message then something is going wrong!!!"
  rescue Rubernate::ObjectNotFoundException
    puts "object: pk = #{pk} not found"
  end
end