Properties for a model are not derived from a database structure, but instead explicitly declared inside your model class definitions. These properties then map (or, if using automigrate, generate) fields in your repository/database.
If you are coming to DataMapper from another ORM framework, such as ActiveRecord, this may be a fundamental difference in thinking to you. However, there are several advantages to defining your properties in your models:
Inside your class, you call the property method for each property you want to add. The only two required arguments are the name and type, everything else is optional.
class Post include DataMapper::Resource property :title, String, :required => true # Cannot be null property :publish, Boolean, :default => false # Default value for new records is false end
By default, DataMapper supports the following primitive (Ruby) types also called core properties:
Property access control is uses the same terminology Ruby does. Properties are public by default, but can also be declared private or protected as needed (via the :accessor option).
class Post include DataMapper::Resource property :title, String, :accessor => :private # Both reader and writer are private property :body, Text, :accessor => :protected # Both reader and writer are protected end
Access control is also analogous to Ruby attribute readers and writers, and can be declared using :reader and :writer, in addition to :accessor.
class Post include DataMapper::Resource property :title, String, :writer => :private # Only writer is private property :tags, String, :reader => :protected # Only reader is protected end
The reader/writer for any property can be overridden in the same manner that Ruby attr readers/writers can be. After the property is defined, just add your custom reader or writer:
class Post include DataMapper::Resource property :title, String def title=(new_title) raise ArgumentError if new_title != 'Lee is l337' super(new_title) end end
Calling super ensures that any validators defined for the property are kept active.
By default, some properties are not loaded when an object is fetched in DataMapper. These lazily loaded properties are fetched on demand when their accessor is called for the first time (as it is often unnecessary to instantiate -every- property -every- time an object is loaded). For instance, DataMapper::Property::Text fields are lazy loading by default, although you can over-ride this behavior if you wish:
Example:
class Post include DataMapper::Resource property :title, String # Loads normally property :body, Text # Is lazily loaded by default end
If you want to over-ride the lazy loading on any field you can set it to a context or false to disable it with the :lazy option. Contexts allow multiple lazy properties to be loaded at one time. If you set :lazy to true, it is placed in the :default context
class Post include DataMapper::Resource property :title, String # Loads normally property :body, Text, :lazy => false # The default is now over-ridden property :comment, String, :lazy => [ :detailed ] # Loads in the :detailed context property :author, String, :lazy => [ :summary, :detailed ] # Loads in :summary & :detailed context end
Delaying the request for lazy-loaded attributes even applies to objects accessed through associations. In a sense, DataMapper anticipates that you will likely be iterating over objects in associations and rolls all of the load commands for lazy-loaded properties into one request from the database.
Example:
Widget.get(1).components # loads when the post object is pulled from database, by default Widget.get(1).components.first.body # loads the values for the body property on all objects in the # association, rather than just this one. Widget.get(1).components.first.comment # loads both comment and author for all objects in the association # since they are both in the :detailed context
Properties can be declared as primary or natural keys on a table. You should a property as the primary key of the table:
Examples:
property :id, Serial # auto-incrementing key property :legacy_pk, String, :key => true # 'natural' key
This is roughly equivalent to ActiveRecord‘s set_primary_key, though non-integer data types may be used, thus DataMapper supports natural keys. When a property is declared as a natural key, accessing the object using the indexer syntax Class[key] remains valid.
User.get(1) # when :id is the primary key on the users table User.get('bill') # when :name is the primary (natural) key on the users table
You can add indices for your properties by using the :index option. If you use true as the option value, the index will be automatically named. If you want to name the index yourself, use a symbol as the value.
property :last_name, String, :index => true property :first_name, String, :index => :name
You can create multi-column composite indices by using the same symbol in all the columns belonging to the index. The columns will appear in the index in the order they are declared.
property :last_name, String, :index => :name property :first_name, String, :index => :name # => index on (last_name, first_name)
If you want to make the indices unique, use :unique_index instead of :index
If you require the dm-validations plugin, auto-validations will automatically be mixed-in in to your model classes: validation rules that are inferred when properties are declared with specific column restrictions.
class Post include DataMapper::Resource property :title, String, :length => 250, :min => 0, :max => 250 # => infers 'validates_length :title' property :title, String, :required => true # => infers 'validates_present :title' property :email, String, :format => :email_address # => infers 'validates_format :email, :with => :email_address' property :title, String, :length => 255, :required => true # => infers both 'validates_length' as well as 'validates_present' # better: property :title, String, :length => 1..255 end
This functionality is available with the dm-validations gem. For more information about validations, check the documentation for dm-validations.
To set a default for a property, use the :default key. The property will be set to the value associated with that key the first time it is accessed, or when the resource is saved if it hasn‘t been set with another value already. This value can be a static value, such as ‘hello’ but it can also be a proc that will be evaluated when the property is read before its value has been set. The property is set to the return of the proc. The proc is passed two values, the resource the property is being set for and the property itself.
property :display_name, String, :default => lambda { |resource, property| resource.login }
Word of warning. Don‘t try to read the value of the property you‘re setting the default for in the proc. An infinite loop will ensue.
As an alternative to extraneous has_one relationships, consider using an EmbeddedValue.
:accessor if false, neither reader nor writer methods are created for this property :reader if false, reader method is not created for this property :writer if false, writer method is not created for this property :lazy if true, property value is only loaded when on first read if false, property value is always loaded if a symbol, property value is loaded with other properties in the same group :default default value of this property :allow_nil if true, property may have a nil value on save :key name of the key associated with this property. :field field in the data-store which the property corresponds to :length string field length :format format for autovalidation. Use with dm-validations plugin. :index if true, index is created for the property. If a Symbol, index is named after Symbol value instead of being based on property name. :unique_index true specifies that index on this property should be unique :auto_validation if true, automatic validation is performed on the property :validates validation context. Use together with dm-validations. :unique if true, property column is unique. Properties of type Serial are unique by default. :precision Indicates the number of significant digits. Usually only makes sense for float type properties. Must be >= scale option value. Default is 10. :scale The number of significant digits to the right of the decimal point. Only makes sense for float type properties. Must be > 0. Default is nil for Float type and 10 for BigDecimal
There is the ability to reconfigure a Property and it‘s subclasses by explicitly setting a value in the Property, eg:
# set all String properties to have a default length of 255 DataMapper::Property::String.length(255) # set all Boolean properties to not allow nil (force true or false) DataMapper::Property::Boolean.allow_nil(false) # set all properties to be required by default DataMapper::Property.required(true) # turn off auto-validation for all properties by default DataMapper::Property.auto_validation(false) # set all mutator methods to be private by default DataMapper::Property.writer(:private)
Please note that this has no effect when a subclass has explicitly defined it‘s own option. For example, setting the String length to 255 will not affect the Text property even though it inherits from String, because it sets it‘s own default length to 65535.
PRIMITIVES | = | [ TrueClass, ::String, ::Float, ::Integer, ::BigDecimal, ::DateTime, ::Date, ::Time, ::Class | ||
OPTIONS | = | [ :accessor, :reader, :writer, :lazy, :default, :key, :field, :index, :unique_index, :unique, :allow_nil, :allow_blank, :required | ||
VISIBILITY_OPTIONS | = | [ :public, :protected, :private ].to_set.freeze | Possible :visibility option values | |
INVALID_NAMES | = | (Resource.instance_methods + Resource.private_instance_methods + Query::OPTIONS.to_a | Invalid property names |
allow_blank | [R] | |
allow_nil | [R] | |
default | [R] | |
index | [R] |
Returns index name if property has index.
@return [Boolean, Symbol, Array] returns true if property is indexed by itself returns a Symbol if the property is indexed with other properties returns an Array if the property belongs to multiple indexes returns false if the property does not belong to any indexes @api public |
instance_variable_name | [R] | |
model | [R] | |
name | [R] | |
options | [R] | |
primitive | [R] | |
reader_visibility | [R] | |
repository_name | [R] | |
required | [R] | |
unique_index | [R] |
Returns true if property has unique index. Serial properties and keys are unique by default.
@return [Boolean, Symbol, Array] returns true if property is indexed by itself returns a Symbol if the property is indexed with other properties returns an Array if the property belongs to multiple indexes returns false if the property does not belong to any indexes @api public |
writer_visibility | [R] |
Returns whether or not the property can be a blank value
@return [Boolean]
whether or not the property can be blank
@api public
Returns whether or not the property can accept ‘nil’ as it‘s value
@return [Boolean]
whether or not the property can accept 'nil'
@api public
A hook to allow properties to extend or modify the model it‘s bound to. Implementations are not supposed to modify the state of the property class, and should produce no side-effects on the property instance.
Returns a concise string representation of the property instance.
@return [String]
Concise string representation of the property instance.
@api public
Returns whether or not the property is a key or a part of a key
@return [Boolean]
true if the property is a key or a part of a key
@api public
Returns whether or not the property is to be lazy-loaded
@return [Boolean]
true if the property is to be lazy-loaded
@api public
Returns whether or not the property must be non-nil and non-blank
@return [Boolean]
whether or not the property is required
@api public
Returns whether or not the property is "serial" (auto-incrementing)
@return [Boolean]
whether or not the property is "serial"
@api public
Provides a standardized setter method for the property
@param [Resource] resource
the resource to get the value from
@param [Object] value
the value to set in the resource
@return [Object]
+value+ after being typecasted according to this property's primitive
@raise [ArgumentError] "resource should be a Resource, but was .…"
@api private
Returns true if property is unique. Serial properties and keys are unique by default.
@return [Boolean]
true if property has uniq index defined, false otherwise
@api public
Assert given visibility value is supported.
Will raise ArgumentError if this Property‘s reader and writer visibilities are not included in VISIBILITY_OPTIONS.
@return [undefined]
@raise [ArgumentError] "property visibility must be :public, :protected, or :private"
@api private