Mapper for DataMapper 2
Switch branches/tags
Nothing to show
Pull request Compare This branch is 1330 commits behind solnic:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
config
lib
spec
tasks
.gitignore
.travis.yml
Gemfile
Guardfile
README.md
Rakefile
TODO
dm-mapper.gemspec

README.md

The Mapper for DataMapper 2

Build Status

The mapper for DataMapper 2 is a thin wrapper around Veritas relations. It gives you the power of mapping data into PORO.

It currently works with PostgreSQL - more databases will be added soon.

Roadmap

  • Mapping relationships (prototype is ready)
  • MongoDB support
  • Support for Veritas functions
  • Extend existing Query API with more common features
  • Integration with Virtus
  • Automatic generation of mappers based on model definitions

Establishing Connection & Defining PORO with mappers

# Setup db connection
DataMapper.setup(:postgres, "postgres://localhost/test")

# Define a PORO
class User
  attr_reader :id, :name

  def initialize(attributes)
    @id, @name = attributes.values_at(:id, :name)
  end
end

# Define a mapper
class Mapper < DataMapper::VeritasMapper
  map :id, :type => Integer
  map :name, :to => :username, :type => String

  model         User
  relation_name :users
  repository    :postgres
end

# Finalize setup
DataMapper.finalize

Finding Objects

Mappers come with a simple high-level query API similar to what you know from other Ruby ORMS:

# Find all users matching criteria
DataMapper[User].find(:age => 21)

# Find and sort users
DataMapper[User].find(:age => 21).order(:name, :age)

# Get one object matching criteria
DataMapper[User].one(:name => 'Piotr')

Low-level API using underlying relations

You can interact with the underlying relations if you want. A more "user friendly" API will be built on top of that.

Mappers are enumerables so it should feel natural when working with them.

# Grab the user mapper instance and have fun
user_mapper = DataMapper[User]

# Get them all
user_mapper.to_a

# Iterate on all users
user_mapper.each { |user| puts user.name }

# Restrict
user_mapper.restrict { |relation| relation.name.eq('John') }.to_a

# Sort by
user_mapper.sort_by { |r| [ r.name, r.id ] }.to_a