Compose, decouple and manage domain logic and data persistence separately. Works particularly great for composing form objects!
Compose, decouple and manage domain logic and data persistence separately. Works particularly great for composing form objects!
Datamappify is no longer being maintained. It started off with a noble goal, unfortunately due to it being on the critical path of our project, we have decided not to continue developing it given the lack of development time from me.
Feel free to read the README and browse the code, I still believe in the solutions for this particular domain.
For a more active albeit still young project, check out Lotus::Model.
The typical Rails (and ActiveRecord) way of building applications is great for small to medium sized projects, but when projects grow larger and more complex, your models too become larger and more complex - it is not uncommon to have god classes such as a User model.
Datamappify tries to solve two common problems in web applications:
Datamappify is loosely based on the Repository Pattern and Entity Aggregation, and is built on top of Virtus and existing ORMs (ActiveRecord and Sequel, etc).
There are three main design goals:
UserAccount ActiveRecord model that is attached to a User entity managed by Datamappify.UserRepository.save(user), certain attributes of the user entity are now persisted on a remote web service. Better yet, dirty tracking and lazy loading are supported out of the box!Datamappify consists of three components:
find, save and destroy, etc.Below is a high level and somewhat simplified overview of Datamappify's architecture.
Note: Datamappify is NOT affiliated with the Datamapper project.
You may implement your own data provider and criteria, but Datamappify comes with build-in support for the following ORMS:
Add this line to your application's Gemfile:
gem 'datamappify'Entity uses Virtus DSL for defining attributes and ActiveModel::Validations DSL for validations.
The cool thing about Virtus is that all your attributes get coercion for free!
Below is an example of a User entity, with inline comments on how some of the DSLs work.
…Inheritance is supported for entities, for example:
…Inheritance is supported for repositories when your data structure is based on STI (Single Table Inheritance), for example:
class AdminUserRepository 'User#expiry_date'
endIn the above example, both repositories deal with the ActiveRecord::User data model.
Datamappify repository by default creates the underlying data model classes for you. For example:
map_attribute :driver_license, :to => 'UserData::DriverLicense#number'In the above example, a Datamppify::Data::Record::ActiveRecord::UserDriverLicense ActiveRecord model will be created. If you would like to customise the data model class, you may do so by creating one either under the default namespace or under the Datamappify::Data::Record::NameOfDataProvider namespace:
module UserData
class DriverLicense 'Fred', :driver_license => 'AABBCCDD')users = UserRepository.match(:first_name => 'Fre%', :driver_license => '%bbcc%')You may compose search criteria via the criteria method.
users = UserRepository.criteria(
:where => {
:first_name => 'Fred'
},
:order => {
:last_name => :asc
},
:limit => [10, 20]
)Currently implemented criteria options:
Note: it does not currently support searching attributes from different data providers.
Accepts an entity.
There is also save! that raises Datamappify::Data::EntityNotSaved.
UserRepository.save(user)Datamappify supports attribute dirty tracking - only dirty attributes will be saved.
Sometimes it's useful to manually mark the whole entity, or some attributes in the entity to be dirty. In this case, you could:
UserRepository.states.mark_as_dirty(user) # marks the whole entity as dirty
UserRepository.states.find(user).changed? #=> true
UserRepository.states.find(user).first_name_changed? #=> true
UserRepository.states.find(user).last_name_changed? #=> true
UserRepository.states.find(user).age_changed? #=> trueOr:
UserRepository.states.mark_as_dirty(user, :first_name, :last_name) # marks only first_name and last_name as dirty
UserRepository.states.find(user).changed? #=> true
UserRepository.states.find(user).first_name_changed? #=> true
UserRepository.states.find(user).last_name_changed? #=> true
UserRepository.states.find(user).age_changed? #=> falseAccepts an entity.
There is also destroy! that raises Datamappify::Data::EntityNotDestroyed.
Note that due to the attributes mapping, any data found in mapped records are not touched. For example the corresponding ActiveRecord::User record will be destroyed, but ActiveRecord::Hobby that is associated will not.
UserRepository.destroy(user)Accepts an entity class and returns a new entity.
This is useful for using before_init and after_init callbacks to set up the entity.
UserRepository.init(user_class) #=> userDatamappify supports the following callbacks via Hooks:
Callbacks are defined in repositories, and they have access to the entity. For example:
class UserRepository
include Datamappify::Repository
before_create :make_me_admin
before_create :make_me_awesome
after_save :make_me_smile
private
def make_me_admin(entity)
# ...
end
def make_me_awesome(entity)
# ...
end
def make_me_smile(entity)
# ...
end
# ...
endNote: Returning either nil or false from the callback will cancel all subsequent callbacks (and the action itself, if it's a before_ callback).
Datamappify also supports entity association. It is experimental and it currently supports the following association types:
Set up your entities and repositories:
…Usage examples:
new_post = Post.new(post_attributes)
another_new_post = Post.new(post_attributes)
user = UserRepository.find(1)
user.title = Title.new(title_attributes)
user.posts = [new_post, another_new_post]
persisted_user = UserRepository.save!(user)
persisted_user.title #=> associated title
persisted_user.posts #=> an array of associated postsLike ActiveRecord and ActionView, Datamappify also supports nested attributes via fields_for or simple_fields_for.
# slim template
= simple_form_for @post do |f|
= f.input :title
= f.input :body
= f.simple_fields_for :comment do |fp|
= fp.input :author_name
= fp.input :comment_bodyYou may configure Datamappify's default behaviour. In Rails you would put it in an initializer file.
Datamappify.config do |c|
c.default_provider = :ActiveRecord
endDatamappify ships with a few extensions to make certain tasks easier.
Use Criteria with page and per.
UserRepository.criteria(
:where => {
:gender => 'male',
:age => 42
},
:page => 1,
:per => 10
)You may check out this article for more examples.
Refer to CHANGELOG.
Licensed under MIT
No open issues yet, or sync has not completed.