ActiveRedis
Small childly ORM for Redis
Installation
Add this line to your application's Gemfile:
gem 'active_redis', '0.0.9'
And then execute:
$ bundle
Or install it yourself as:
$ gem install active_redis
Usage
Basic usage
In model you may add attributes with next types: string, integer, time
class Article < ActiveRedis::Base
attributes title: :string, link: :string
end
By class method attributes you must define own attributes.
Attribute accessing
By using accessible methods
article = Article.new
article.title = "Some title"
article.link = "http://someblog.com/1"
p article.title
p article.link
Or by attributes method
article = Article.new
article.attributes = {title: "Some title", link: "http://someblog.com/1"}
p article.attributes
p article.title
p article.link
Persistence
ActiveRedis::Base inherited model respond to all CRUD method such as create, update, destroy and save
article = Article.new(title: "Some title", link: "http://someblog.com/1")
article.save
another_article = Article.create(title: "Another title", link: "http://someblog.com/2")
article.update(title: "New article title")
p article.title
another_article.destroy
Article.destroy_all
p Article.count
Associations (from 0.0.4)
has_one association
has_many association
belongs_to association
Finders
You may find 'row' by it's id
Article.find(1)
Also gem add support for some aggregation functions like sum, min, max, pluck
class Article < ActiveRedis::Base
attributes link: :string, title: :string, views: :integer
end
Article.create(views: 1000, link: "http://someblog.com/1", title: "Title article")
Article.create(views: 3000, link: "http://someblog.com/2", title: "Title article")
From version 0.0.2 you are able to search item by multiple attributes using method where
Article.where(title: "Title article", views: 1000)
Generators
In version 0.0.4 is implemented model generator
rails g active_redis:model ModelName attribute1 attribute2
For example:
Sergeys-MacBook-Pro-2:test_active_redis sergey$ rails g active_redis:model User name:string city:string
create app/models/user.rb
The result is app/models/user.rb with stub content
class User < ActiveRedis::Base
attributes name: :string, city: :string
end
NEW IN VERSION 0.0.7
Method chaining. Now you may calling where, order, limit something like this:
Article.where(title: "Article title").where(views: 1000).order(title: :asc).limit(per_page: 20, page: 3)
NEW IN VERSION 0.0.8
Using aggregation functions with method chaining functional.
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request