Rubernate

{Dynamic Persistence
for Dynamic Language}

#
# This example demonstrates supported types of attributes
# Rubernate support:
#       Integer, Float, String, Date, Time, Reference to anther periseter object,
#       Array of References, Hash of References with keys of type Integer, Float, String, Date, Time.
#
require 'ex0_config'

class Ex3Class
  persistent :prop  
  def to_s
    "<pk: #{primary_key}>"
  end
end

pk = nil

Rubernate.session do
  # Create object and attach it to session
  o = Ex3Class.new.attach
  # Remember primary keys
  pk = o.primary_key

  # Set value of type Integer
  o.prop = 777
end # Commit transaction

Rubernate.session do
  # Find object by pk
  o = Rubernate.find_by_pk pk

  # Print value setted in previous transaction
  puts "class: #{o.class}  - #{o.prop}"

  # Set value of type Float
  o.prop = 3.1415
end

Rubernate.session do
  o = Rubernate.find_by_pk pk

  # Print value setted in previous transaction
  puts "class: #{o.class}  - #{o.prop}"

  # Set value of type String
  o.prop = 'Hello World'    
end

Rubernate.session do
  o = Rubernate.find_by_pk pk
  
  puts "class: #{o.class}  - #{o.prop}"

  # Set value of type - Time
  o.prop = Time.now
end

# The same with type Date

Rubernate.session do
  o = Rubernate.find_by_pk pk

  puts "class: #{o.class}  - #{o.prop}"

  # Set value of type - Persistent object..   
  o.prop = o # Reverence to itself.
end

Rubernate.session do
  o = Rubernate.find_by_pk pk

  puts "class: #{o.class}  - #{o.prop}"

  # Set value of type - Array of persistent objects   
  o.prop = [o, o, o] # Array contains three references to itself
end

Rubernate.session do
  o = Rubernate.find_by_pk pk

  puts "class: #{o.class}  - #{o.prop}"

  # Set value of type - Hash of persistent objects with keys of type Integer, Float, String, Date or Time.
  o.prop = {
    27  => o,  # Key - Integer,  value - Reference to itself
    1.5 => o,  # Key - Float,      value - Reference to itself
    'hi!' => o, # Key - String
    Time.now => o, # Key - Time
    Date.today => o, # Key - Date  
  }  
end

Rubernate.session do
  o = Rubernate.find_by_pk pk
  # Print value setted in previous transaction
  puts "class: #{o.class}  - #{o.prop}"
  # Remove object
  o.remove!
end