Skip to content

A simple yet powerful object relational mapper.

Notifications You must be signed in to change notification settings

digitalfrost/rmap

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

59 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Rmap - a simple yet powerfull object relational mapper

##Installation

gem install rmap

or add the following to your gem file:

gem 'rmap'

and then run bundle install from your shell.

##Basic configuration

require 'rmap'

db = Rmap::Database.new :database => 'rmap', :username => 'root'

You can get more configuration options by going to the mysql2 gem documentation, as rmap wraps the mysql2 gem and simply passes through the conf hash to it:

For instance a more advanced configuration might be:

require 'rmap'

db = Rmap::Database.new :database => 'rmap', :host => 'localhost', :username => 'root', :password => "secret"

How to use

The following will return a representation of the 'posts' table:

db.posts

Creating

You can insert rows into the posts table by doing the following:

db.posts.insert(:title => "Hello World", :body => "This is a test")

Retrieval

You can list all the posts by doing the following:

db.posts.all.each do |post|
  puts "title #{post.title}"
end

You can list all the posts that contain the word apple by doing the following:

db.posts.contains(:body, "apple").all.each do |post|
  puts "title #{post.title}"
end

You can list all the posts that contain the word apple or pear by doing the following:

db.posts.contains(:body, ["apple", "pear"]).all.each do |post|
  puts "title #{post.title}"
end

You can retrieve a particular post (row) by doing the following:

db.posts.eq(:id, 7).first

and then you can print the title to the screen:

puts db.posts.eq(:id, 7).first.title

Joins

Joins are really easy. To retrieve all the posts by gmail users, you can do the following:

db.users.contains(:email, "@gmail.com").posts.all

Deleting

You can delete all the posts by gmail users by doing the following:

db.users.contains(:email, "@gmail.com").posts.delete

Updating

You can update all the posts by gmail users by doing the following:

db.users.contains(:email, "@gmail.com").posts.update(:published => true, :last_published => Time.now)

or if you just want to update a column:

db.users.contains(:email, "@gmail.com").posts.published = true

more coming soon....

License

Rmap is released under the MIT license:

About

A simple yet powerful object relational mapper.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages