mongo_watchable 0.0.0.pre1

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ Copyright (c) 2010 Jonathan Bell
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+
data/Rakefile ADDED
@@ -0,0 +1,34 @@
1
+ begin
2
+ GEM = "mongo_watchable"
3
+ AUTHOR = "Jonathan Bell"
4
+ EMAIL = "jonbell@spamcop.net"
5
+ SUMMARY = "A ruby gem for adding watching to mongo documents."
6
+ HOMEPAGE = "http://github.com/jonbell/mongo_watchable"
7
+
8
+ gem 'jeweler', '>= 1.0.0'
9
+ require 'jeweler'
10
+
11
+ Jeweler::Tasks.new do |s|
12
+ s.name = GEM
13
+ s.summary = SUMMARY
14
+ s.email = EMAIL
15
+ s.homepage = HOMEPAGE
16
+ s.description = SUMMARY
17
+ s.author = AUTHOR
18
+
19
+ s.require_path = 'lib'
20
+ s.files = %w(MIT-LICENSE README.textile Rakefile) + Dir.glob("{rails,lib,generators,spec}/**/*")
21
+
22
+ # Runtime dependencies: When installing Formtastic these will be checked if they are installed.
23
+ # Will be offered to install these if they are not already installed.
24
+ s.add_dependency 'mongo_mapper', '>= 0.7.0'
25
+
26
+ # Development dependencies. Not installed by default.
27
+ # Install with: sudo gem install formtastic --development
28
+ #s.add_development_dependency 'rspec-rails', '>= 1.2.6'
29
+ end
30
+
31
+ Jeweler::GemcutterTasks.new
32
+ rescue LoadError
33
+ puts "[mongo_watchable:] Jeweler - or one of its dependencies - is not available. Install it with: sudo gem install jeweler -s http://gemcutter.org"
34
+ end
@@ -0,0 +1,67 @@
1
+ module MongoWatchable
2
+ class Proxy
3
+ attr_reader :array_key
4
+ attr_reader :model
5
+ attr_reader :target_class
6
+
7
+ def initialize(model, array_key, target_class)
8
+ @model, @array_key, @target_class = model, array_key, target_class
9
+ end
10
+
11
+ def to_a
12
+ fetch_all.to_a
13
+ end
14
+
15
+ def count
16
+ array.size
17
+ end
18
+
19
+ def all(opts = {})
20
+ fetch_all
21
+ end
22
+
23
+ def each(&block)
24
+ fetch_all.each {|entry| yield entry}
25
+ end
26
+
27
+ def find(id)
28
+ return nil unless array.include?(id)
29
+ target_class.find(id)
30
+ end
31
+
32
+ def first(opts = {})
33
+ return @first ||= target_class.find(array.first) if opts.empty?
34
+ target_class.first(opts.merge(:_id.in => array))
35
+ end
36
+
37
+ def last(opts = {})
38
+ return @last ||= target_class.find(array.last) if opts.empty?
39
+ target_class.last(opts.merge(:_id.in => array))
40
+ end
41
+
42
+ alias :size :count
43
+
44
+ def << (entry)
45
+ array << entry.id
46
+ @fetch ? @fetch << entry : fetch_all
47
+ end
48
+
49
+ def delete(entry)
50
+ array.delete entry.id
51
+ @fetch ? @fetch.delete(entry) : fetch_all
52
+ end
53
+
54
+ def inspect
55
+ all.inspect
56
+ end
57
+
58
+ private
59
+ def fetch_all
60
+ @fetch ||= target_class.find(array)
61
+ end
62
+
63
+ def array
64
+ model.send(array_key)
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,34 @@
1
+ module MongoWatchable
2
+ module Watchable
3
+ def self.included(watchable)
4
+ watchable.class_eval do
5
+ MongoWatchable.watchers.each do |watcher|
6
+ key :"#{watcher.name.underscore}_watcher_ids", Array
7
+
8
+ define_method :"#{watcher.name.underscore}_watchers" do
9
+ MongoWatchable::Proxy.new(self, :"#{watcher.name.underscore}_watcher_ids", watcher)
10
+ end
11
+ end
12
+ end
13
+
14
+ MongoWatchable.watchers.each do |watcher|
15
+ watcher.class_eval do
16
+ key :"#{watchable.name.underscore}_watching_ids", Array
17
+
18
+ define_method :"#{watchable.name.underscore}_watchings" do
19
+ MongoWatchable::Proxy.new(self, :"#{watchable.name.underscore}_watching_ids", watchable)
20
+ end
21
+ end
22
+ end
23
+ MongoWatchable.watchables << watchable
24
+ end
25
+
26
+ def watchers_proxy_for(watcher)
27
+ klass = watcher.class
28
+ while klass.superclass && klass.superclass.include?(MongoWatchable::Watcher)
29
+ klass = klass.superclass
30
+ end
31
+ send("#{klass.name.underscore}_watchers")
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,95 @@
1
+ module MongoWatchable
2
+ module Watcher
3
+ def self.included(watcher)
4
+ watcher.class_eval do
5
+ MongoWatchable.watchables do |watchable|
6
+ key :"#{watchable.name.underscore}_watching_ids", Array
7
+
8
+ define_method :"#{watchable.name.underscore}_watchings" do
9
+ MongoWatchable::Proxy.new(self, :"#{watchable.name.underscore}_watching_ids", watchable)
10
+ end
11
+ end
12
+ end
13
+
14
+ MongoWatchable.watchables.each do |watchable|
15
+ watchable.class_eval do
16
+ key :"#{watcher.name.underscore}_watcher_ids", Array
17
+
18
+ define_method :"#{watcher.name.underscore}_watchers" do
19
+ MongoWatchable::Proxy.new(self, :"#{watcher.name.underscore}_watcher_ids", watcher)
20
+ end
21
+ end
22
+ end
23
+
24
+ MongoWatchable.watchers << watcher
25
+ end
26
+
27
+ def watch(watchable)
28
+ unless watching?(watchable)
29
+ watchings_proxy_for(watchable) << watchable
30
+ watchable.watchers_proxy_for(self) << self
31
+ save if watchable.save
32
+ end
33
+ end
34
+
35
+ def watch!(watchable)
36
+ raise "#{self.class.name} is already watching #{watchable.class.name}" if watching?(watchable)
37
+ watchings_proxy_for(watchable) << watchable
38
+ watchable.watchers_proxy_for(self) << self
39
+ watchable.save!
40
+ self.save!
41
+ end
42
+
43
+ def watching?(watchable)
44
+ watching_ids_for(watchable).include?(watchable.id)
45
+ end
46
+
47
+ def unwatch(watchable)
48
+ if watching?(watchable)
49
+ watchings_proxy_for(watchable).delete watchable
50
+ watchable.watchers_proxy_for(self).delete self
51
+ save if watchable.save
52
+ end
53
+ end
54
+
55
+ def unwatch!(watchable)
56
+ raise "#{self.class.name} is not watching #{watchable.class.name}" unless watching?(watchable)
57
+ watchings_proxy_for(watchable).delete watchable
58
+ watchable.watchers_proxy_for(self).delete self
59
+ watchable.save!
60
+ self.save!
61
+ end
62
+
63
+ def watching_ids_for(watchable)
64
+ klass = watchable.class
65
+ while klass.superclass && klass.superclass.include?(MongoWatchable::Watchable)
66
+ klass = klass.superclass
67
+ end
68
+ send("#{klass.name.underscore}_watching_ids")
69
+ end
70
+
71
+ def watchings_proxy_for(watchable)
72
+ klass = watchable.class
73
+ while klass.superclass && klass.superclass.include?(MongoWatchable::Watchable)
74
+ klass = klass.superclass
75
+ end
76
+ send("#{klass.name.underscore}_watchings")
77
+ end
78
+
79
+ private
80
+
81
+ def watch(watchable)
82
+ klass = watchable
83
+ while klass.superclass && klass.superclass.include?(MongoWatchable::Watchable)
84
+ klass = klass.superclass
85
+ end
86
+ end
87
+ def root_watching_class(instance)
88
+ klass = instance
89
+ while klass.superclass && klass.superclass.include?(MongoWatchable::Watchable)
90
+ klass = klass.superclass
91
+ end
92
+ klass
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,13 @@
1
+ require 'mongo_watchable/proxy'
2
+ require 'mongo_watchable/watcher'
3
+ require 'mongo_watchable/watchable'
4
+
5
+ module MongoWatchable
6
+ def self.watchables
7
+ @watchables ||= []
8
+ end
9
+
10
+ def self.watchers
11
+ @watchers ||= []
12
+ end
13
+ end
@@ -0,0 +1,51 @@
1
+ require 'rubygems'
2
+ require 'active_support'
3
+
4
+ require 'active_support/test_case'
5
+ require 'test/unit'
6
+
7
+ ENV['RAILS_ROOT'] ||= File.dirname(__FILE__) + '/../../../..'
8
+ env_rb = File.expand_path(File.join(ENV['RAILS_ROOT'], 'config/environment.rb'))
9
+
10
+ if File.exists? env_rb
11
+ require env_rb
12
+ else
13
+ require 'mongo_mapper'
14
+ $LOAD_PATH << File.expand_path(File.dirname(__FILE__) + '/../lib')
15
+ require 'mongo_watchable'
16
+ config = {'test' => {'database' => 'mongo_watchable-test'}}
17
+ MongoMapper.setup(config, 'test')
18
+ end
19
+
20
+ class ActiveSupport::TestCase
21
+ # Drop all columns after each test case.
22
+ def teardown
23
+ MongoMapper.database.collections.each do |coll|
24
+ coll.drop
25
+ end
26
+ end
27
+
28
+ # Make sure that each test case has a teardown
29
+ # method to clear the db after each test.
30
+ def inherited(base)
31
+ base.define_method teardown do
32
+ super
33
+ end
34
+ end
35
+ end
36
+
37
+ # kinda weird, but we have to do this so we can ignore the app's User class and use our own for testing
38
+ Object.send(:remove_const, :User) if Object.const_defined?(:User)
39
+
40
+ class User
41
+ include MongoMapper::Document
42
+ include MongoWatchable::Watcher
43
+ key :name, String
44
+ end
45
+
46
+ class Widget
47
+ include MongoMapper::Document
48
+ include MongoWatchable::Watchable
49
+ key :name, String
50
+ end
51
+
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongo_watchable
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: true
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 0
9
+ - pre1
10
+ version: 0.0.0.pre1
11
+ platform: ruby
12
+ authors:
13
+ - Jonathan Bell
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-03-22 00:00:00 -05:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: mongo_mapper
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ - 7
31
+ - 0
32
+ version: 0.7.0
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ description: A ruby gem for adding watching to mongo documents.
36
+ email: jonbell@spamcop.net
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files: []
42
+
43
+ files:
44
+ - MIT-LICENSE
45
+ - Rakefile
46
+ - lib/mongo_watchable.rb
47
+ - lib/mongo_watchable/proxy.rb
48
+ - lib/mongo_watchable/watchable.rb
49
+ - lib/mongo_watchable/watcher.rb
50
+ has_rdoc: true
51
+ homepage: http://github.com/jonbell/mongo_watchable
52
+ licenses: []
53
+
54
+ post_install_message:
55
+ rdoc_options:
56
+ - --charset=UTF-8
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ segments:
64
+ - 0
65
+ version: "0"
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">"
69
+ - !ruby/object:Gem::Version
70
+ segments:
71
+ - 1
72
+ - 3
73
+ - 1
74
+ version: 1.3.1
75
+ requirements: []
76
+
77
+ rubyforge_project:
78
+ rubygems_version: 1.3.6
79
+ signing_key:
80
+ specification_version: 3
81
+ summary: A ruby gem for adding watching to mongo documents.
82
+ test_files:
83
+ - test/test_helper.rb