redis_tags 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +1 -0
- data/lib/redis_tags/tag.rb +54 -53
- data/lib/redis_tags/tag_list.rb +1 -1
- data/lib/redis_tags/version.rb +1 -1
- data/lib/redis_tags.rb +2 -2
- data/spec/models/book.rb +5 -1
- metadata +3 -3
data/README.md
CHANGED
data/lib/redis_tags/tag.rb
CHANGED
@@ -1,68 +1,69 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
module RedisTags
|
2
|
+
class Tag
|
3
|
+
attr_accessor :name
|
4
|
+
attr_reader :owner_class
|
4
5
|
|
5
|
-
|
6
|
+
attr_reader :owner
|
6
7
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
8
|
+
def initialize(owner, name)
|
9
|
+
@owner_class = owner.class.to_s.downcase
|
10
|
+
@name = name.downcase.strip
|
11
|
+
@owner = owner
|
12
|
+
end
|
12
13
|
|
13
|
-
|
14
|
-
|
15
|
-
|
14
|
+
def self.starts_with?(use_engine, partial_tag_name)
|
15
|
+
use_engine.smembers "tags:all:#{partial_tag_name}"
|
16
|
+
end
|
16
17
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
18
|
+
def self.register_tag_for_autocomplete(use_engine, tag_name)
|
19
|
+
partial_tag_name = ""
|
20
|
+
tag_name.each_char do |char|
|
21
|
+
partial_tag_name += char
|
22
|
+
use_engine.sadd "tags:all:#{partial_tag_name}", tag_name
|
23
|
+
end
|
22
24
|
end
|
23
|
-
end
|
24
25
|
|
25
|
-
|
26
|
-
|
27
|
-
|
26
|
+
def count
|
27
|
+
engine.scard redis_key
|
28
|
+
end
|
28
29
|
|
29
|
-
|
30
|
-
#debugger
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
30
|
+
def self.tagged_with(klass, options = {})
|
31
|
+
#debugger
|
32
|
+
key_array = []
|
33
|
+
if options[:tags].to_a.size == 1
|
34
|
+
if options[:random].to_i > 0
|
35
|
+
klass.redis_tags_engine.srandmember RedisTags::Tag.tagged_with_key_for(klass, options[:tags]), options[:random].to_i
|
36
|
+
else
|
37
|
+
klass.redis_tags_engine.smembers RedisTags::Tag.tagged_with_key_for(klass, options[:tags])
|
38
|
+
end
|
35
39
|
else
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
else
|
46
|
-
klass.redis_tags_engine.smembers Tag.intersect_key_for(klass, options[:tags])
|
40
|
+
options[:tags].to_a.each do |tag_name|
|
41
|
+
key_array << Tag.tagged_with_key_for(klass, tag_name)
|
42
|
+
end
|
43
|
+
klass.redis_tags_engine.sinterstore RedisTags::Tag.intersect_key_for(klass, options[:tags]), *key_array
|
44
|
+
if options[:random].to_i > 0
|
45
|
+
klass.redis_tags_engine.srandmember RedisTags::Tag.intersect_key_for(klass, options[:tags]), options[:random].to_i
|
46
|
+
else
|
47
|
+
klass.redis_tags_engine.smembers RedisTags::Tag.intersect_key_for(klass, options[:tags])
|
48
|
+
end
|
47
49
|
end
|
48
50
|
end
|
49
|
-
end
|
50
51
|
|
51
|
-
|
52
|
+
protected
|
52
53
|
|
53
|
-
|
54
|
-
|
55
|
-
|
54
|
+
def self.intersect_key_for(klass, tags)
|
55
|
+
"#{klass.to_s.downcase}:inter:#{tags.sort.collect{ |tag_name| tag_name.downcase.strip.gsub(" ", '-') }.join(":")}"
|
56
|
+
end
|
56
57
|
|
57
|
-
|
58
|
-
|
59
|
-
|
58
|
+
def self.tagged_with_key_for(klass, tag_name)
|
59
|
+
"#{klass.to_s.downcase}:tagged_with:#{tag_name.to_s.downcase.strip.gsub(" ", '-')}"
|
60
|
+
end
|
60
61
|
|
61
|
-
|
62
|
-
|
63
|
-
|
62
|
+
def engine
|
63
|
+
self.owner.class.redis_tags_engine
|
64
|
+
end
|
64
65
|
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
end
|
66
|
+
def redis_key
|
67
|
+
"#{self.owner_class}:tagged_with:#{self.name.downcase.gsub(" ", '-')}"
|
68
|
+
end
|
69
|
+
end
|
data/lib/redis_tags/tag_list.rb
CHANGED
@@ -19,7 +19,7 @@ module RedisTags
|
|
19
19
|
engine.sadd "#{self.owner_class}:tagged_with:#{tag_name.gsub(" ", '-')}", self.owner_id
|
20
20
|
end
|
21
21
|
engine.multi do
|
22
|
-
Tag.register_tag_for_autocomplete(engine, tag_name)
|
22
|
+
RedisTags::Tag.register_tag_for_autocomplete(engine, tag_name)
|
23
23
|
end
|
24
24
|
end
|
25
25
|
super(tag_name)
|
data/lib/redis_tags/version.rb
CHANGED
data/lib/redis_tags.rb
CHANGED
@@ -10,7 +10,7 @@ module RedisTags
|
|
10
10
|
extend ClassMethods
|
11
11
|
include InstanceMethods
|
12
12
|
|
13
|
-
|
13
|
+
after_save :update_tags_to_redis
|
14
14
|
|
15
15
|
@@redis_tags_engine = nil
|
16
16
|
@@acts_as_taggable_on_steroids_legacy = false
|
@@ -42,7 +42,7 @@ module RedisTags
|
|
42
42
|
end
|
43
43
|
|
44
44
|
def tagged_with_prefix(partial_tag_name)
|
45
|
-
Tag.starts_with?(self.redis_tags_engine, partial_tag_name)
|
45
|
+
RedisTags::Tag.starts_with?(self.redis_tags_engine, partial_tag_name)
|
46
46
|
end
|
47
47
|
end
|
48
48
|
|
data/spec/models/book.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: redis_tags
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Elad Meidar
|