mongoid-lock 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ gem 'mongoid'
7
+
8
+ # Add dependencies to develop your gem here.
9
+ # Include everything needed to run rake, tests, features, etc.
10
+ group :development do
11
+ gem "shoulda", ">= 0"
12
+ gem "bundler", "~> 1.0.0"
13
+ gem "jeweler", "~> 1.6.4"
14
+ gem "rcov", ">= 0"
15
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 James Petty
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.
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = mongoid-lock
2
+
3
+ Mongoid lock is a module that allows model instances to behave as synchronized objects.
4
+
5
+ == Contributing to mongoid-lock
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
9
+ * Fork the project
10
+ * Start a feature/bugfix branch
11
+ * Commit and push until you are happy with your contribution
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2012 James Petty. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "mongoid-lock"
18
+ gem.homepage = "http://github.com/pettyjamesm/mongoid-lock"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Locks for Mongoid Instances}
21
+ gem.description = %Q{Adds synchronized lock-like behavior to a mongoid model instance}
22
+ gem.email = "jp@jibe.com"
23
+ gem.authors = ["James Petty"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rake/testtask'
29
+ Rake::TestTask.new(:test) do |test|
30
+ test.libs << 'lib' << 'test'
31
+ test.pattern = 'test/**/test_*.rb'
32
+ test.verbose = true
33
+ end
34
+
35
+ require 'rcov/rcovtask'
36
+ Rcov::RcovTask.new do |test|
37
+ test.libs << 'test'
38
+ test.pattern = 'test/**/test_*.rb'
39
+ test.verbose = true
40
+ test.rcov_opts << '--exclude "gems/*"'
41
+ end
42
+
43
+ task :default => :test
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "mongoid-lock #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,49 @@
1
+ require 'socket'
2
+ module Mongoid
3
+ module Lock
4
+
5
+ def reset_lock!
6
+ self.set(:lock_used_by, nil)
7
+ end
8
+
9
+ def lock_acquire
10
+ local = "#{Socket.hostname}:#{Process.pid}"
11
+ ident = self.lock_used_by
12
+ if (ident and ident != local)
13
+ raise Mongoid::Lock::UnsynchronizedAccess.new(ident)
14
+ else
15
+ self.set(:lock_used_by, local)
16
+ end
17
+ end
18
+
19
+ def lock_release
20
+ local = "#{Socket.hostname}:#{Process.pid}"
21
+ ident = self.lock_used_by
22
+ if (ident and ident == local)
23
+ self.reset_lock!
24
+ else
25
+ raise Mongoid::Lock::UnsynchronizedAccess.new(ident)
26
+ end
27
+ end
28
+
29
+ def synchronized(&block)
30
+ self.lock_acquire
31
+ begin
32
+ block.call()
33
+ ensure
34
+ self.lock_release
35
+ end
36
+ end
37
+
38
+ def try_synchronized(&try_block)
39
+ begin
40
+ self.synchronized do
41
+ try_block.call()
42
+ end
43
+ return true
44
+ rescue Mongoid::Lock::UnsynchronizedAccess => ua
45
+ return false
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,33 @@
1
+ require 'mongoid/lock/synch_methods'
2
+ module Mongoid
3
+ module Lock
4
+ #------------------------------------------------------------------------------
5
+ # => Embedded class for Unsynchronized use Exceptions
6
+ #------------------------------------------------------------------------------
7
+ class UnsynchronizedAccess < StandardError
8
+ attr_reader :used_by
9
+ def initialize(used_by, *args)
10
+ @used_by = used_by
11
+ super(*args)
12
+ end
13
+ end
14
+ #------------------------------------------------------------------------------
15
+ # => Volatile Instance Attribute Accessor
16
+ #------------------------------------------------------------------------------
17
+ def lock_used_by
18
+ self.save
19
+ self.reload
20
+ return super
21
+ end
22
+ #------------------------------------------------------------------------------
23
+ # => Module inclusion hook
24
+ #------------------------------------------------------------------------------
25
+ def self.included(base)
26
+ super if defined?(super)
27
+ base.class_eval do
28
+ # => Add special fields for use
29
+ field :lock_used_by, :type => String, :default => nil
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,19 @@
1
+ module Mongoid
2
+ module Lock
3
+ def self.load_requirements
4
+ require 'mongoid/lock'
5
+ end
6
+ end
7
+ end
8
+
9
+ if (defined?(Rails) and Rails.version.to_i >= 3)
10
+ require 'rails'
11
+ class MongoidLockRailtie < Rails::Railtie
12
+ railtie_name :"mongoid-lock"
13
+ initializer "mongoid-lock.load_requirements" do
14
+ Mongoid::Lock::load_requirements
15
+ end
16
+ end
17
+ else
18
+ Mongoid::Lock::load_requirements
19
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'mongoid-lock'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestMongoidLock < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid-lock
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - James Petty
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-03-26 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: mongoid
16
+ requirement: &24538800 !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: *24538800
25
+ - !ruby/object:Gem::Dependency
26
+ name: shoulda
27
+ requirement: &24536860 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *24536860
36
+ - !ruby/object:Gem::Dependency
37
+ name: bundler
38
+ requirement: &24627640 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 1.0.0
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *24627640
47
+ - !ruby/object:Gem::Dependency
48
+ name: jeweler
49
+ requirement: &24625680 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 1.6.4
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *24625680
58
+ - !ruby/object:Gem::Dependency
59
+ name: rcov
60
+ requirement: &24624900 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *24624900
69
+ description: Adds synchronized lock-like behavior to a mongoid model instance
70
+ email: jp@jibe.com
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files:
74
+ - LICENSE.txt
75
+ - README.rdoc
76
+ files:
77
+ - .document
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.rdoc
81
+ - Rakefile
82
+ - VERSION
83
+ - lib/mongoid-lock.rb
84
+ - lib/mongoid/lock.rb
85
+ - lib/mongoid/lock/synch_methods.rb
86
+ - test/helper.rb
87
+ - test/test_mongoid-lock.rb
88
+ homepage: http://github.com/pettyjamesm/mongoid-lock
89
+ licenses:
90
+ - MIT
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ! '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ segments:
102
+ - 0
103
+ hash: 4402273842210052622
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 1.8.10
113
+ signing_key:
114
+ specification_version: 3
115
+ summary: Locks for Mongoid Instances
116
+ test_files: []