Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
require 'rubygems'
require 'dm-core'
DataMapper::Logger.new(STDOUT, :debug)
DataMapper.setup(:default, "mysql://root@localhost/foo")
class Foo
include DataMapper::Resource
property :id, Serial
property :str, String
end
::DataMapper.auto_migrate!
Foo.create(:str => 'foo')
foo = Foo.first(:str => 'foo')
puts "(should be empty) " + foo.dirty_attributes.inspect
foo.str = 'bar'
puts "(should be :str => 'bar') " + foo.dirty_attributes.inspect
foo.str = 'foo'
puts "(should be empty) " + foo.dirty_attributes.inspect
foo.str = 'blort'
puts "(should be :str => 'blort') " + foo.dirty_attributes.inspect
$ ruby foo.rb
~ (0.000091) SET sql_auto_is_null = 0
~ (0.000071) SET SESSION sql_mode = 'ANSI,NO_BACKSLASH_ESCAPES,NO_DIR_IN_CREATE,NO_ENGINE_SUBSTITUTION,NO_UNSIGNED_SUBTRACTION,TRADITIONAL'
~ (0.001998) DROP TABLE IF EXISTS `foos`
~ (0.000259) SHOW TABLES LIKE 'foos'
~ (0.000135) SHOW VARIABLES LIKE 'character_set_connection'
~ (0.000102) SHOW VARIABLES LIKE 'collation_connection'
~ (0.001202) CREATE TABLE `foos` (`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT, `str` VARCHAR(50), PRIMARY KEY(`id`)) ENGINE = InnoDB CHARACTER SET utf8 COLLATE utf8_general_ci
~ (0.040553) INSERT INTO `foos` (`str`) VALUES ('foo')
~ (0.000209) SELECT `id`, `str` FROM `foos` WHERE `str` = 'foo' ORDER BY `id` LIMIT 1
(should be empty) {}
(should be :str => 'bar') {#<DataMapper::Property @model=Foo @name=:str>=>"bar"}
(should be empty) {#<DataMapper::Property @model=Foo @name=:str>=>"foo"}
(should be :str => 'blort') {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment