redis-objects-rmap 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +42 -0
- data/Rakefile +1 -0
- data/init.rb +1 -0
- data/lib/redis/objects/rmap/version.rb +7 -0
- data/lib/redis/objects/rmap.rb +45 -0
- data/redis-objects-rmap.gemspec +21 -0
- metadata +66 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Boris Staal
|
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,42 @@
|
|
1
|
+
# Redis::Objects::RMap
|
2
|
+
|
3
|
+
Adds Redis-cached hash containing correspondence between row id and selected field to ActiveRecord models.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'redis-objects-rmap'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install redis-objects-rmap
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
class Foo < ActiveRecord::Base
|
23
|
+
include Redis::Objects::RMap
|
24
|
+
has_rmap :title # field to use as a title
|
25
|
+
end
|
26
|
+
|
27
|
+
Foo.create! :title => 'foo'
|
28
|
+
Foo.rmap # {'foo' => '1'} <- Does SQL request and puts to Redis
|
29
|
+
Foo.rmap # {'foo' => '1'} <- Gets from Redis without SQL query
|
30
|
+
|
31
|
+
Foo.create! :title => 'bar'
|
32
|
+
Foo.rmap # {'foo' => '1', 'bar' => '2'} <- Does SQL request and puts to Redis
|
33
|
+
Foo.rmap # {'foo' => '1', 'bar' => '2'} <- Gets from Redis without SQL query
|
34
|
+
```
|
35
|
+
|
36
|
+
## Contributing
|
37
|
+
|
38
|
+
1. Fork it
|
39
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
40
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
41
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
42
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'redis/objects/rmap'
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'redis/objects'
|
2
|
+
require 'pry'
|
3
|
+
|
4
|
+
class Redis
|
5
|
+
module Objects
|
6
|
+
module RMap extend ActiveSupport::Concern
|
7
|
+
included do
|
8
|
+
include Redis::Objects
|
9
|
+
|
10
|
+
cattr_accessor :rmap_title_field
|
11
|
+
value :rmap_storage, :global => true, :marshal => true
|
12
|
+
|
13
|
+
after_save do
|
14
|
+
self.class.rmap_clear
|
15
|
+
end
|
16
|
+
|
17
|
+
after_destroy do
|
18
|
+
self.class.rmap_clear
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
module ClassMethods
|
23
|
+
def has_rmap(title_field)
|
24
|
+
self.rmap_title_field = title_field
|
25
|
+
end
|
26
|
+
|
27
|
+
def rmap_cache
|
28
|
+
models = self.select([:id, rmap_title_field]).map{|a|
|
29
|
+
[a.send(rmap_title_field), a.id.to_s]
|
30
|
+
}
|
31
|
+
|
32
|
+
self.rmap_storage = Hash[*models.flatten]
|
33
|
+
end
|
34
|
+
|
35
|
+
def rmap_clear
|
36
|
+
self.rmap_storage = nil
|
37
|
+
end
|
38
|
+
|
39
|
+
def rmap
|
40
|
+
rmap_storage.value || rmap_cache
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'redis/objects/rmap/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "redis-objects-rmap"
|
8
|
+
gem.version = Redis::Objects::Rmap::VERSION
|
9
|
+
gem.authors = ["Boris Staal"]
|
10
|
+
gem.email = ["boris@roundlake.ru"]
|
11
|
+
gem.description = %q{Adds fast-map to a AR model}
|
12
|
+
gem.summary = gem.description
|
13
|
+
gem.homepage = "http://github.com/inossidabile/redis-objects-rmap"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_dependency "redis-objects"
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: redis-objects-rmap
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Boris Staal
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-01-02 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: redis-objects
|
16
|
+
requirement: &70207114757980 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70207114757980
|
25
|
+
description: Adds fast-map to a AR model
|
26
|
+
email:
|
27
|
+
- boris@roundlake.ru
|
28
|
+
executables: []
|
29
|
+
extensions: []
|
30
|
+
extra_rdoc_files: []
|
31
|
+
files:
|
32
|
+
- .gitignore
|
33
|
+
- Gemfile
|
34
|
+
- LICENSE.txt
|
35
|
+
- README.md
|
36
|
+
- Rakefile
|
37
|
+
- init.rb
|
38
|
+
- lib/redis/objects/rmap.rb
|
39
|
+
- lib/redis/objects/rmap/version.rb
|
40
|
+
- redis-objects-rmap.gemspec
|
41
|
+
homepage: http://github.com/inossidabile/redis-objects-rmap
|
42
|
+
licenses: []
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options: []
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
none: false
|
49
|
+
requirements:
|
50
|
+
- - ! '>='
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ! '>='
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: '0'
|
59
|
+
requirements: []
|
60
|
+
rubyforge_project:
|
61
|
+
rubygems_version: 1.8.15
|
62
|
+
signing_key:
|
63
|
+
specification_version: 3
|
64
|
+
summary: Adds fast-map to a AR model
|
65
|
+
test_files: []
|
66
|
+
has_rdoc:
|