redis-objects-rmap 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,4 +1,7 @@
1
1
  source 'https://rubygems.org'
2
-
3
- # Specify your gem's dependencies in redis-objects-rmap.gemspec
4
2
  gemspec
3
+
4
+ gem 'sqlite3'
5
+ gem 'activerecord'
6
+ gem 'rspec'
7
+ gem 'pry'
data/README.md CHANGED
@@ -25,12 +25,26 @@ class Foo < ActiveRecord::Base
25
25
  end
26
26
 
27
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
28
+ Foo.rmap # {1 => 'foo'} <- Does SQL request and puts to Redis
29
+ Foo.rmap # {1 => 'foo'} <- Gets from Redis without an SQL query
30
+ ```
31
+
32
+ You can specify field to use as an ID source:
30
33
 
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
+ ```ruby
35
+ class Foo < ActiveRecord::Base
36
+ include Redis::Objects::RMap
37
+ has_rmap :title, :my_id
38
+ end
39
+ ```
40
+
41
+ Or even specify lambdas to prepare your cache:
42
+
43
+ ```ruby
44
+ class Foo < ActiveRecord::Base
45
+ include Redis::Objects::RMap
46
+ has_rmap :title => lambda{|x| x.camelize}, :id => lambda{|x| x.to_s}
47
+ end
34
48
  ```
35
49
 
36
50
  ## Contributing
@@ -1,7 +1,7 @@
1
1
  class Redis
2
2
  module Objects
3
3
  module Rmap
4
- VERSION = "0.0.1"
4
+ VERSION = "0.1.0"
5
5
  end
6
6
  end
7
7
  end
@@ -1,3 +1,4 @@
1
+ require 'active_support/concern'
1
2
  require 'redis/objects'
2
3
  require 'pry'
3
4
 
@@ -7,7 +8,7 @@ class Redis
7
8
  included do
8
9
  include Redis::Objects
9
10
 
10
- cattr_accessor :rmap_title_field
11
+ class << self; attr_accessor :rmap_options; end
11
12
  value :rmap_storage, :global => true, :marshal => true
12
13
 
13
14
  after_save do
@@ -20,14 +21,40 @@ class Redis
20
21
  end
21
22
 
22
23
  module ClassMethods
23
- def has_rmap(title_field)
24
- self.rmap_title_field = title_field
24
+ def has_rmap(title=nil, id=nil)
25
+ options = {
26
+ :title => title,
27
+ :id => id || :id
28
+ }.with_indifferent_access
29
+
30
+ raise "title can not be blank" if options[:title].blank?
31
+
32
+ self.rmap_options = {}.with_indifferent_access
33
+
34
+ %w(id title).each do |x|
35
+ if options[x].is_a?(Hash)
36
+ self.rmap_options["#{x}_field"] = options[x].keys[0].to_sym
37
+ self.rmap_options["#{x}_proc"] = options[x].values[0]
38
+ else
39
+ self.rmap_options["#{x}_field"] = options[x].to_sym
40
+ self.rmap_options["#{x}_proc"] = nil
41
+ end
42
+ end
25
43
  end
26
44
 
27
45
  def rmap_cache
28
- models = self.select([:id, rmap_title_field]).map{|a|
29
- [a.send(rmap_title_field), a.id.to_s]
30
- }
46
+ models = self.select([rmap_options[:id_field], rmap_options[:title_field]]).map do |a|
47
+ data = []
48
+
49
+ %w(id title).each do |x|
50
+ value = a.send(rmap_options["#{x}_field"])
51
+ value = rmap_options["#{x}_proc"].call(value) unless rmap_options["#{x}_proc"].blank?
52
+
53
+ data << value
54
+ end
55
+
56
+ data
57
+ end
31
58
 
32
59
  self.rmap_storage = Hash[*models.flatten]
33
60
  end
@@ -17,5 +17,6 @@ Gem::Specification.new do |gem|
17
17
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
18
  gem.require_paths = ["lib"]
19
19
 
20
+ gem.add_dependency "activesupport"
20
21
  gem.add_dependency "redis-objects"
21
22
  end
@@ -0,0 +1,84 @@
1
+ require_relative '../../../spec_helper'
2
+
3
+ describe Redis::Objects::RMap do
4
+ describe "Module" do
5
+ it "includes" do
6
+ class Model < ActiveRecord::Base
7
+ include Redis::Objects::RMap
8
+ end
9
+ end
10
+
11
+ it "requires title" do
12
+ expect {
13
+ class Foo < ActiveRecord::Base
14
+ include Redis::Objects::RMap
15
+ has_rmap
16
+ end
17
+ }.to raise_error
18
+ end
19
+ end
20
+
21
+ describe "Declaration" do
22
+ it "can be simple" do
23
+ class Foo < ActiveRecord::Base
24
+ include Redis::Objects::RMap
25
+ has_rmap 'foo'
26
+ end
27
+
28
+ Foo.rmap_options.should == {
29
+ "id_field" => :id,
30
+ "id_proc" => nil,
31
+ "title_field" => :foo,
32
+ "title_proc" => nil
33
+ }
34
+ end
35
+
36
+ it "can contain id" do
37
+ class Foo < ActiveRecord::Base
38
+ include Redis::Objects::RMap
39
+ has_rmap 'foo', :bar
40
+ end
41
+
42
+ Foo.rmap_options.should == {
43
+ "id_field" => :bar,
44
+ "id_proc" => nil,
45
+ "title_field" => :foo,
46
+ "title_proc" => nil
47
+ }
48
+ end
49
+
50
+ it "can contain lambdas" do
51
+ class Foo < ActiveRecord::Base
52
+ include Redis::Objects::RMap
53
+ has_rmap 'foo' => lambda{|x| x.to_s}
54
+ end
55
+
56
+ Foo.rmap_options[:title_proc].call(:test).should == 'test'
57
+ end
58
+ end
59
+
60
+ describe "Cache" do
61
+ it "works" do
62
+ class Foo < ActiveRecord::Base
63
+ include Redis::Objects::RMap
64
+ has_rmap 'name', :id => lambda{|x| x.to_s}
65
+ end
66
+
67
+ Foo.rmap_clear
68
+ Foo.rmap.should == {}
69
+
70
+ Foo.create! :name => "test1"
71
+ Foo.should_receive(:rmap_cache)
72
+ Foo.rmap
73
+ Foo.rspec_reset
74
+
75
+ Foo.rmap.should == {"1" => "test1"}
76
+ Foo.should_not_receive(:rmap_cache)
77
+ Foo.rmap
78
+ Foo.rspec_reset
79
+
80
+ Foo.destroy_all
81
+ Foo.rmap.should == {}
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,10 @@
1
+ require 'redis/objects/rmap'
2
+ require 'active_record'
3
+
4
+ ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"
5
+ ActiveRecord::Migration.create_table :foos do |t|
6
+ t.string :name
7
+ end
8
+
9
+ RSpec.configure do |config|
10
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redis-objects-rmap
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,22 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-02 00:00:00.000000000 Z
12
+ date: 2013-01-11 00:00:00.000000000 Z
13
13
  dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: &70093162372080 !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: *70093162372080
14
25
  - !ruby/object:Gem::Dependency
15
26
  name: redis-objects
16
- requirement: &70207114757980 !ruby/object:Gem::Requirement
27
+ requirement: &70093162387240 !ruby/object:Gem::Requirement
17
28
  none: false
18
29
  requirements:
19
30
  - - ! '>='
@@ -21,7 +32,7 @@ dependencies:
21
32
  version: '0'
22
33
  type: :runtime
23
34
  prerelease: false
24
- version_requirements: *70207114757980
35
+ version_requirements: *70093162387240
25
36
  description: Adds fast-map to a AR model
26
37
  email:
27
38
  - boris@roundlake.ru
@@ -38,6 +49,8 @@ files:
38
49
  - lib/redis/objects/rmap.rb
39
50
  - lib/redis/objects/rmap/version.rb
40
51
  - redis-objects-rmap.gemspec
52
+ - spec/lib/redis/objects/rmap_spec.rb
53
+ - spec/spec_helper.rb
41
54
  homepage: http://github.com/inossidabile/redis-objects-rmap
42
55
  licenses: []
43
56
  post_install_message:
@@ -62,5 +75,7 @@ rubygems_version: 1.8.15
62
75
  signing_key:
63
76
  specification_version: 3
64
77
  summary: Adds fast-map to a AR model
65
- test_files: []
78
+ test_files:
79
+ - spec/lib/redis/objects/rmap_spec.rb
80
+ - spec/spec_helper.rb
66
81
  has_rdoc: