Skip to content

Instantly share code, notes, and snippets.

@warrickcustomhomes
Created June 22, 2010 16:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save warrickcustomhomes/0a77160612394a0ecc67 to your computer and use it in GitHub Desktop.
Save warrickcustomhomes/0a77160612394a0ecc67 to your computer and use it in GitHub Desktop.
if $0 == __FILE__
require 'rubygems'
require 'dm-core'
require 'dm-migrations'
require 'dm-types'
require 'dm-validations'
end
module App
module Models
class System
include DataMapper::Resource
storage_names[:default] = 'systems'
property :id, Serial
property :description, String
property :mva_base, Float
has n, :simulations
end
class Simulation
include DataMapper::Resource
storage_names[:default] = 'simulations'
property :id, UUID, :key => true, :unique_index => true, :default => lambda { UUIDTools::UUID.random_create }
property :description, String
property :years, Integer
property :seed, Integer
belongs_to :system
end
end
end
if $0 == __FILE__
DataMapper::Logger.new($stdout, :debug)
DataMapper.setup(:default, { :adapter => 'postgres', :database => 'app' })
DataMapper.auto_migrate!
begin
system = App::Models::System.new :description => 'test system', :mva_base => 100.0
system.raise_on_save_failure = true
system.save
sim = App::Models::Simulation.new :system => system, :description => 'test sim', :years => '100', :seed => '10546712'
sim.raise_on_save_failure = true
sim.save
rescue => ex
puts ex.message
ensure
puts sim.inspect
puts sim.errors.inspect
puts sim.saved?
end
end
scrapcoder@scraptop:~$ ruby uuid-test.rb
~ (0.001205) SET backslash_quote = off
~ (0.001053) SET standard_conforming_strings = on
~ (0.000960) SET client_min_messages = warning
~ (0.001733) SELECT version()
~ (0.001020) SET client_min_messages = warning
~ (0.034073) DROP TABLE IF EXISTS "systems"
~ (0.001057) RESET client_min_messages
~ (0.000919) SET client_min_messages = warning
~ (0.001255) SELECT current_schema()
~ (0.006177) SELECT COUNT(*) FROM "information_schema"."tables" WHERE "table_type" = 'BASE TABLE' AND "table_schema" = 'public' AND "table_name" = 'systems'
~ (0.070804) CREATE TABLE "systems" ("id" SERIAL NOT NULL, "description" VARCHAR(50), "mva_base" DOUBLE PRECISION, PRIMARY KEY("id"))
~ (0.001143) RESET client_min_messages
~ (0.001227) SET client_min_messages = warning
~ (0.005704) DROP TABLE IF EXISTS "simulations"
~ (0.000995) RESET client_min_messages
~ (0.000871) SET client_min_messages = warning
~ (0.002370) SELECT COUNT(*) FROM "information_schema"."tables" WHERE "table_type" = 'BASE TABLE' AND "table_schema" = 'public' AND "table_name" = 'simulations'
~ (0.068049) CREATE TABLE "simulations" ("id" VARCHAR(36) NOT NULL, "description" VARCHAR(50), "years" INTEGER, "seed" INTEGER, "system_id" INTEGER NOT NULL, PRIMARY KEY("id"))
~ (0.058213) CREATE INDEX "index_simulations_system" ON "simulations" ("system_id")
~ (0.001044) RESET client_min_messages
~ (0.005402) INSERT INTO "systems" ("description", "mva_base") VALUES ('test system', 100.0) RETURNING "id"
App::Models::Simulation#save returned false, App::Models::Simulation was not saved
#<App::Models::Simulation @id=#<UUID:0x-24721480 UUID:8a43d3a1-02ec-427d-ab3e-9b6915c36412> @description="test sim" @years=100 @seed=10546712 @system_id=1>
#<DataMapper::Validations::ValidationErrors:0xb71be5d8 @resource=#<App::Models::Simulation @id=#<UUID:0x-24721480 UUID:8a43d3a1-02ec-427d-ab3e-9b6915c36412> @description="test sim" @years=100 @seed=10546712 @system_id=1>, @errors=#<OrderedHash {}>>
false
if $0 == __FILE__
require 'rubygems'
require 'dm-core'
require 'dm-migrations'
require 'dm-validations'
end
module App
module Models
class System
include DataMapper::Resource
storage_names[:default] = 'systems'
property :id, Serial
property :description, String
property :mva_base, Float
has n, :simulations
end
class Simulation
include DataMapper::Resource
storage_names[:default] = 'simulations'
property :id, Serial
property :description, String
property :years, Integer
property :seed, Integer
belongs_to :system
end
end
end
if $0 == __FILE__
DataMapper::Logger.new($stdout, :debug)
DataMapper.setup(:default, { :adapter => 'postgres', :database => 'app' })
DataMapper.auto_migrate!
begin
system = App::Models::System.new :description => 'test system', :mva_base => 100.0
system.raise_on_save_failure = true
system.save
sim = App::Models::Simulation.new :system => system, :description => 'test sim', :years => '100', :seed => '10546712'
sim.raise_on_save_failure = true
sim.save
rescue => ex
puts ex.message
ensure
puts sim.inspect
puts sim.errors.inspect
puts sim.saved?
end
end
scrapcoder@scraptop:~$ ruby uuid-test.rb
~ (0.001204) SET backslash_quote = off
~ (0.001023) SET standard_conforming_strings = on
~ (0.000966) SET client_min_messages = warning
~ (0.001721) SELECT version()
~ (0.000905) SET client_min_messages = warning
~ (0.039933) DROP TABLE IF EXISTS "systems"
~ (0.001065) RESET client_min_messages
~ (0.000911) SET client_min_messages = warning
~ (0.001236) SELECT current_schema()
~ (0.006134) SELECT COUNT(*) FROM "information_schema"."tables" WHERE "table_type" = 'BASE TABLE' AND "table_schema" = 'public' AND "table_name" = 'systems'
~ (0.088659) CREATE TABLE "systems" ("id" SERIAL NOT NULL, "description" VARCHAR(50), "mva_base" DOUBLE PRECISION, PRIMARY KEY("id"))
~ (0.001149) RESET client_min_messages
~ (0.000913) SET client_min_messages = warning
~ (0.006026) DROP TABLE IF EXISTS "simulations"
~ (0.000960) RESET client_min_messages
~ (0.000903) SET client_min_messages = warning
~ (0.002169) SELECT COUNT(*) FROM "information_schema"."tables" WHERE "table_type" = 'BASE TABLE' AND "table_schema" = 'public' AND "table_name" = 'simulations'
~ (0.076521) CREATE TABLE "simulations" ("id" SERIAL NOT NULL, "description" VARCHAR(50), "years" INTEGER, "seed" INTEGER, "system_id" INTEGER NOT NULL, PRIMARY KEY("id"))
~ (0.074828) CREATE INDEX "index_simulations_system" ON "simulations" ("system_id")
~ (0.001038) RESET client_min_messages
~ (0.005409) INSERT INTO "systems" ("description", "mva_base") VALUES ('test system', 100.0) RETURNING "id"
~ (0.010884) INSERT INTO "simulations" ("description", "years", "seed", "system_id") VALUES ('test sim', 100, 10546712, 1) RETURNING "id"
#<App::Models::Simulation @id=1 @description="test sim" @years=100 @seed=10546712 @system_id=1>
#<DataMapper::Validations::ValidationErrors:0xb733865c @resource=#<App::Models::Simulation @id=1 @description="test sim" @years=100 @seed=10546712 @system_id=1>, @errors=#<OrderedHash {}>>
true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment