Sha256: 047c2866176c021dc89a115ef842fface7760962d7453250e30c759d28c76e69

Contents?: true

Size: 1.2 KB

Versions: 2

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.read(user.id).equal?(user): #{User.read(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.read(user.id).equal?(user): #{User.read(user.id).equal?(user)}"
  # User is queried from database
  # ToyStore GET User :memory "08a1c54c-3393-11e0-8b37-89da41d2f675"
  #   {"name"=>"John"}
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
toystore-0.13.2 examples/identity_map.rb
toystore-0.13.1 examples/identity_map.rb