Class: Editstore::ObjectLock

Inherits:
Connection
  • Object
show all
Defined in:
app/models/editstore/object_lock.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.all_lockedObject



25
26
27
# File 'app/models/editstore/object_lock.rb', line 25

def self.all_locked
  self.where('locked IS NOT NULL')
end

.all_unlockedObject



29
30
31
# File 'app/models/editstore/object_lock.rb', line 29

def self.all_unlocked
  self.where(:locked=>nil)
end

.get_pid(pid) ⇒ Object



21
22
23
# File 'app/models/editstore/object_lock.rb', line 21

def self.get_pid(pid)
  pid.start_with?('druid:') ? pid : "druid:#{pid}"
end

.lock(pid) ⇒ Object

some convenience class level methods for operating on a specific druid



34
35
36
37
38
39
40
41
42
# File 'app/models/editstore/object_lock.rb', line 34

def self.lock(pid)
  status=self.find_by_druid(self.get_pid(pid))
  if status # this druid already exists in the table
    return status.lock
  else # this druid does not exist in the table, create it and lock
     self.create(:druid=>self.get_pid(pid),:locked=>Time.now)
     return true
  end
end

.locked?(pid) ⇒ Boolean

check to see if this druid is locked

Returns:

  • (Boolean)


54
55
56
57
# File 'app/models/editstore/object_lock.rb', line 54

def self.locked?(pid)
  status=self.find_by_druid(self.get_pid(pid))
  status.nil? ? false : status.locked?
end

.pruneObject



13
14
15
# File 'app/models/editstore/object_lock.rb', line 13

def self.prune
  self.where(:locked=>nil).where('updated_at < ?',1.month.ago).each {|obj| obj.destroy}  # anything that is not locked older than 1 month
end

.prune_allObject

THESE REALLY BELONG IN THE CONTROLLER and are only necessary for mass assignment def objectlock_params

params.require(:objectlock).permit(:locked, :druid)

end



9
10
11
# File 'app/models/editstore/object_lock.rb', line 9

def self.prune_all
  self.where(:locked=>nil).destroy_all # anything that is not locked
end

.unlock(pid) ⇒ Object



44
45
46
47
48
49
50
51
# File 'app/models/editstore/object_lock.rb', line 44

def self.unlock(pid)
 status=self.find_by_druid(self.get_pid(pid))
 if status # this druid already exists in the table
     return status.unlock
  else
    return false # druid doesn't exist, can't unlock
  end
end

.unlock_allObject



17
18
19
# File 'app/models/editstore/object_lock.rb', line 17

def self.unlock_all
  self.all_locked.each {|obj| obj.unlock} # unlock anything that is locked
end

Instance Method Details

#lockObject

lock this object



69
70
71
72
73
74
75
76
77
# File 'app/models/editstore/object_lock.rb', line 69

def lock
   if unlocked?
     self.locked = Time.now
     save
     return true # lock at the current time
  else
    return false # already locked
  end
end

#locked?Boolean

object level methods for performing actions

Returns:

  • (Boolean)


60
61
62
# File 'app/models/editstore/object_lock.rb', line 60

def locked?
  !unlocked?
end

#unlockObject

unlock this object



80
81
82
83
84
85
86
87
88
# File 'app/models/editstore/object_lock.rb', line 80

def unlock
  if locked?
     self.locked = nil
     save
     return true # unlock it
  else
    return false # already unlocked
  end
end

#unlocked?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'app/models/editstore/object_lock.rb', line 64

def unlocked?
  locked == nil
end