redis_tags 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in redis_tags.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Elad Meidar
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,71 @@
1
+ # RedisTags
2
+
3
+ RedisTags is a simple graph based implementation of a tagging system.
4
+ Instead of using complex relational structure, this implementation consists of using Redis sets for reference and aggregation.
5
+
6
+ ## Example:
7
+
8
+ Say we tag a `User` instance with a tag named `tzetzi`:
9
+
10
+ @user = User.new
11
+ @user.tags_collection << `tzetzi`
12
+ @user.save # => <User: id:1>
13
+
14
+ Now in Redis, we will have these keys:
15
+
16
+ 1. A set that holds the ids of the users tagged by "tzetzi"
17
+
18
+ user:tagged_with:tzetzi => [1]
19
+
20
+ 2. A set that holds all the tags for a specific user instance
21
+
22
+ user:1:tag_list => ["tzetzi"]
23
+
24
+ 3. A complete breakdown of each tag per char, to allow simple autocomplete interface
25
+
26
+ tag:all:t # => ["tzetzi"]
27
+ tag:all:tz # => ["tzetzi"]
28
+ tag:all:tze # => ["tzetzi"]
29
+ tag:all:tzet # => ["tzetzi"]
30
+ tag:all:tzetz # => ["tzetzi"]
31
+ tag:all:tzetzi # => ["tzetzi"]
32
+
33
+ ## Installation
34
+
35
+ Add this line to your application's Gemfile:
36
+
37
+ gem 'redis_tags'
38
+
39
+ And then execute:
40
+
41
+ $ bundle
42
+
43
+ Or install it yourself as:
44
+
45
+ $ gem install redis_tags
46
+
47
+ ## Usage
48
+
49
+ class User
50
+
51
+ include RedisTags
52
+
53
+ uses_redis_tags :engine => Redis.new
54
+ end
55
+
56
+ @user = User.new
57
+ @user.tag_collection # => []
58
+ @user.tag_collection << "elad" # => ["elad"]
59
+ @user.tag_collection = ["beata"] # => ["beata"]
60
+ @user.save
61
+
62
+ User.tagged_with(:tags => ["elad"]) # => [@user.id]
63
+ User.tagged_with_prefix("el") # => ["elad"]
64
+
65
+ ## Contributing
66
+
67
+ 1. Fork it
68
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
69
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
70
+ 4. Push to the branch (`git push origin my-new-feature`)
71
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,68 @@
1
+ class Tag
2
+ attr_accessor :name
3
+ attr_reader :owner_class
4
+
5
+ attr_reader :owner
6
+
7
+ def initialize(owner, name)
8
+ @owner_class = owner.class.to_s.downcase
9
+ @name = name.downcase.strip
10
+ @owner = owner
11
+ end
12
+
13
+ def Tag.starts_with?(use_engine, partial_tag_name)
14
+ use_engine.smembers "tags:all:#{partial_tag_name}"
15
+ end
16
+
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
22
+ end
23
+ end
24
+
25
+ def count
26
+ engine.scard redis_key
27
+ end
28
+
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
35
+ 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])
47
+ end
48
+ end
49
+ end
50
+
51
+ protected
52
+
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
56
+
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
60
+
61
+ def engine
62
+ self.owner.class.redis_tags_engine
63
+ end
64
+
65
+ def redis_key
66
+ "#{self.owner_class}:tagged_with:#{self.name.downcase.gsub(" ", '-')}"
67
+ end
68
+ end
@@ -0,0 +1,78 @@
1
+ module RedisTags
2
+ class TagList < Array
3
+
4
+ attr_reader :owner, :owner_class, :owner_id, :tags
5
+
6
+ def initialize(owner)
7
+ @owner = owner
8
+ @owner_class = owner.class.to_s.downcase
9
+ @owner_id = owner.id
10
+ super(engine.smembers(self.redis_key))
11
+ self
12
+ end
13
+
14
+ def <<(tag_name)
15
+ tag_name = tag_name.downcase.strip
16
+ if !(self.owner_id.nil?)
17
+ engine.multi do
18
+ engine.sadd self.redis_key, tag_name
19
+ engine.sadd "#{self.owner_class}:tagged_with:#{tag_name.gsub(" ", '-')}", self.owner_id
20
+ end
21
+ engine.multi do
22
+ Tag.register_tag_for_autocomplete(engine, tag_name)
23
+ end
24
+ end
25
+ super(tag_name)
26
+ end
27
+
28
+ def delete(tag_name)
29
+ tag_name = tag_name.downcase.strip
30
+ if !(self.owner_id.nil?)
31
+ engine.multi do
32
+ engine.srem self.redis_key, tag_name
33
+ engine.srem "#{self.owner_class}:tagged_with:#{tag_name.gsub(" ", '-')}", self.owner_id
34
+ end
35
+ end
36
+ super(tag_name)
37
+ end
38
+
39
+ def delete_all
40
+ if !(self.owner_id.nil?)
41
+ engine.multi do
42
+ engine.del self.redis_key
43
+ self.each do |tag_name|
44
+ engine.srem "#{self.owner_class}:tagged_with:#{tag_name.gsub(" ", '-')}", self.owner_id
45
+ end
46
+ end
47
+ end
48
+ self.each do |tag_name|
49
+ self.delete(tag_name)
50
+ end
51
+ self
52
+ end
53
+
54
+ def append_multi(tags)
55
+ if tags.is_a?(String)
56
+ tags = tags.split(",").collect {|tag| tag.strip.downcase}
57
+ end
58
+ tags.each do |tag|
59
+ self << tag
60
+ end
61
+ self
62
+ end
63
+
64
+ def save
65
+ my_tags = self.dup
66
+ delete_all
67
+ self.append_multi(my_tags)
68
+ end
69
+
70
+ def engine
71
+ self.owner.class.redis_tags_engine
72
+ end
73
+
74
+ def redis_key
75
+ "#{self.owner_class}:#{self.owner_id}:tag_list"
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,3 @@
1
+ module RedisTags
2
+ VERSION = "0.0.1"
3
+ end
data/lib/redis_tags.rb ADDED
@@ -0,0 +1,81 @@
1
+ require "redis_tags/version"
2
+ require "redis_tags/tag"
3
+ require "redis_tags/tag_list"
4
+
5
+ module RedisTags
6
+
7
+ def self.included(base)
8
+ base.class_eval do
9
+
10
+ extend ClassMethods
11
+ include InstanceMethods
12
+
13
+ #after_save :update_tags_to_redis
14
+
15
+ @@redis_tags_engine = nil
16
+ @@acts_as_taggable_on_steroids_legacy = false
17
+ end
18
+ end
19
+
20
+ module ClassMethods
21
+
22
+ def uses_redis_tags(options = {})
23
+ options = {:engine => Redis.new, :acts_as_taggable_on_steroids_legacy_mode => false}.merge!(options)
24
+ @@redis_tags_engine = options[:engine]
25
+ @@acts_as_taggable_on_steroids_legacy = options[:acts_as_taggable_on_steroids_legacy_mode]
26
+ end
27
+
28
+ def redis_tags_engine=(redis_instance)
29
+ @@redis_tags_engine = redis_instance
30
+ end
31
+
32
+ def acts_as_taggable_on_steroids_legacy_mode?
33
+ @@acts_as_taggable_on_steroids_legacy == true
34
+ end
35
+
36
+ def redis_tags_engine
37
+ @@redis_tags_engine
38
+ end
39
+
40
+ def tagged_with(options = {})
41
+ Tag.tagged_with(self, options)
42
+ end
43
+
44
+ def tagged_with_prefix(partial_tag_name)
45
+ Tag.starts_with?(self.redis_tags_engine, partial_tag_name)
46
+ end
47
+ end
48
+
49
+ module InstanceMethods
50
+
51
+ def tag_list
52
+ if self.class.acts_as_taggable_on_steroids_legacy_mode?
53
+ legacy_tag_list = super()
54
+ tags_collection = legacy_tag_list
55
+ legacy_tag_list
56
+ else
57
+ tags_collection
58
+ end
59
+ end
60
+
61
+ def tags_collection
62
+ @tag_list ||= RedisTags::TagList.new(self)
63
+ end
64
+
65
+ def tag_with(tag)
66
+ tags_collection << tag
67
+ end
68
+
69
+ def tags_collection=(new_tag_list)
70
+ tags_collection.delete_all
71
+ @tag_list = RedisTags::TagList.new(self).append_multi(new_tag_list)
72
+ end
73
+
74
+ private
75
+
76
+ # After save callback, confirms that redis is saved after object exists.
77
+ def update_tags_to_redis
78
+ tags_collection = @tag_list
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'redis_tags/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "redis_tags"
8
+ gem.version = RedisTags::VERSION
9
+ gem.authors = ["Elad Meidar"]
10
+ gem.email = ["elad@eizesus.com"]
11
+ gem.description = "A tagging system implemented in Redis graph"
12
+ gem.summary = "This implementation consists over 3 basic rules. keep who is tagged, keep what is tagged and keep all tags."
13
+ gem.homepage = ""
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", "2.2.2"
21
+ gem.add_development_dependency "rspec"
22
+ gem.add_development_dependency "ruby-debug"
23
+ end
@@ -0,0 +1,18 @@
1
+ class Book
2
+ include RedisTags
3
+
4
+ uses_redis_tags
5
+
6
+ def new
7
+ @id = nil
8
+ end
9
+
10
+ def id
11
+ @id
12
+ end
13
+
14
+ def save
15
+ @id = rand(1000)
16
+ self.tags_collection = RedisTags::TagList.new(self).append_multi(@tag_list)
17
+ end
18
+ end
@@ -0,0 +1,51 @@
1
+ require 'spec_helper'
2
+
3
+ describe RedisTags do
4
+ describe "#tags_list" do
5
+ it "should return a empty array for a new object" do
6
+ Book.new.tags_collection.should eql([])
7
+ end
8
+
9
+ it "should create multiple tags" do
10
+ @book = Book.new
11
+ @book.tags_collection = "elad, koko, loko"
12
+ @book.tags_collection.should eql(["elad", "koko", "loko"])
13
+ end
14
+
15
+ it "should save a new tag using <<" do
16
+ @book = Book.new
17
+ @book.tag_with "elad"
18
+ @book.tags_collection.should eql(["elad"])
19
+ end
20
+
21
+ it "should save a new tag downcased" do
22
+ @book = Book.new
23
+ @book.tag_with "ELAD"
24
+ @book.tags_collection.should eql(["elad"])
25
+ end
26
+
27
+ it "should support backward compatibility for acts_as_taggable_on_steroids (#tag_list)" do
28
+ Book.class_eval do
29
+ uses_redis_tags :acts_as_taggable_on_steroids_legacy_mode => true
30
+ end
31
+
32
+ @book = Book.new
33
+ lambda {
34
+ @book.tag_list
35
+ }.should raise_error(NoMethodError)
36
+ end
37
+
38
+ it "should update Redis tags collection with backward compatibility for acts_as_taggable_on_steroids (#tag_list)" do
39
+ Book.class_eval do
40
+ uses_redis_tags :acts_as_taggable_on_steroids_legacy_mode => true
41
+ end
42
+
43
+ @book = Book.new
44
+ lambda {
45
+ @book.tag_list
46
+ }.should raise_error(NoMethodError)
47
+
48
+ @book.tags_collection.should eql([])
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,51 @@
1
+ require 'spec_helper'
2
+
3
+ describe Tag do
4
+
5
+ before(:each) do
6
+ Book.redis_tags_engine.flushdb
7
+ end
8
+
9
+ it "should initialize a new Tag instance" do
10
+ @b = Book.new
11
+ @t = Tag.new(@b, "koksi")
12
+ @t.name.should == "koksi"
13
+ @t.owner_class.should == "book"
14
+
15
+ @t.count.should be_zero
16
+ end
17
+
18
+ it "should intersect more than 1 tag" do
19
+ @book1 = Book.new
20
+ @book2 = Book.new
21
+ #debugger
22
+ @book1.tags_collection = "elad, deddy, erez"
23
+
24
+ @book1.save
25
+ @book2.tags_collection = "elad, deddy"
26
+ @book2.save
27
+
28
+ [@book1.id, @book2.id].should =~ Tag.tagged_with(Book, {:tags => ["elad", "deddy"]}).collect(&:to_i)
29
+ end
30
+
31
+ it "should intersect 1 tag" do
32
+ @book1 = Book.new
33
+ @book2 = Book.new
34
+
35
+ @book1.tags_collection = "elad, deddy, erez"
36
+ @book1.save
37
+ @book2.tags_collection = "elad, deddy"
38
+ @book2.save
39
+
40
+ [@book1.id, @book2.id].should =~ Tag.tagged_with(Book, {:tags => ["elad"]}).collect(&:to_i)
41
+ end
42
+
43
+ it "should log autocomplete options for each tag" do
44
+ @book = Book.new
45
+ @book.tags_collection = "elad", "eli", "eliran hamelech shel ramle"
46
+ @book.save
47
+ Book.tagged_with_prefix("el").should =~ ["elad", "eli", "eliran hamelech shel ramle"]
48
+ Book.tagged_with_prefix("ela").should =~ ["elad"]
49
+ Book.tagged_with_prefix("eli").should =~ ["eli", "eliran hamelech shel ramle"]
50
+ end
51
+ end
@@ -0,0 +1,13 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'redis'
4
+ require 'ruby-debug'
5
+
6
+ require 'redis_tags' # and any other gems you need
7
+ require 'models/book'
8
+
9
+ Redis.new.flushdb
10
+
11
+ RSpec.configure do |config|
12
+
13
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: redis_tags
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Elad Meidar
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-11-05 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: redis
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - "="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 2
32
+ - 2
33
+ - 2
34
+ version: 2.2.2
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: rspec
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 3
46
+ segments:
47
+ - 0
48
+ version: "0"
49
+ type: :development
50
+ version_requirements: *id002
51
+ - !ruby/object:Gem::Dependency
52
+ name: ruby-debug
53
+ prerelease: false
54
+ requirement: &id003 !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ hash: 3
60
+ segments:
61
+ - 0
62
+ version: "0"
63
+ type: :development
64
+ version_requirements: *id003
65
+ description: A tagging system implemented in Redis graph
66
+ email:
67
+ - elad@eizesus.com
68
+ executables: []
69
+
70
+ extensions: []
71
+
72
+ extra_rdoc_files: []
73
+
74
+ files:
75
+ - .gitignore
76
+ - .rspec
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - lib/redis_tags.rb
82
+ - lib/redis_tags/tag.rb
83
+ - lib/redis_tags/tag_list.rb
84
+ - lib/redis_tags/version.rb
85
+ - redis_tags.gemspec
86
+ - spec/models/book.rb
87
+ - spec/spec/redis_tags_spec.rb
88
+ - spec/spec/tag_spec.rb
89
+ - spec/spec_helper.rb
90
+ has_rdoc: true
91
+ homepage: ""
92
+ licenses: []
93
+
94
+ post_install_message:
95
+ rdoc_options: []
96
+
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ none: false
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ hash: 3
105
+ segments:
106
+ - 0
107
+ version: "0"
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ none: false
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ hash: 3
114
+ segments:
115
+ - 0
116
+ version: "0"
117
+ requirements: []
118
+
119
+ rubyforge_project:
120
+ rubygems_version: 1.3.7
121
+ signing_key:
122
+ specification_version: 3
123
+ summary: This implementation consists over 3 basic rules. keep who is tagged, keep what is tagged and keep all tags.
124
+ test_files:
125
+ - spec/models/book.rb
126
+ - spec/spec/redis_tags_spec.rb
127
+ - spec/spec/tag_spec.rb
128
+ - spec/spec_helper.rb