class Locker::Resource::Resource
Proper Resource
class
Attributes
id[R]
Public Class Methods
create(key)
click to toggle source
# File lib/locker/resource.rb, line 28 def self.create(key) raise 'Resource key already exists' if Resource.exists?(key) redis.sadd('resource-list', key) r = Resource.new(key) r.state = 'unlocked' r.owner_id = '' r end
delete(key)
click to toggle source
# File lib/locker/resource.rb, line 37 def self.delete(key) raise 'Unknown resource key' unless Resource.exists?(key) %w[state owner_id].each do |item| redis.del("resource:#{key}:#{item}") end redis.srem('resource-list', key) end
exists?(key)
click to toggle source
# File lib/locker/resource.rb, line 24 def self.exists?(key) redis.sismember('resource-list', key) end
list()
click to toggle source
# File lib/locker/resource.rb, line 45 def self.list redis.smembers('resource-list').sort end
new(key)
click to toggle source
# File lib/locker/resource.rb, line 19 def initialize(key) raise 'Unknown resource key' unless Resource.exists?(key) @id = key end
Public Instance Methods
lock!(owner_id)
click to toggle source
# File lib/locker/resource.rb, line 49 def lock!(owner_id) return false if state == 'locked' coord_lock.lock do self.owner_id = owner_id self.state = 'locked' end true end
locked?()
click to toggle source
# File lib/locker/resource.rb, line 67 def locked? (state == 'locked') end
owner()
click to toggle source
# File lib/locker/resource.rb, line 71 def owner return nil unless locked? Lita::User.find_by_id(owner_id.value) end
to_json()
click to toggle source
# File lib/locker/resource.rb, line 76 def to_json { id: id, state: state.value, owner_id: owner_id.value }.to_json end
unlock!()
click to toggle source
# File lib/locker/resource.rb, line 58 def unlock! return true if state == 'unlocked' coord_lock.lock do self.owner_id = '' self.state = 'unlocked' end true end