Skip to content

Instantly share code, notes, and snippets.

@mbj
Created November 22, 2012 22:28
Show Gist options
  • Save mbj/4133170 to your computer and use it in GitHub Desktop.
Save mbj/4133170 to your computer and use it in GitHub Desktop.
dm-session mapper interface

Mapper interfaces as used by dm-session

Mapper#loader(body)   => [Loader]      # Loader for specific body
Mapper#dumper(object) => [Dumper]      # Dumper for object
Mapper#execute(command)                # command is an instance of DataMapper::Session::Command::{Insert,Update,Delete}

Loader#identity       => [Identity]    # Any mapper specific object that implements#hash
Loader#object         => [Object]      # The loaded domain object

Dumper#identity       => [Identity]    # Any mapper specific object that implements#hash
Dumper#body           => [Dump]        # The dumped domain suitable for database interaction

# The mapper decides about the identity an object is tracked under!
# The identity only needs to support #eql? and #hash.
#
# Example:
#   For RDBMS backed stuff identities could be an object that wrap table name and sequencial id.
#   For mongo it would be the BSON::ObjectId
#   For other document stuff a UUID
#
Identity#hash         => [Fixnum]      # For hash position
Identity#eql?(other)  => [true, false] # For hash collision

# The mapper decides about the serialized representation of an object.
# 
# Example:
#   For a mapper that is backed by veritas the returned dump could be a Veritas::Tuple object.
#   As the commands transport the dumps (tuples) back to the mapper on updates/inserts/deletes the tuples can 
#   be used to call Veritas::Relation#{delete,insert}.
#
#   For a mapper that is backed directly by mongo (or any other 
Dump#eql?(other)      => [true, false] # For dirty detection

DataMapper::Session::Command::{Insert,Delete}
  #identity            => [Identity]
  #object              => [Object]
  #body                => [Dump]

DataMApper::Session::Command::Update
  #identity            => [Identity]
  #object              => [Object]
  #body                => [Dump]
  #old_body            => [Dump] 

Mapper transitivity

For dm-session to work properly the mapper must guarantee the following properties:

mapper   = some_mapper
body     = some_body

loader   = mapper.loader(body)
identity = loader.identity

object   = loader.object
dumper   = mpper.dumper(object)

dumper.body.should     eql(body)
dumper.identity.should eql(identity)
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment