ar_lock 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: db283404486b781cd5d9208bcb164dcbec5ee3ec
4
+ data.tar.gz: c1dc98c45377586d05c2ec45a3f4cf75f65dd0b4
5
+ SHA512:
6
+ metadata.gz: 204b456b525ac84293b34074b7eb2585d9c911e223d9208596d8dccc8eeb234fdfe07b74e2d5ab721e39dbdd53942b6e5cfb325319ba99306944aaab3e246de5
7
+ data.tar.gz: 66ce042d815aabb14699acf96467c47b94e369499a2a2707c3d2b4eb73035b9e3a983249dd47971f37ee2278a25062b1ece01d88f26d69e3bd63e776f206d05b
@@ -1,4 +1,5 @@
1
1
  = ArLock for Rails3
2
+ {<img src="https://codeclimate.com/github/InWork/ar_lock.png" />}[https://codeclimate.com/github/InWork/ar_lock]
2
3
 
3
4
  This Rails3 Gem implements an atomic locking model based on ActiveRecord.
4
5
 
@@ -6,46 +7,51 @@ This Rails3 Gem implements an atomic locking model based on ActiveRecord.
6
7
 
7
8
  Inside your Gemfile:
8
9
 
9
- gem "ar_record"
10
+ gem "ar_lock"
10
11
 
11
12
  and then run:
12
13
 
13
14
  * bundle install
14
15
 
15
- === Gem Dependencies
16
-
17
- Please check if all those requirements are satisfied on your environment.
18
-
19
- * rails >= 3.0.0
20
-
21
16
  === Inside your Application:
22
17
 
23
18
  Geting a lock is always an atomic operation. If you try to get more than one lock (by specifying an array), this is also executed atomic.
24
19
 
25
20
  ==== Get lock(s):
21
+ Get one lock:
26
22
  Lock.get :lock_name
27
- or
23
+
24
+ Get multiple locks as one atomic operation:
28
25
  Lock.get [:lock_name1, :lock_name2]
29
26
 
30
27
  optional you can specify a value (for example the user, which acquired the lock):
31
- Lock.get :lock_name, :value => 'value'
32
- or
33
- Lock.get [:lock_name1, :lock_name2], :value => 'value'
28
+ Lock.get :lock_name, value: 'value'
34
29
 
35
30
  You can let Lock.get block, until the lock was acquired:
36
- Lock.get :lock_name, :blocking => true
37
- or
38
- Lock.get [:lock_name1, :lock_name2], :blocking => true
31
+ Lock.get :lock_name, blocking: true
32
+
33
+ If you do not want a lock which was already acquired will block your code:
34
+ Lock.get :lock_name, blocking: true, successful_if: :value_matches
35
+
36
+ It is possible to force a lock. Values of already acquired locks will be overwriten by the given value.
37
+ Lock.get :lock_name, value: 'value', force: true
39
38
 
40
39
  ==== Release lock(s):
40
+ Release one Lock:
41
41
  Lock.release :lock_name
42
- or
42
+
43
+ Release multiple locks:
43
44
  Lock.release [:lock_name1, :lock_name2]
44
45
 
45
46
  Only release a lock, if the value matches:
46
- Lock.release :lock_name, :value => 'value'
47
- or
48
- Lock.release [:lock_name1, :lock_name2], :value => 'value'
47
+ Lock.release :lock_name, value: 'value'
48
+
49
+ You can also release all locks at once:
50
+ Lock.release_all
51
+
52
+ ==== Get the value of a lock:
53
+ To read the value of a lock:
54
+ Lock.get_value :lock_name
49
55
 
50
56
  == Generators
51
57
 
@@ -61,6 +67,8 @@ This will create a database migration:
61
67
 
62
68
  TIMESTAMP_add_ar_lock_table.rb
63
69
 
64
- == Copyright
70
+ == License
71
+ This project is licenced under the MIT license.
65
72
 
66
- Copyright (c) 2011 Philip Kurmann. See LICENSE for details.
73
+ == Author
74
+ Philip Kurmann (philip (at) kman.ch)
@@ -2,8 +2,9 @@ class Lock < ActiveRecord::Base
2
2
 
3
3
  # Get an atomic lock if value matches or is nil. Block until lock was successful if args[:blocking] was set to true
4
4
  def self.get name, args = {}
5
- poll_time = args[:poll_time] || 10
6
- until (lock = get_lock_for(name, args)) || args[:blocking] != true do
5
+ poll_time = args[:poll_time] || 10
6
+ start_time = Time.now
7
+ until (lock = get_lock_for(name, args)) || args[:blocking] != true || (args[:timeout] && (Time.now - start_time) > args[:timeout]) do
7
8
  sleep poll_time
8
9
  end
9
10
  lock
@@ -69,7 +70,7 @@ private
69
70
  # Get an atomic lock if value matches or is nil
70
71
  def self.get_lock_for names, args = {}
71
72
  successful_if_value_matches = args[:successful_if].to_s == 'value_matches'
72
- successful = false
73
+ successful = false
73
74
 
74
75
  if names
75
76
  # If names is a single string, transorm it to an array
@@ -108,7 +109,7 @@ private
108
109
  end
109
110
  end
110
111
 
111
- # Wait some secound inside the transaction for debuging and testing purposes
112
+ # Wait some seconds inside the transaction for debuging and testing purposes
112
113
  sleep args[:debug_wait].to_i if args[:debug_wait]
113
114
  end
114
115
  end
@@ -1,3 +1,3 @@
1
1
  module ArLock
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,15 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ar_lock
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
5
- prerelease:
4
+ version: 0.2.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Philip Kurmann
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-07-19 00:00:00.000000000 Z
11
+ date: 2015-01-26 00:00:00.000000000 Z
13
12
  dependencies: []
14
13
  description: Atomic lock model which stores its locks in the database.
15
14
  email:
@@ -18,7 +17,7 @@ executables: []
18
17
  extensions: []
19
18
  extra_rdoc_files: []
20
19
  files:
21
- - .gitignore
20
+ - ".gitignore"
22
21
  - Gemfile
23
22
  - LICENSE
24
23
  - README.rdoc
@@ -31,26 +30,25 @@ files:
31
30
  - lib/generators/ar_lock/migration/templates/migration.rb
32
31
  homepage: ''
33
32
  licenses: []
33
+ metadata: {}
34
34
  post_install_message:
35
35
  rdoc_options: []
36
36
  require_paths:
37
37
  - lib
38
38
  required_ruby_version: !ruby/object:Gem::Requirement
39
- none: false
40
39
  requirements:
41
- - - ! '>='
40
+ - - ">="
42
41
  - !ruby/object:Gem::Version
43
42
  version: '0'
44
43
  required_rubygems_version: !ruby/object:Gem::Requirement
45
- none: false
46
44
  requirements:
47
- - - ! '>='
45
+ - - ">="
48
46
  - !ruby/object:Gem::Version
49
47
  version: '0'
50
48
  requirements: []
51
49
  rubyforge_project: ar_lock
52
- rubygems_version: 1.8.24
50
+ rubygems_version: 2.2.2
53
51
  signing_key:
54
- specification_version: 3
52
+ specification_version: 4
55
53
  summary: Atomic lock model implemented by Active Record
56
54
  test_files: []