resolver 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/.rvmrc +1 -0
- data/Gemfile +3 -0
- data/LICENSE +20 -0
- data/README.md +70 -0
- data/Rakefile +12 -0
- data/init.rb +1 -0
- data/lib/resolver/key.rb +97 -0
- data/lib/resolver/version.rb +3 -0
- data/lib/resolver.rb +44 -0
- data/resolver.gemspec +27 -0
- data/test/redis-test.conf +115 -0
- data/test/resolver_test.rb +60 -0
- data/test/test_helper.rb +43 -0
- data/test/test_model.rb +99 -0
- metadata +125 -0
data/.gitignore
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm 1.9.2@resolver --create
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010, 2011 Niklas Holmgren, Sutajio
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
Resolver
|
2
|
+
========
|
3
|
+
|
4
|
+
Resolver is a flexible Redis-backed high performance index and cache
|
5
|
+
solution for ActiveModel-like objects.
|
6
|
+
|
7
|
+
Setup
|
8
|
+
-----
|
9
|
+
|
10
|
+
Assuming you already have Redis installed:
|
11
|
+
|
12
|
+
$ gem install resolver
|
13
|
+
|
14
|
+
Or add the gem to your Gemfile.
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
require 'resolver'
|
18
|
+
Resolver.redis = 'redis://.../'
|
19
|
+
```
|
20
|
+
|
21
|
+
If you are using Rails, add the above to an initializer. If Redis is running
|
22
|
+
on localhost and on the default port the second line is not needed.
|
23
|
+
|
24
|
+
Example usage
|
25
|
+
-------------
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
class Post < ActiveRecord::Base
|
29
|
+
include Resolver::Key
|
30
|
+
|
31
|
+
key :category
|
32
|
+
key :slug, :unique => true
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
Post.create(:category => 'News', :slug => 'example-post')
|
37
|
+
|
38
|
+
Post.find_by(:slug, 'example-post')
|
39
|
+
Post.exists_with?(:slug, 'example-post') # => true
|
40
|
+
Post.count_with(:category, 'News') # => 1
|
41
|
+
```
|
42
|
+
|
43
|
+
Development
|
44
|
+
-----------
|
45
|
+
|
46
|
+
If you want to make your own changes to Resolver, first clone the repo and
|
47
|
+
run the tests:
|
48
|
+
|
49
|
+
git clone git://github.com/sutajio/resolver.git
|
50
|
+
cd resolver
|
51
|
+
rake test
|
52
|
+
|
53
|
+
Remember to install the Redis server on your local machine.
|
54
|
+
|
55
|
+
Contributing
|
56
|
+
------------
|
57
|
+
|
58
|
+
Once you've made your great commits:
|
59
|
+
|
60
|
+
1. Fork Resolver
|
61
|
+
2. Create a topic branch - git checkout -b my_branch
|
62
|
+
3. Push to your branch - git push origin my_branch
|
63
|
+
4. Create a Pull Request from your branch
|
64
|
+
5. That's it!
|
65
|
+
|
66
|
+
Author
|
67
|
+
------
|
68
|
+
|
69
|
+
Resolver was created by Niklas Holmgren (niklas@sutajio.se) and released under
|
70
|
+
the MIT license.
|
data/Rakefile
ADDED
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'resolver'
|
data/lib/resolver/key.rb
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
module Resolver
|
2
|
+
module Key
|
3
|
+
|
4
|
+
def self.included(base)
|
5
|
+
base.extend(ClassMethods)
|
6
|
+
base.send(:resolver_keys)
|
7
|
+
base.send(:include, InstanceMethods)
|
8
|
+
base.after_save :resolver_add_keys_to_index
|
9
|
+
base.after_destroy :resolver_remove_keys_from_index
|
10
|
+
end
|
11
|
+
|
12
|
+
module ClassMethods
|
13
|
+
|
14
|
+
def key(name, options = {})
|
15
|
+
@resolver_keys[name.to_sym] = options || {}
|
16
|
+
define_attribute_method(name)
|
17
|
+
end
|
18
|
+
|
19
|
+
def find_by(key, value)
|
20
|
+
if options = @resolver_keys[key.to_sym]
|
21
|
+
if options[:unique]
|
22
|
+
self.find(*Resolver.redis.get(index_name(key, value)))
|
23
|
+
else
|
24
|
+
self.find(Resolver.redis.smembers(index_name(key, value)))
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def exists_with?(key, value)
|
30
|
+
Resolver.redis.exists(index_name(key, value))
|
31
|
+
end
|
32
|
+
|
33
|
+
def count_with(key, value)
|
34
|
+
if options = @resolver_keys[key.to_sym]
|
35
|
+
if options[:unique]
|
36
|
+
exists_with?(key, value) ? 1 : 0
|
37
|
+
else
|
38
|
+
Resolver.redis.scard(index_name(key, value))
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
protected
|
44
|
+
|
45
|
+
def resolver_keys
|
46
|
+
@resolver_keys ||= {}
|
47
|
+
@resolver_keys
|
48
|
+
end
|
49
|
+
|
50
|
+
def index_name(name, value)
|
51
|
+
"#{self.class.to_s}:#{name}:#{value}"
|
52
|
+
end
|
53
|
+
|
54
|
+
def write_index(key, object, options)
|
55
|
+
value = object.send(key)
|
56
|
+
if options[:unique]
|
57
|
+
Resolver.redis.setnx(index_name(key, value), object.id)
|
58
|
+
else
|
59
|
+
if object.respond_to?(:previous_changes) && object.send(:previous_changes).include?(key.to_s)
|
60
|
+
prev_value = object.send(:previous_changes)[key.to_s].first
|
61
|
+
Resolver.redis.smove(index_name(key, prev_value),
|
62
|
+
index_name(key, value), object.id)
|
63
|
+
else
|
64
|
+
Resolver.redis.sadd(index_name(key, value), object.id)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def clean_index(key, object, options)
|
70
|
+
value = object.send(key)
|
71
|
+
if options[:unique]
|
72
|
+
Resolver.redis.del(index_name(key, value))
|
73
|
+
else
|
74
|
+
Resolver.redis.srem(index_name(key, value), object.id)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
79
|
+
|
80
|
+
module InstanceMethods
|
81
|
+
|
82
|
+
def resolver_add_keys_to_index
|
83
|
+
self.class.send(:resolver_keys).each do |key,options|
|
84
|
+
self.class.send(:write_index, key, self, options)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
def resolver_remove_keys_from_index
|
89
|
+
self.class.send(:resolver_keys).each do |key,options|
|
90
|
+
self.class.send(:clean_index, key, self, options)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
95
|
+
|
96
|
+
end
|
97
|
+
end
|
data/lib/resolver.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'redis'
|
2
|
+
require 'redis/namespace'
|
3
|
+
|
4
|
+
require 'resolver/version'
|
5
|
+
require 'resolver/key'
|
6
|
+
|
7
|
+
module Resolver
|
8
|
+
extend self
|
9
|
+
|
10
|
+
# Accepts:
|
11
|
+
# 1. A 'hostname:port' string
|
12
|
+
# 2. A 'hostname:port:db' string (to select the Redis db)
|
13
|
+
# 3. A 'hostname:port/namespace' string (to set the Redis namespace)
|
14
|
+
# 4. A redis URL string 'redis://host:port'
|
15
|
+
# 5. An instance of `Redis`, `Redis::Client`, `Redis::DistRedis`,
|
16
|
+
# or `Redis::Namespace`.
|
17
|
+
def redis=(server)
|
18
|
+
if server.respond_to? :split
|
19
|
+
if server =~ /redis\:\/\//
|
20
|
+
redis = Redis.connect(:url => server)
|
21
|
+
else
|
22
|
+
server, namespace = server.split('/', 2)
|
23
|
+
host, port, db = server.split(':')
|
24
|
+
redis = Redis.new(:host => host, :port => port,
|
25
|
+
:thread_safe => true, :db => db)
|
26
|
+
end
|
27
|
+
namespace ||= :resolver
|
28
|
+
@redis = Redis::Namespace.new(namespace, :redis => redis)
|
29
|
+
elsif server.respond_to? :namespace=
|
30
|
+
@redis = server
|
31
|
+
else
|
32
|
+
@redis = Redis::Namespace.new(:resolver, :redis => server)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
# Returns the current Redis connection. If none has been created, will
|
37
|
+
# create a new one.
|
38
|
+
def redis
|
39
|
+
return @redis if @redis
|
40
|
+
self.redis = 'localhost:6379'
|
41
|
+
self.redis
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
data/resolver.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "resolver/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "resolver"
|
7
|
+
s.version = Resolver::VERSION
|
8
|
+
s.authors = ["Niklas Holmgren"]
|
9
|
+
s.email = ["niklas@sutajio.se"]
|
10
|
+
s.homepage = "http://github.com/sutajio/resolver/"
|
11
|
+
s.summary = %q{Resolver is a flexible Redis-backed high performance index and cache solution for ActiveModel-like objects.}
|
12
|
+
s.description = %q{Resolver is a flexible Redis-backed high performance index and cache solution for ActiveModel-like objects.}
|
13
|
+
|
14
|
+
s.rubyforge_project = "resolver"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
s.extra_rdoc_files = [ "LICENSE", "README.md" ]
|
22
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
23
|
+
|
24
|
+
s.add_dependency('redis', '> 2.0.0')
|
25
|
+
s.add_dependency('redis-namespace', '> 0.8.0')
|
26
|
+
s.add_development_dependency('activemodel')
|
27
|
+
end
|
@@ -0,0 +1,115 @@
|
|
1
|
+
# Redis configuration file example
|
2
|
+
|
3
|
+
# By default Redis does not run as a daemon. Use 'yes' if you need it.
|
4
|
+
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
|
5
|
+
daemonize yes
|
6
|
+
|
7
|
+
# When run as a daemon, Redis write a pid file in /var/run/redis.pid by default.
|
8
|
+
# You can specify a custom pid file location here.
|
9
|
+
pidfile ./test/redis-test.pid
|
10
|
+
|
11
|
+
# Accept connections on the specified port, default is 6379
|
12
|
+
port 9736
|
13
|
+
|
14
|
+
# If you want you can bind a single interface, if the bind option is not
|
15
|
+
# specified all the interfaces will listen for connections.
|
16
|
+
#
|
17
|
+
# bind 127.0.0.1
|
18
|
+
|
19
|
+
# Close the connection after a client is idle for N seconds (0 to disable)
|
20
|
+
timeout 300
|
21
|
+
|
22
|
+
# Save the DB on disk:
|
23
|
+
#
|
24
|
+
# save <seconds> <changes>
|
25
|
+
#
|
26
|
+
# Will save the DB if both the given number of seconds and the given
|
27
|
+
# number of write operations against the DB occurred.
|
28
|
+
#
|
29
|
+
# In the example below the behaviour will be to save:
|
30
|
+
# after 900 sec (15 min) if at least 1 key changed
|
31
|
+
# after 300 sec (5 min) if at least 10 keys changed
|
32
|
+
# after 60 sec if at least 10000 keys changed
|
33
|
+
save 900 1
|
34
|
+
save 300 10
|
35
|
+
save 60 10000
|
36
|
+
|
37
|
+
# The filename where to dump the DB
|
38
|
+
dbfilename dump.rdb
|
39
|
+
|
40
|
+
# For default save/load DB in/from the working directory
|
41
|
+
# Note that you must specify a directory not a file name.
|
42
|
+
dir ./test/
|
43
|
+
|
44
|
+
# Set server verbosity to 'debug'
|
45
|
+
# it can be one of:
|
46
|
+
# debug (a lot of information, useful for development/testing)
|
47
|
+
# notice (moderately verbose, what you want in production probably)
|
48
|
+
# warning (only very important / critical messages are logged)
|
49
|
+
loglevel debug
|
50
|
+
|
51
|
+
# Specify the log file name. Also 'stdout' can be used to force
|
52
|
+
# the demon to log on the standard output. Note that if you use standard
|
53
|
+
# output for logging but daemonize, logs will be sent to /dev/null
|
54
|
+
logfile stdout
|
55
|
+
|
56
|
+
# Set the number of databases. The default database is DB 0, you can select
|
57
|
+
# a different one on a per-connection basis using SELECT <dbid> where
|
58
|
+
# dbid is a number between 0 and 'databases'-1
|
59
|
+
databases 16
|
60
|
+
|
61
|
+
################################# REPLICATION #################################
|
62
|
+
|
63
|
+
# Master-Slave replication. Use slaveof to make a Redis instance a copy of
|
64
|
+
# another Redis server. Note that the configuration is local to the slave
|
65
|
+
# so for example it is possible to configure the slave to save the DB with a
|
66
|
+
# different interval, or to listen to another port, and so on.
|
67
|
+
|
68
|
+
# slaveof <masterip> <masterport>
|
69
|
+
|
70
|
+
################################## SECURITY ###################################
|
71
|
+
|
72
|
+
# Require clients to issue AUTH <PASSWORD> before processing any other
|
73
|
+
# commands. This might be useful in environments in which you do not trust
|
74
|
+
# others with access to the host running redis-server.
|
75
|
+
#
|
76
|
+
# This should stay commented out for backward compatibility and because most
|
77
|
+
# people do not need auth (e.g. they run their own servers).
|
78
|
+
|
79
|
+
# requirepass foobared
|
80
|
+
|
81
|
+
################################### LIMITS ####################################
|
82
|
+
|
83
|
+
# Set the max number of connected clients at the same time. By default there
|
84
|
+
# is no limit, and it's up to the number of file descriptors the Redis process
|
85
|
+
# is able to open. The special value '0' means no limts.
|
86
|
+
# Once the limit is reached Redis will close all the new connections sending
|
87
|
+
# an error 'max number of clients reached'.
|
88
|
+
|
89
|
+
# maxclients 128
|
90
|
+
|
91
|
+
# Don't use more memory than the specified amount of bytes.
|
92
|
+
# When the memory limit is reached Redis will try to remove keys with an
|
93
|
+
# EXPIRE set. It will try to start freeing keys that are going to expire
|
94
|
+
# in little time and preserve keys with a longer time to live.
|
95
|
+
# Redis will also try to remove objects from free lists if possible.
|
96
|
+
#
|
97
|
+
# If all this fails, Redis will start to reply with errors to commands
|
98
|
+
# that will use more memory, like SET, LPUSH, and so on, and will continue
|
99
|
+
# to reply to most read-only commands like GET.
|
100
|
+
#
|
101
|
+
# WARNING: maxmemory can be a good idea mainly if you want to use Redis as a
|
102
|
+
# 'state' server or cache, not as a real DB. When Redis is used as a real
|
103
|
+
# database the memory usage will grow over the weeks, it will be obvious if
|
104
|
+
# it is going to use too much memory in the long run, and you'll have the time
|
105
|
+
# to upgrade. With maxmemory after the limit is reached you'll start to get
|
106
|
+
# errors for write operations, and this may even lead to DB inconsistency.
|
107
|
+
|
108
|
+
# maxmemory <bytes>
|
109
|
+
|
110
|
+
############################### ADVANCED CONFIG ###############################
|
111
|
+
|
112
|
+
# Glue small output buffers together in order to send small replies in a
|
113
|
+
# single TCP packet. Uses a bit more CPU but most of the times it is a win
|
114
|
+
# in terms of number of queries per second. Use 'yes' if unsure.
|
115
|
+
glueoutputbuf yes
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require File.expand_path('test/test_helper')
|
2
|
+
require File.expand_path('test/test_model')
|
3
|
+
|
4
|
+
class ResolverTest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
TestModel.send(:include, Resolver::Key)
|
7
|
+
|
8
|
+
def setup
|
9
|
+
Resolver.redis.flushall
|
10
|
+
TestModel.reset
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_can_set_a_namespace_through_a_url_like_string
|
14
|
+
assert Resolver.redis
|
15
|
+
assert_equal :resolver, Resolver.redis.namespace
|
16
|
+
Resolver.redis = 'localhost:9736/namespace'
|
17
|
+
assert_equal 'namespace', Resolver.redis.namespace
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_key
|
21
|
+
TestModel.key(:example)
|
22
|
+
TestModel.create('one', :example => 'foo')
|
23
|
+
TestModel.create('two', :example => 'foo')
|
24
|
+
assert_equal [TestModel.new('one'), TestModel.new('two')], TestModel.find_by(:example, 'foo').sort
|
25
|
+
assert_equal [], TestModel.find_by(:example, 'bar')
|
26
|
+
assert_equal true, TestModel.exists_with?(:example, 'foo')
|
27
|
+
assert_equal false, TestModel.exists_with?(:example, 'bar')
|
28
|
+
assert_equal 2, TestModel.count_with(:example, 'foo')
|
29
|
+
assert_equal 0, TestModel.count_with(:example, 'bar')
|
30
|
+
TestModel.find('one').first.destroy
|
31
|
+
TestModel.find('two').first.destroy
|
32
|
+
assert_equal 0, TestModel.count_with(:example, 'foo')
|
33
|
+
assert_equal false, TestModel.exists_with?(:example, 'foo')
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_key_moving
|
37
|
+
TestModel.key(:example)
|
38
|
+
one = TestModel.create('one', :example => 'foo')
|
39
|
+
assert_equal [one], TestModel.find_by(:example, 'foo')
|
40
|
+
one.update_attributes(:example => 'bar')
|
41
|
+
assert_equal [], TestModel.find_by(:example, 'foo')
|
42
|
+
assert_equal [one], TestModel.find_by(:example, 'bar')
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_unique_key
|
46
|
+
TestModel.key(:example, :unique => true)
|
47
|
+
TestModel.create('one', :example => 'foo')
|
48
|
+
TestModel.create('two', :example => 'foo')
|
49
|
+
assert_equal [TestModel.new('one')], TestModel.find_by(:example, 'foo')
|
50
|
+
assert_equal true, TestModel.exists_with?(:example, 'foo')
|
51
|
+
assert_equal false, TestModel.exists_with?(:example, 'bar')
|
52
|
+
assert_equal 1, TestModel.count_with(:example, 'foo')
|
53
|
+
assert_equal 0, TestModel.count_with(:example, 'bar')
|
54
|
+
TestModel.find('one').first.destroy
|
55
|
+
TestModel.find('two').first.destroy
|
56
|
+
assert_equal 0, TestModel.count_with(:example, 'foo')
|
57
|
+
assert_equal false, TestModel.exists_with?(:example, 'foo')
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
|
2
|
+
dir = File.dirname(File.expand_path(__FILE__))
|
3
|
+
$LOAD_PATH.unshift dir + '/../lib'
|
4
|
+
|
5
|
+
require 'rubygems'
|
6
|
+
require 'test/unit'
|
7
|
+
require 'resolver'
|
8
|
+
|
9
|
+
#
|
10
|
+
# make sure we can run redis
|
11
|
+
#
|
12
|
+
|
13
|
+
if !system("which redis-server")
|
14
|
+
puts '', "** can't find `redis-server` in your path"
|
15
|
+
puts "** try running `sudo rake install`"
|
16
|
+
abort ''
|
17
|
+
end
|
18
|
+
|
19
|
+
|
20
|
+
#
|
21
|
+
# start our own redis when the tests start,
|
22
|
+
# kill it when they end
|
23
|
+
#
|
24
|
+
|
25
|
+
at_exit do
|
26
|
+
next if $!
|
27
|
+
|
28
|
+
if defined?(MiniTest)
|
29
|
+
exit_code = MiniTest::Unit.new.run(ARGV)
|
30
|
+
else
|
31
|
+
exit_code = Test::Unit::AutoRunner.run
|
32
|
+
end
|
33
|
+
|
34
|
+
pid = `ps -A -o pid,command | grep [r]edis-test`.split(" ")[0]
|
35
|
+
puts "Killing test redis server..."
|
36
|
+
`rm -f #{dir}/dump.rdb`
|
37
|
+
Process.kill("KILL", pid.to_i)
|
38
|
+
exit exit_code
|
39
|
+
end
|
40
|
+
|
41
|
+
puts "Starting redis for testing at localhost:9736..."
|
42
|
+
`redis-server #{dir}/redis-test.conf`
|
43
|
+
Resolver.redis = 'localhost:9736'
|
data/test/test_model.rb
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
require 'active_model'
|
2
|
+
|
3
|
+
class TestModel
|
4
|
+
extend ActiveModel::Callbacks
|
5
|
+
include ActiveModel::Dirty
|
6
|
+
|
7
|
+
define_model_callbacks :create, :update, :destroy, :save
|
8
|
+
|
9
|
+
attr_reader :id
|
10
|
+
attr_reader :attributes
|
11
|
+
attr_accessor :persisted
|
12
|
+
|
13
|
+
def initialize(id, attributes = {})
|
14
|
+
@id = id
|
15
|
+
@attributes = attributes
|
16
|
+
end
|
17
|
+
|
18
|
+
def to_key
|
19
|
+
@id
|
20
|
+
end
|
21
|
+
|
22
|
+
def method_missing(sym, *args, &block)
|
23
|
+
if @attributes.keys.include?(sym)
|
24
|
+
@attributes[sym]
|
25
|
+
else
|
26
|
+
super
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def <=>(other)
|
31
|
+
self.to_key <=> other.to_key
|
32
|
+
end
|
33
|
+
|
34
|
+
def ==(other)
|
35
|
+
other ? self.to_key == other.to_key : false
|
36
|
+
end
|
37
|
+
|
38
|
+
def save
|
39
|
+
run_callbacks :save do
|
40
|
+
@persisted ? update : create
|
41
|
+
@previously_changed = changes
|
42
|
+
@changed_attributes.clear
|
43
|
+
end
|
44
|
+
self
|
45
|
+
end
|
46
|
+
|
47
|
+
def destroy
|
48
|
+
run_callbacks :destroy do
|
49
|
+
@@database ||= {}
|
50
|
+
@@database[@id] = nil
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def update_attributes(attributes)
|
55
|
+
attributes.keys.each do |key|
|
56
|
+
unless @attributes[key] == attributes[key]
|
57
|
+
self.send("#{key}_will_change!")
|
58
|
+
end
|
59
|
+
end
|
60
|
+
@attributes.merge!(attributes)
|
61
|
+
save
|
62
|
+
end
|
63
|
+
|
64
|
+
def self.create(id, attributes = {})
|
65
|
+
self.new(id, attributes).save
|
66
|
+
end
|
67
|
+
|
68
|
+
def self.find(*ids)
|
69
|
+
@@database ||= {}
|
70
|
+
ids.flatten.map do |id|
|
71
|
+
if @@database[id]
|
72
|
+
doc = self.new(id, @@database[id])
|
73
|
+
doc.persisted = true
|
74
|
+
doc
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def self.reset
|
80
|
+
@@database = {}
|
81
|
+
end
|
82
|
+
|
83
|
+
protected
|
84
|
+
|
85
|
+
def create
|
86
|
+
run_callbacks :create do
|
87
|
+
@@database ||= {}
|
88
|
+
@@database[@id] = @attributes
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
def update
|
93
|
+
run_callbacks :update do
|
94
|
+
@@database ||= {}
|
95
|
+
@@database[@id] = @attributes
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
end
|
metadata
ADDED
@@ -0,0 +1,125 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: resolver
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Niklas Holmgren
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2011-11-02 00:00:00 +01:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: redis
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">"
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 2
|
30
|
+
- 0
|
31
|
+
- 0
|
32
|
+
version: 2.0.0
|
33
|
+
type: :runtime
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: redis-namespace
|
37
|
+
prerelease: false
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ">"
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
segments:
|
44
|
+
- 0
|
45
|
+
- 8
|
46
|
+
- 0
|
47
|
+
version: 0.8.0
|
48
|
+
type: :runtime
|
49
|
+
version_requirements: *id002
|
50
|
+
- !ruby/object:Gem::Dependency
|
51
|
+
name: activemodel
|
52
|
+
prerelease: false
|
53
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
segments:
|
59
|
+
- 0
|
60
|
+
version: "0"
|
61
|
+
type: :development
|
62
|
+
version_requirements: *id003
|
63
|
+
description: Resolver is a flexible Redis-backed high performance index and cache solution for ActiveModel-like objects.
|
64
|
+
email:
|
65
|
+
- niklas@sutajio.se
|
66
|
+
executables: []
|
67
|
+
|
68
|
+
extensions: []
|
69
|
+
|
70
|
+
extra_rdoc_files:
|
71
|
+
- LICENSE
|
72
|
+
- README.md
|
73
|
+
files:
|
74
|
+
- .gitignore
|
75
|
+
- .rvmrc
|
76
|
+
- Gemfile
|
77
|
+
- LICENSE
|
78
|
+
- README.md
|
79
|
+
- Rakefile
|
80
|
+
- init.rb
|
81
|
+
- lib/resolver.rb
|
82
|
+
- lib/resolver/key.rb
|
83
|
+
- lib/resolver/version.rb
|
84
|
+
- resolver.gemspec
|
85
|
+
- test/redis-test.conf
|
86
|
+
- test/resolver_test.rb
|
87
|
+
- test/test_helper.rb
|
88
|
+
- test/test_model.rb
|
89
|
+
has_rdoc: true
|
90
|
+
homepage: http://github.com/sutajio/resolver/
|
91
|
+
licenses: []
|
92
|
+
|
93
|
+
post_install_message:
|
94
|
+
rdoc_options:
|
95
|
+
- --charset=UTF-8
|
96
|
+
require_paths:
|
97
|
+
- lib
|
98
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
99
|
+
none: false
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
segments:
|
104
|
+
- 0
|
105
|
+
version: "0"
|
106
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
107
|
+
none: false
|
108
|
+
requirements:
|
109
|
+
- - ">="
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
segments:
|
112
|
+
- 0
|
113
|
+
version: "0"
|
114
|
+
requirements: []
|
115
|
+
|
116
|
+
rubyforge_project: resolver
|
117
|
+
rubygems_version: 1.3.7
|
118
|
+
signing_key:
|
119
|
+
specification_version: 3
|
120
|
+
summary: Resolver is a flexible Redis-backed high performance index and cache solution for ActiveModel-like objects.
|
121
|
+
test_files:
|
122
|
+
- test/redis-test.conf
|
123
|
+
- test/resolver_test.rb
|
124
|
+
- test/test_helper.rb
|
125
|
+
- test/test_model.rb
|