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 CHANGED
@@ -51,6 +51,7 @@ Or install it yourself as:
51
51
  include RedisTags
52
52
 
53
53
  uses_redis_tags :engine => Redis.new
54
+
54
55
  end
55
56
 
56
57
  @user = User.new
@@ -1,68 +1,69 @@
1
- class Tag
2
- attr_accessor :name
3
- attr_reader :owner_class
1
+ module RedisTags
2
+ class Tag
3
+ attr_accessor :name
4
+ attr_reader :owner_class
4
5
 
5
- attr_reader :owner
6
+ attr_reader :owner
6
7
 
7
- def initialize(owner, name)
8
- @owner_class = owner.class.to_s.downcase
9
- @name = name.downcase.strip
10
- @owner = owner
11
- end
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
- def Tag.starts_with?(use_engine, partial_tag_name)
14
- use_engine.smembers "tags:all:#{partial_tag_name}"
15
- end
14
+ def self.starts_with?(use_engine, partial_tag_name)
15
+ use_engine.smembers "tags:all:#{partial_tag_name}"
16
+ end
16
17
 
17
- def Tag.register_tag_for_autocomplete(use_engine, tag_name)
18
- partial_tag_name = ""
19
- tag_name.each_char do |char|
20
- partial_tag_name += char
21
- use_engine.sadd "tags:all:#{partial_tag_name}", tag_name
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
- def count
26
- engine.scard redis_key
27
- end
26
+ def count
27
+ engine.scard redis_key
28
+ end
28
29
 
29
- def Tag.tagged_with(klass, options = {})
30
- #debugger
31
- key_array = []
32
- if options[:tags].to_a.size == 1
33
- if options[:random].to_i > 0
34
- klass.redis_tags_engine.srandmember Tag.tagged_with_key_for(klass, options[:tags]), options[:random].to_i
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
- klass.redis_tags_engine.smembers Tag.tagged_with_key_for(klass, options[:tags])
37
- end
38
- else
39
- options[:tags].to_a.each do |tag_name|
40
- key_array << Tag.tagged_with_key_for(klass, tag_name)
41
- end
42
- klass.redis_tags_engine.sinterstore Tag.intersect_key_for(klass, options[:tags]), *key_array
43
- if options[:random].to_i > 0
44
- klass.redis_tags_engine.srandmember Tag.intersect_key_for(klass, options[:tags]), options[:random].to_i
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
- protected
52
+ protected
52
53
 
53
- def Tag.intersect_key_for(klass, tags)
54
- "#{klass.to_s.downcase}:inter:#{tags.sort.collect{ |tag_name| tag_name.downcase.strip.gsub(" ", '-') }.join(":")}"
55
- end
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
- def Tag.tagged_with_key_for(klass, tag_name)
58
- "#{klass.to_s.downcase}:tagged_with:#{tag_name.to_s.downcase.strip.gsub(" ", '-')}"
59
- end
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
- def engine
62
- self.owner.class.redis_tags_engine
63
- end
62
+ def engine
63
+ self.owner.class.redis_tags_engine
64
+ end
64
65
 
65
- def redis_key
66
- "#{self.owner_class}:tagged_with:#{self.name.downcase.gsub(" ", '-')}"
67
- end
68
- end
66
+ def redis_key
67
+ "#{self.owner_class}:tagged_with:#{self.name.downcase.gsub(" ", '-')}"
68
+ end
69
+ end
@@ -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)
@@ -1,3 +1,3 @@
1
1
  module RedisTags
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/redis_tags.rb CHANGED
@@ -10,7 +10,7 @@ module RedisTags
10
10
  extend ClassMethods
11
11
  include InstanceMethods
12
12
 
13
- #after_save :update_tags_to_redis
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
@@ -13,6 +13,10 @@ class Book
13
13
 
14
14
  def save
15
15
  @id = rand(1000)
16
- self.tags_collection = RedisTags::TagList.new(self).append_multi(@tag_list)
16
+ after_save
17
+ end
18
+
19
+ def after_save
20
+ update_tags_to_redis
17
21
  end
18
22
  end
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: 29
4
+ hash: 27
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Elad Meidar