0.0
No commit activity in last 3 years
No release in over 3 years
Data mapper using ActiveRecord for data access
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.5
>= 0
>= 0

Runtime

 Project Readme

ActiveMapper

Code Climate

ActiveMapper is a data-mapper using ActiveRecord/Arel as a backend. It allows you to create models using PORO and comes with a Memory adapter which you can use during testing to make your tests faster and not reliant on a database. The only requirements are that your models have an id attribute (auto-incrementing int) and respond to a valid? and .model_name ( ActiveSupport::Name). These can easily be injected into your models by including ActiveModel::Model.

Installation

Add this line to your application's Gemfile:

gem 'active_mapper'

And then execute:

$ bundle

Or install it yourself as:

$ gem install active_mapper

Usage

class Post
    include ActiveModel::Model
    
    attr_accessor :id, :title, :content, :status, :created_at, :updated_at
    
    def persisted?
        id
    end
end

post = Post.new(title: 'Post', content: 'My First Post')

# ActiveMapper[Class] will automatically create and wire up the mapper
# if it does not exist. The only requirement is that the table exists
# with the proper columns (e.g. use ActiveRecord migrations).
ActiveMapper[Post].save(post)

record = ActiveMapper[Post].find(post.id)

Querying

mapper = ActiveMapper[Post]

# some methods return an ActiveMapper::Relation which will not execute
# any queries until #to_a, #each or #map are called.

mapper.select { |post| post.title.starts_with('Post') }
mapper.reject { |post| post.status == 'draft' }

# some methods actually execute the query immediately

mapper.first { |post| !(post.status == 'draft') }
mapper.last { |post| (post.status == 'published') & (post.created_at > 5.days.ago) }
mapper.all { |post| (post.content.contains('monkey')) & (post.status == 'published) }

Contributing

  1. Fork it ( http://github.com//active_mapper/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request