Sha256: e7343df8bfe12dfb59d45977259d02848caf0a03a65700dd9317c22f6862fdf2

Contents?: true

Size: 1.2 KB

Versions: 9

Compression:

Stored size: 1.2 KB

Contents

require 'pp'
require 'pathname'
require 'rubygems'
require 'adapter/memory'

root_path = Pathname(__FILE__).dirname.join('..').expand_path
lib_path  = root_path.join('lib')
$:.unshift(lib_path)
require 'toystore'

# More Reading...
# http://martinfowler.com/eaaCatalog/identityMap.html
# http://en.wikipedia.org/wiki/Identity_map

# Identity map is turned off by default for Toystore so let's turn it on
Toy::IdentityMap.enabled = true
puts 'Identity map enabled'

class User
  include Toy::Store

  attribute :name, String
end

user = User.create(:name => 'John')
# User is added to identity map
# ToyStore SET #<User:0x10220b4c8> :memory "08a1c54c-3393-11e0-8b37-89da41d2f675"
#   {"name"=>"John"}
# ToyStore IMS User "08a1c54c-3393-11e0-8b37-89da41d2f675"

puts "With identity map - User.get(user.id).equal?(user): #{User.get(user.id).equal?(user)}"
# User is retrieved from identity map instead of querying
# ToyStore IMG User "08a1c54c-3393-11e0-8b37-89da41d2f675"

Toy::IdentityMap.without do
  puts "Without identity map - User.get(user.id).equal?(user): #{User.get(user.id).equal?(user)}"
  # User is queried from database
  # ToyStore GET User :memory "08a1c54c-3393-11e0-8b37-89da41d2f675"
  #   {"name"=>"John"}
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
toystore-0.13.0 examples/identity_map.rb
toystore-0.12.0 examples/identity_map.rb
toystore-0.11.0 examples/identity_map.rb
toystore-0.10.4 examples/identity_map.rb
toystore-0.10.3 examples/identity_map.rb
toystore-0.10.2 examples/identity_map.rb
toystore-0.10.1 examples/identity_map.rb
toystore-0.10.0 examples/identity_map.rb
toystore-0.9.0 examples/identity_map.rb