DMap

DMap is a generator for creating models with DataMapper. It’s currently under a lot of development but I, as well as others, use it for production use. DataMapper gems are not even required for this gem/tool.

“DMap a shorter way for saying, and using, DataMapper”

Installation

gem install dmapper

Usage

As of right now the only command available is new. Here’s a basic example of DMap to explain the structure…

dmap new user id name:string email:str:required,unique str:password:required

The first, after dmap new, is the [table]. You can create multiple tables at a time by adding a comma (,). Every argument (space) is a separate [field]. Fields are divided into three sections by a colon (:). The first part is the field’s [type]. The second part is the field’s name, and the last part marks additional options for that field (explained later).

Options

-b, --backup

Backs up the current model files (renames them to *.rb.bk)

-h, --help

Displays help message

-s, --silent

If you entered incorrect values for a validation just suppress it and continue on.

-t, --test

Displays what DMap would have made into a file onto the screen.

-v, --verbose

Will print out the model(s) after changes have been made

Examples

Using DMap is incredibly easy once you learn the basic structure of it all. Here’s a quick example…

dmap new user id username:str email:str signature:text

Which creates (user.rb file in the folder that you’re currently in)…

class User
  include DataMapper::Resource

  property :id, Serial
  property :username, String
  property :email, String
  property :signature, Text

end

Let’s get a little more “complex”

dmap new user id username:str email:str:required name:str:required,unique password:str date_crtd:datetime:default=DateTime.now signature:text

Which produces…

class User
  include DataMapper::Resource

  property :id, Serial
  property :username, String
  property :email, String, :required => true
  property :name, String, :required => true, :unique => true
  property :password, String
  property :date_crtd, DateTime, :default => proc => { DateTime.now }
  property :signature, Text

end

I know, still not that impressive, let’s try something a tad more difficult…

dmap new user,user_copy id first_name,last_name:str name1:datetime:default=true,length=1..5,presence_of=title-publish20..50,length_of=1,absent,confirm=field,format=email_address,primitive,unique,accessor=private Name1:str

Which creates (along with a copy model “UserCopy” of the same exact thing)

class User
  include DataMapper::Resource

  property :id, Serial
  property :first_name, String
  property :last_name, String
  property :name1, DateTime, :default => true, :length => 1..5, :unique => true, :accessor => :private
  property :Name1, String

  validates_format_of :name1, :as => :email_address 
  validates_confirmation :name1, :confirm => :field 
  validates_absence_of :name1 
  validates_length_of :name1, :equals => 1 
  validates_presence_of :name1, :within => 20..50, :when => [:title, :publish] 
  validates_primitive_type_of :name1 

end

Types

Here is a list of all the types available from DataMapper, which ones we support, and custom aliases.

DataMapper Property Type

DMap Command/Aliases

Boolean

boolean, bool

String

string, str, s

Text

text, txt

Float

float, f

Integer

integer, int, i

Decimal

decimal, dec

DateTime

datetime, dt

Date

date, d

Time

time, t

Object

object, obj

Discriminator

discriminator, disc

Binary

binary, blob, b

More coming soon!

Property Validators

This part belongs in the third section of the command for example:

dmap new user id name:str:<b>required,accessor=private</b>

Each validator is separated by a comma (,) and can have a value placed within it by an equal sign (=). The property name will look like…

property :name, String, :required => true, :accessor => :private

Property Validator List

DataMapper’s List

DMap Command

required

required

default

default

key

key

lazy

lazy

accessor

accessor

writer

writer

reader

reader

That’s all the ones I know for now, let me know if there are more!

Validations

A list of all of the validates_*_of commands and which DMap currently supports

Note: DMap currently does not support :allow_nil parameters. This will change in the future.

DataMapper’s List

DMap Command

validates_absence_of

absence, absent

validates_acceptance_of

acceptance, accept

validates_with_block

Unsupported at this time

validates_confirmation_of

confirmation, confirm

validates_format_of

format

validates_length_of

length_of

validates_with_method

withmethod, method

validates_numericality_of

Unsupported

validates_primitive_type_of

primitive

validates_presence_of

presence, present

validates_uniqueness_of

uniqueness

validates_within

within

Associations

DataMapper Command

DMap Command

has 1

has1, hasone

has n

hasn

belongs_to

belongs_to, belongs

has_and_belongs_to_many

hasmany

Note: use hasn when wanting to use :through => :model and hasmany for wanting to :through => Resource

Also, conditions are not supported through this generator (suggestions on this could be implemented would be helpful).

To Do

  • Add a way to add/edit/remove columns from pre-existing tables

  • All of the misc. property types

  • Template system similar to Sinatra::Fedora’s hatrack option

  • Better documentation!

  • Migrations! (This will most likely be started after DM core team has finished the Veritas system).

  • Conditions with associations?