active_redis 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +97 -0
- data/Rakefile +2 -0
- data/active_redis.gemspec +26 -0
- data/lib/active_redis.rb +24 -0
- data/lib/active_redis/adapters/basic_adapter.rb +19 -0
- data/lib/active_redis/associations.rb +27 -0
- data/lib/active_redis/attributes.rb +41 -0
- data/lib/active_redis/base.rb +21 -0
- data/lib/active_redis/calculations.rb +17 -0
- data/lib/active_redis/config.rb +14 -0
- data/lib/active_redis/connection.rb +22 -0
- data/lib/active_redis/connection_ext/calculations_layer.rb +24 -0
- data/lib/active_redis/connection_ext/finders_layer.rb +14 -0
- data/lib/active_redis/connection_ext/persistence_layer.rb +35 -0
- data/lib/active_redis/constants.rb +11 -0
- data/lib/active_redis/errors.rb +12 -0
- data/lib/active_redis/finders.rb +11 -0
- data/lib/active_redis/helpers/lua_scripts.rb +24 -0
- data/lib/active_redis/lua_scripts/main.lua +50 -0
- data/lib/active_redis/naming.rb +19 -0
- data/lib/active_redis/persistence.rb +55 -0
- data/lib/active_redis/railtie.rb +12 -0
- data/lib/active_redis/version.rb +3 -0
- metadata +126 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
Mzc0OWM3NzZhZmM1ZTViNWU1NmJlNDI3MzAyM2QyMDEzZDA1MmUzZg==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
OWM3MjQwNTU2NDFjZDBmMzg5NWY5NjRjZGYyYmEwMGQzMDhlZTJhMA==
|
7
|
+
!binary "U0hBNTEy":
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
ZmQzNzNlZjQzZTMxMDA0MmI2YjA3MWQzNWUzNDdjY2UwZDA5MWFkNzQ3MzI3
|
10
|
+
MzUwYTMyZjEyMmY5YTU2YzJjN2NiZjM4ZGRhODc0NTY2MWY3MTcyMDViYjI2
|
11
|
+
YzhmZmZhMGRmZjU0NDBlZWFiYmE3ZjVmOWQyZDZhMmI3OTI0MDI=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
ZmIyNTYzNjE0OTY0ZmIzZTkwOGY1OGU3MTcyYmIyNTk5NjY0M2UxNjM0YmQ4
|
14
|
+
OTU3OWUwNDQwYTEyNTk5OTg0ODhjOWY5YzlkYjJmMDk4OTdmZWQ2ZDgwZWVl
|
15
|
+
M2E5ODUxMzExYzEwMzViZTFmMmY5YjIwMDNkODU0ODllMThkMjY=
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 sergio1990
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
# ActiveRedis
|
2
|
+
|
3
|
+
Small childly ORM for Redis
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'active_redis'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install active_redis
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
### Basic usage
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
class Article < ActiveRedis::Base
|
25
|
+
attributes :title, :link
|
26
|
+
end
|
27
|
+
```
|
28
|
+
|
29
|
+
By class method __attributes__ you must define own attributes.
|
30
|
+
|
31
|
+
### Attribute accessing
|
32
|
+
|
33
|
+
By using accessible methods
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
article = Article.new
|
37
|
+
article.title = "Some title"
|
38
|
+
article.link = "http://someblog.com/1"
|
39
|
+
|
40
|
+
p article.title # => Some title
|
41
|
+
p article.link # => http://someblog.com/1
|
42
|
+
```
|
43
|
+
|
44
|
+
Or by __attributes__ method
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
article = Article.new
|
48
|
+
article.attributes = {title: "Some title", link: "http://someblog.com/1"}
|
49
|
+
|
50
|
+
p article.attributes # => {title: "Some title", link: "http://someblog.com/1"}
|
51
|
+
p article.title # => Some title
|
52
|
+
p article.link # => http://someblog.com/1
|
53
|
+
```
|
54
|
+
|
55
|
+
### Persistence
|
56
|
+
|
57
|
+
ActiveRedis::Base inherited model respond to all CRUD method such as create, update, destroy and save
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
article = Article.new(title: "Some title", link: "http://someblog.com/1")
|
61
|
+
article.save # => true
|
62
|
+
|
63
|
+
another_article = Article.create(title: "Another title", link: "http://someblog.com/2") # => true
|
64
|
+
|
65
|
+
article.update(title: "New article title") # => true
|
66
|
+
|
67
|
+
p article.title # => New article title
|
68
|
+
|
69
|
+
another_article.destroy
|
70
|
+
```
|
71
|
+
|
72
|
+
### Finders and Calculations
|
73
|
+
|
74
|
+
Now you may find 'row' by it's id
|
75
|
+
|
76
|
+
```ruby
|
77
|
+
Article.find(1) # => [#<Article:0x007fec2526f4f8 @updated_at="1382608780", @link="http://someblog.com/1", @id="1", @title="Some title", @created_at="1382608780">]
|
78
|
+
```
|
79
|
+
|
80
|
+
Now gem add support for some aggregation functions like __sum__, __min__, __max__, __pluck__.
|
81
|
+
|
82
|
+
### Future work
|
83
|
+
|
84
|
+
At an early time I want to implement such features:
|
85
|
+
|
86
|
+
1. Add finders like ActiveRecord's where
|
87
|
+
2. Add _all_ class method
|
88
|
+
3. Add associations
|
89
|
+
4. Setting/getting attributes with it's types
|
90
|
+
|
91
|
+
## Contributing
|
92
|
+
|
93
|
+
1. Fork it
|
94
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
95
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
96
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
97
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'active_redis/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "active_redis"
|
8
|
+
spec.version = ActiveRedis::VERSION
|
9
|
+
spec.authors = ["Sergey Gernyak"]
|
10
|
+
spec.email = ["sergeg1990@gmail.com"]
|
11
|
+
spec.description = %q{Small childly ORM for Redis}
|
12
|
+
spec.summary = spec.description
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "redis"
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
24
|
+
spec.add_development_dependency "rake"
|
25
|
+
spec.add_development_dependency "rake-notes"
|
26
|
+
end
|
data/lib/active_redis.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require "active_redis/version"
|
2
|
+
require "active_redis/errors"
|
3
|
+
require "active_redis/railtie" if defined?(Rails)
|
4
|
+
|
5
|
+
module ActiveRedis
|
6
|
+
autoload :Constants, 'active_redis/constants'
|
7
|
+
autoload :Config, 'active_redis/config'
|
8
|
+
autoload :Base, 'active_redis/base'
|
9
|
+
autoload :Connection, 'active_redis/connection'
|
10
|
+
|
11
|
+
def self.config(&block)
|
12
|
+
@config ||= ::ActiveRedis::Config.new
|
13
|
+
block_given? ? yield(@config) : @config
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.connection
|
17
|
+
raise ActiveRedis::NoConnectionError, "Connection not provided!" unless @connection
|
18
|
+
@connection
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.connection=(value)
|
22
|
+
@connection = value
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'delegate'
|
2
|
+
require 'redis'
|
3
|
+
|
4
|
+
module ActiveRedis
|
5
|
+
module Adapters
|
6
|
+
|
7
|
+
class BasicAdapter < SimpleDelegator
|
8
|
+
|
9
|
+
alias_method :adapter, :__getobj__
|
10
|
+
|
11
|
+
def initialize(options)
|
12
|
+
redis = Redis.new(options)
|
13
|
+
super(redis)
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module ActiveRedis
|
2
|
+
module Associations
|
3
|
+
|
4
|
+
def self.included(base)
|
5
|
+
base.extend ClassMethods
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
|
10
|
+
# TODO: add has_one functionality
|
11
|
+
def has_one
|
12
|
+
|
13
|
+
end
|
14
|
+
|
15
|
+
# TODO: add has_many functionality
|
16
|
+
def has_many
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
# TODO: add belongs_to functionality
|
21
|
+
def belongs_to
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module ActiveRedis
|
2
|
+
module Attributes
|
3
|
+
|
4
|
+
def self.included(base)
|
5
|
+
base.extend ClassMethods
|
6
|
+
end
|
7
|
+
|
8
|
+
def attributes=(value)
|
9
|
+
raise ActiveRedis::InvalidArgumentError, "Value must be a Hash" unless value.is_a? Hash
|
10
|
+
value.each { |attribute, value| self.send("#{attribute}=", value) }
|
11
|
+
end
|
12
|
+
|
13
|
+
def attributes
|
14
|
+
self.class.defined_attributes.inject({}) do |hash, attribute|
|
15
|
+
hash[attribute.to_sym] = self.send(attribute.to_s)
|
16
|
+
hash
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
module ClassMethods
|
21
|
+
|
22
|
+
def attributes(*attrs)
|
23
|
+
attrs = attrs.concat(ActiveRedis::Constants::DEFAULT_ATTRIBUTES)
|
24
|
+
attrs.each do |attribute|
|
25
|
+
define_method "#{attribute}=" do |value|
|
26
|
+
instance_variable_set("@#{attribute}", value)
|
27
|
+
end
|
28
|
+
define_method attribute do
|
29
|
+
instance_variable_get("@#{attribute}")
|
30
|
+
end
|
31
|
+
end
|
32
|
+
class << self
|
33
|
+
attr_accessor :defined_attributes
|
34
|
+
end
|
35
|
+
self.defined_attributes = attrs
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'active_redis/attributes'
|
2
|
+
require 'active_redis/associations'
|
3
|
+
require 'active_redis/persistence'
|
4
|
+
require 'active_redis/naming'
|
5
|
+
require 'active_redis/calculations'
|
6
|
+
require 'active_redis/finders'
|
7
|
+
|
8
|
+
# TODO: Add Expiring module
|
9
|
+
|
10
|
+
module ActiveRedis
|
11
|
+
class Base
|
12
|
+
extend Naming
|
13
|
+
extend Calculations
|
14
|
+
extend Finders
|
15
|
+
|
16
|
+
include Attributes
|
17
|
+
include Associations
|
18
|
+
include Persistence
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module ActiveRedis
|
2
|
+
module Calculations
|
3
|
+
|
4
|
+
def self.extended(base)
|
5
|
+
ActiveRedis::Constants::CALCULATION_METHODS.each do |method|
|
6
|
+
base.instance_eval <<-EVAL
|
7
|
+
def #{method}(attribute = "")
|
8
|
+
ActiveRedis.connection.calculate_#{method} self, attribute
|
9
|
+
end
|
10
|
+
EVAL
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
# TODO: add average method
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
class ActiveRedis::Config
|
2
|
+
# Specify connection string to Redis. To check all available options please see redis-rb library.
|
3
|
+
#
|
4
|
+
# Some samples:
|
5
|
+
# ActiveRedis.config.connection_options = {host: 10.10.1.1, port: 6380}
|
6
|
+
# or
|
7
|
+
# ActiveRedis.config.connection_options = {path: '/tmp/redis.sock'}
|
8
|
+
attr_accessor :connection_options
|
9
|
+
|
10
|
+
# Specify adapter for connection to DB
|
11
|
+
# By default it's ActiveRedis::Adapters::BasicAdapter
|
12
|
+
attr_accessor :adapter
|
13
|
+
|
14
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'active_redis/helpers/lua_scripts'
|
2
|
+
Dir[File.dirname(__FILE__) + '/connection_ext/*.rb'].each {|file| require file }
|
3
|
+
|
4
|
+
include ActiveRedis::Helpers::LuaScripts
|
5
|
+
|
6
|
+
module ActiveRedis
|
7
|
+
class Connection
|
8
|
+
extend ActiveRedis::ConnectionExt::CalculationsLayer
|
9
|
+
|
10
|
+
include ActiveRedis::ConnectionExt::FindersLayer
|
11
|
+
include ActiveRedis::ConnectionExt::PersistenceLayer
|
12
|
+
|
13
|
+
calculations ActiveRedis::Constants::CALCULATION_METHODS
|
14
|
+
|
15
|
+
def initialize(options, adapter)
|
16
|
+
@adapter = adapter.new(options)
|
17
|
+
end
|
18
|
+
|
19
|
+
attr_accessor :adapter
|
20
|
+
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module ActiveRedis::ConnectionExt
|
2
|
+
module CalculationsLayer
|
3
|
+
|
4
|
+
def self.extended(base)
|
5
|
+
base.extend ClassMethods
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
|
10
|
+
def calculations(methods)
|
11
|
+
eval_string = ""
|
12
|
+
methods.each do |method|
|
13
|
+
eval_string += <<-EVAL
|
14
|
+
def calculate_#{method}(model, attributes = "")
|
15
|
+
adapter.eval send("#{method}_script"), keys: [model.key_name], argv: [attributes]
|
16
|
+
end
|
17
|
+
EVAL
|
18
|
+
end
|
19
|
+
class_eval(eval_string)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module ActiveRedis::ConnectionExt
|
2
|
+
module FindersLayer
|
3
|
+
|
4
|
+
def fetch_row(model, id)
|
5
|
+
raise ActiveRedis::NotSpecifiedIdError, "Must specified ID for finding record!" unless id
|
6
|
+
adapter.hgetall model.table_name(id)
|
7
|
+
end
|
8
|
+
|
9
|
+
def fetch_keys(model)
|
10
|
+
adapter.keys model.key_name
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module ActiveRedis::ConnectionExt
|
2
|
+
module PersistenceLayer
|
3
|
+
|
4
|
+
def save_table(model, attributes)
|
5
|
+
raise ActiveRedis::NotSpecifiedIdError, "Must specified ID for saving record!" if !attributes || !attributes[:id]
|
6
|
+
adapter.hmset(model.table_name(attributes[:id]), attributes.flatten) == ActiveRedis::Constants::SAVE_SUCCESS_ANSWER
|
7
|
+
end
|
8
|
+
|
9
|
+
def next_id(model)
|
10
|
+
table = model.info_table_name
|
11
|
+
create_info_table(model) unless @adapter.exists(table)
|
12
|
+
adapter.hincrby table, "next_id", 1
|
13
|
+
adapter.hget table, "next_id"
|
14
|
+
end
|
15
|
+
|
16
|
+
def create_info_table(model)
|
17
|
+
adapter.hmset model.info_table_name, "next_id", 0
|
18
|
+
end
|
19
|
+
|
20
|
+
def destroy(model, id)
|
21
|
+
raise ActiveRedis::NotSpecifiedIdError, "Must specified ID for destroy record!" unless id
|
22
|
+
destroy_by_keys model.table_name(id)
|
23
|
+
end
|
24
|
+
|
25
|
+
def destroy_all(model)
|
26
|
+
destroy_by_keys fetch_keys(model)
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
def destroy_by_keys(keys)
|
31
|
+
adapter.del keys
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module ActiveRedis
|
2
|
+
module Helpers
|
3
|
+
module LuaScripts
|
4
|
+
|
5
|
+
ActiveRedis::Constants::CALCULATION_METHODS.each do |method|
|
6
|
+
define_method "#{method}_script" do
|
7
|
+
<<-LUA
|
8
|
+
#{LuaLoader.get_main}
|
9
|
+
return calculate_#{method}(KEYS[1]#{method == "count" ? "" : ", ARGV[1]"})
|
10
|
+
LUA
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class LuaLoader
|
15
|
+
|
16
|
+
def self.get_main
|
17
|
+
@content ||= File.read(File.dirname(__FILE__) + '/../lua_scripts/main.lua')
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
local keys = function (k)
|
2
|
+
return redis.call("KEYS", k)
|
3
|
+
end
|
4
|
+
|
5
|
+
local cond_tonum = function (value, tonum)
|
6
|
+
if tonum == true then
|
7
|
+
return tonumber(value)
|
8
|
+
else
|
9
|
+
return value
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
local getHashValues = function (k, attr, tonum)
|
14
|
+
local result = {}
|
15
|
+
local ks = keys(k)
|
16
|
+
for index, key in pairs(ks) do
|
17
|
+
local cell = redis.call("HGET", key, attr)
|
18
|
+
table.insert(result, cond_tonum(cell, tonum))
|
19
|
+
end
|
20
|
+
return result
|
21
|
+
end
|
22
|
+
|
23
|
+
local calculate_count = function (key)
|
24
|
+
return #keys(key)
|
25
|
+
end
|
26
|
+
|
27
|
+
local calculate_pluck = function (key, attr)
|
28
|
+
return getHashValues(key, attr, false)
|
29
|
+
end
|
30
|
+
|
31
|
+
local calculate_max = function (key, attr)
|
32
|
+
local res = getHashValues(key, attr, true)
|
33
|
+
table.sort(res)
|
34
|
+
return res[#res]
|
35
|
+
end
|
36
|
+
|
37
|
+
local calculate_min = function (key, attr)
|
38
|
+
local res = getHashValues(key, attr, true)
|
39
|
+
table.sort(res)
|
40
|
+
return res[1]
|
41
|
+
end
|
42
|
+
|
43
|
+
local calculate_sum = function (key, attr)
|
44
|
+
local res = getHashValues(key, attr, true)
|
45
|
+
local sum = 0
|
46
|
+
for index, s in pairs(res) do
|
47
|
+
sum = sum + s
|
48
|
+
end
|
49
|
+
return sum
|
50
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module ActiveRedis
|
2
|
+
|
3
|
+
module Naming
|
4
|
+
|
5
|
+
def table_name(id = "")
|
6
|
+
"#{self.name.downcase.pluralize}:item:#{id}"
|
7
|
+
end
|
8
|
+
|
9
|
+
def key_name
|
10
|
+
"#{table_name}*"
|
11
|
+
end
|
12
|
+
|
13
|
+
def info_table_name
|
14
|
+
"#{self.name.downcase.pluralize}:info"
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
module ActiveRedis
|
2
|
+
module Persistence
|
3
|
+
|
4
|
+
def self.included(base)
|
5
|
+
base.extend ClassMethods
|
6
|
+
end
|
7
|
+
|
8
|
+
def initialize(attrs = {})
|
9
|
+
self.attributes = attrs
|
10
|
+
end
|
11
|
+
|
12
|
+
def save
|
13
|
+
ActiveRedis.connection.save_table self.class, prepare_hash
|
14
|
+
end
|
15
|
+
|
16
|
+
def destroy
|
17
|
+
ActiveRedis.connection.destroy self.class, self.id
|
18
|
+
end
|
19
|
+
|
20
|
+
def update(attrs = {})
|
21
|
+
self.attributes = attrs
|
22
|
+
self.save
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def fill_attributes
|
28
|
+
self.id ||= ActiveRedis.connection.next_id(self.class)
|
29
|
+
self.created_at ||= Time.now.to_i
|
30
|
+
self.updated_at = Time.now.to_i
|
31
|
+
end
|
32
|
+
|
33
|
+
def prepare_hash
|
34
|
+
fill_attributes
|
35
|
+
self.class.defined_attributes.inject({}) do |hash, attribute|
|
36
|
+
hash[attribute] = self.send("#{attribute}"); hash
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
module ClassMethods
|
41
|
+
|
42
|
+
def create(attrs = {})
|
43
|
+
self.new(attrs).tap do |model|
|
44
|
+
model.save
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def destroy_all
|
49
|
+
ActiveRedis.connection.destroy_all self
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'active_redis/adapters/basic_adapter'
|
2
|
+
|
3
|
+
module ActiveRedis
|
4
|
+
class Railtie < Rails::Railtie
|
5
|
+
config.after_initialize do
|
6
|
+
ActiveRedis.connection = ActiveRedis::Connection.new(
|
7
|
+
ActiveRedis.config.connection_options || {},
|
8
|
+
ActiveRedis.config.adapter || ActiveRedis::Adapters::BasicAdapter
|
9
|
+
)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
metadata
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: active_redis
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sergey Gernyak
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-10-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
15
|
+
requirements:
|
16
|
+
- - ! '>='
|
17
|
+
- !ruby/object:Gem::Version
|
18
|
+
version: '0'
|
19
|
+
type: :runtime
|
20
|
+
prerelease: false
|
21
|
+
name: redis
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ! '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - ~>
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '1.3'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
name: bundler
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.3'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
requirement: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ! '>='
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
type: :development
|
48
|
+
prerelease: false
|
49
|
+
name: rake
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
requirement: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ! '>='
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
type: :development
|
62
|
+
prerelease: false
|
63
|
+
name: rake-notes
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ! '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: Small childly ORM for Redis
|
70
|
+
email:
|
71
|
+
- sergeg1990@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- .gitignore
|
77
|
+
- Gemfile
|
78
|
+
- LICENSE.txt
|
79
|
+
- README.md
|
80
|
+
- Rakefile
|
81
|
+
- active_redis.gemspec
|
82
|
+
- lib/active_redis.rb
|
83
|
+
- lib/active_redis/adapters/basic_adapter.rb
|
84
|
+
- lib/active_redis/associations.rb
|
85
|
+
- lib/active_redis/attributes.rb
|
86
|
+
- lib/active_redis/base.rb
|
87
|
+
- lib/active_redis/calculations.rb
|
88
|
+
- lib/active_redis/config.rb
|
89
|
+
- lib/active_redis/connection.rb
|
90
|
+
- lib/active_redis/connection_ext/calculations_layer.rb
|
91
|
+
- lib/active_redis/connection_ext/finders_layer.rb
|
92
|
+
- lib/active_redis/connection_ext/persistence_layer.rb
|
93
|
+
- lib/active_redis/constants.rb
|
94
|
+
- lib/active_redis/errors.rb
|
95
|
+
- lib/active_redis/finders.rb
|
96
|
+
- lib/active_redis/helpers/lua_scripts.rb
|
97
|
+
- lib/active_redis/lua_scripts/main.lua
|
98
|
+
- lib/active_redis/naming.rb
|
99
|
+
- lib/active_redis/persistence.rb
|
100
|
+
- lib/active_redis/railtie.rb
|
101
|
+
- lib/active_redis/version.rb
|
102
|
+
homepage: ''
|
103
|
+
licenses:
|
104
|
+
- MIT
|
105
|
+
metadata: {}
|
106
|
+
post_install_message:
|
107
|
+
rdoc_options: []
|
108
|
+
require_paths:
|
109
|
+
- lib
|
110
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
111
|
+
requirements:
|
112
|
+
- - ! '>='
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
116
|
+
requirements:
|
117
|
+
- - ! '>='
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
version: '0'
|
120
|
+
requirements: []
|
121
|
+
rubyforge_project:
|
122
|
+
rubygems_version: 2.0.6
|
123
|
+
signing_key:
|
124
|
+
specification_version: 4
|
125
|
+
summary: Small childly ORM for Redis
|
126
|
+
test_files: []
|