Class: DAV4Rack::FileResourceLock
- Inherits:
-
Object
- Object
- DAV4Rack::FileResourceLock
- Defined in:
- lib/dav4rack/file_resource_lock.rb
Instance Attribute Summary collapse
-
#created_at ⇒ Object
readonly
Returns the value of attribute created_at.
-
#depth ⇒ Object
Returns the value of attribute depth.
-
#kind ⇒ Object
Returns the value of attribute kind.
-
#owner ⇒ Object
Returns the value of attribute owner.
-
#path ⇒ Object
Returns the value of attribute path.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
-
#scope ⇒ Object
Returns the value of attribute scope.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
-
#token ⇒ Object
Returns the value of attribute token.
-
#user ⇒ Object
Returns the value of attribute user.
Class Method Summary collapse
- .explicit_locks(path, croot, args = {}) ⇒ Object
- .explicitly_locked?(path, croot = nil) ⇒ Boolean
- .find_by_path(path, croot = nil) ⇒ Object
- .find_by_token(token, croot = nil) ⇒ Object
- .generate(path, user, token, croot) ⇒ Object
- .implicitly_locked?(path, croot = nil) ⇒ Boolean
- .init_pstore(croot) ⇒ Object
- .root ⇒ Object
- .root=(path) ⇒ Object
Instance Method Summary collapse
- #destroy ⇒ Object
-
#initialize(args = {}) ⇒ FileResourceLock
constructor
A new instance of FileResourceLock.
- #owner?(user) ⇒ Boolean
- #reload ⇒ Object
- #remaining_timeout ⇒ Object
- #save ⇒ Object
Constructor Details
#initialize(args = {}) ⇒ FileResourceLock
Returns a new instance of FileResourceLock.
85 86 87 88 89 90 91 92 93 94 |
# File 'lib/dav4rack/file_resource_lock.rb', line 85 def initialize(args={}) @path = args[:path] @root = args[:root] @owner = args[:owner] @store = init_pstore(@root) @max_timeout = args[:max_timeout] || 86400 @default_timeout = args[:max_timeout] || 60 load_if_exists! @new_record = true if token.nil? end |
Instance Attribute Details
#created_at ⇒ Object (readonly)
Returns the value of attribute created_at.
13 14 15 |
# File 'lib/dav4rack/file_resource_lock.rb', line 13 def created_at @created_at end |
#depth ⇒ Object
Returns the value of attribute depth.
8 9 10 |
# File 'lib/dav4rack/file_resource_lock.rb', line 8 def depth @depth end |
#kind ⇒ Object
Returns the value of attribute kind.
11 12 13 |
# File 'lib/dav4rack/file_resource_lock.rb', line 11 def kind @kind end |
#owner ⇒ Object
Returns the value of attribute owner.
12 13 14 |
# File 'lib/dav4rack/file_resource_lock.rb', line 12 def owner @owner end |
#path ⇒ Object
Returns the value of attribute path.
5 6 7 |
# File 'lib/dav4rack/file_resource_lock.rb', line 5 def path @path end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
14 15 16 |
# File 'lib/dav4rack/file_resource_lock.rb', line 14 def root @root end |
#scope ⇒ Object
Returns the value of attribute scope.
10 11 12 |
# File 'lib/dav4rack/file_resource_lock.rb', line 10 def scope @scope end |
#timeout ⇒ Object
Returns the value of attribute timeout.
7 8 9 |
# File 'lib/dav4rack/file_resource_lock.rb', line 7 def timeout @timeout end |
#token ⇒ Object
Returns the value of attribute token.
6 7 8 |
# File 'lib/dav4rack/file_resource_lock.rb', line 6 def token @token end |
#user ⇒ Object
Returns the value of attribute user.
9 10 11 |
# File 'lib/dav4rack/file_resource_lock.rb', line 9 def user @user end |
Class Method Details
.explicit_locks(path, croot, args = {}) ⇒ Object
33 34 |
# File 'lib/dav4rack/file_resource_lock.rb', line 33 def explicit_locks(path, croot, args={}) end |
.explicitly_locked?(path, croot = nil) ⇒ Boolean
17 18 19 20 21 22 |
# File 'lib/dav4rack/file_resource_lock.rb', line 17 def explicitly_locked?(path, croot=nil) store = init_pstore(croot) !!store.transaction(true){ store[:paths][path] } end |
.find_by_path(path, croot = nil) ⇒ Object
36 37 38 39 |
# File 'lib/dav4rack/file_resource_lock.rb', line 36 def find_by_path(path, croot=nil) lock = self.class.new(:path => path, :root => croot) lock.token.nil? ? nil : lock end |
.find_by_token(token, croot = nil) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/dav4rack/file_resource_lock.rb', line 41 def find_by_token(token, croot=nil) store = init_pstore(croot) struct = store.transaction(true){ store[:tokens][token] } if(tok) self.class.new(:path => struct[:path], :root => croot) else nil end end |
.generate(path, user, token, croot) ⇒ Object
53 54 55 56 57 58 59 60 |
# File 'lib/dav4rack/file_resource_lock.rb', line 53 def generate(path, user, token, croot) lock = self.new(:root => croot) lock.user = user lock.path = path lock.token = token lock.save lock end |
.implicitly_locked?(path, croot = nil) ⇒ Boolean
24 25 26 27 28 29 30 31 |
# File 'lib/dav4rack/file_resource_lock.rb', line 24 def implicitly_locked?(path, croot=nil) store = init_pstore(croot) !!store.transaction(true){ store[:paths].keys.detect do |check| check.start_with?(path) end } end |
.init_pstore(croot) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/dav4rack/file_resource_lock.rb', line 70 def init_pstore(croot) path = File.join(croot, '.attribs', 'locks.pstore') FileUtils.mkdir_p(File.dirname(path)) unless File.directory?(File.dirname(path)) store = IS_18 ? PStore.new(path) : PStore.new(path, true) store.transaction do unless(store[:paths]) store[:paths] = {} store[:tokens] = {} store.commit end end store end |
.root ⇒ Object
66 67 68 |
# File 'lib/dav4rack/file_resource_lock.rb', line 66 def root @root || '/tmp/dav4rack' end |
.root=(path) ⇒ Object
62 63 64 |
# File 'lib/dav4rack/file_resource_lock.rb', line 62 def root=(path) @root = path end |
Instance Method Details
#destroy ⇒ Object
128 129 130 131 132 133 134 135 |
# File 'lib/dav4rack/file_resource_lock.rb', line 128 def destroy @store.transaction do @store[:paths].delete(path) @store[:tokens].delete(token) @store.commit end nil end |
#owner?(user) ⇒ Boolean
96 97 98 |
# File 'lib/dav4rack/file_resource_lock.rb', line 96 def owner?(user) user == owner end |
#reload ⇒ Object
100 101 102 103 |
# File 'lib/dav4rack/file_resource_lock.rb', line 100 def reload load_if_exists self end |
#remaining_timeout ⇒ Object
105 106 107 108 |
# File 'lib/dav4rack/file_resource_lock.rb', line 105 def remaining_timeout t = timeout.to_i - (Time.now.to_i - created_at.to_i) t < 0 ? 0 : t end |
#save ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/dav4rack/file_resource_lock.rb', line 110 def save struct = { :path => path, :token => token, :timeout => timeout, :depth => depth, :created_at => Time.now, :owner => owner } @store.transaction do @store[:paths][path] = struct @store[:tokens][token] = struct @store.commit end @new_record = false self end |