class Unitylock::Server::Model

Attributes

data[RW]
file[RW]

Public Class Methods

new(file) click to toggle source
# File lib/unitylock/server/model.rb, line 11
def initialize(file)
  @file = file
  File.write(@file, '{}') unless File.exists? @file
  load
end

Public Instance Methods

create_or_update(file:) click to toggle source
# File lib/unitylock/server/model.rb, line 44
def create_or_update(file:)
  @data[file] ||= UnityFile.create(file: file)
  @data[file].update
  save
end
delete(file:) click to toggle source
# File lib/unitylock/server/model.rb, line 25
def delete(file:)
  @data.delete(file)
  save
end
fetch(file:) click to toggle source
# File lib/unitylock/server/model.rb, line 54
def fetch(file:)
  @data[file]
end
load() click to toggle source
# File lib/unitylock/server/model.rb, line 17
def load
  @data = JSON.parse(File.read(@file), create_additions: true)
end
lock(file:, user:) click to toggle source
# File lib/unitylock/server/model.rb, line 30
def lock(file:, user:)
  raise "lock failure" unless @data.has_key?(file) && @data[file].user == nil
  @data[file] = UnityFile.create(file: file, user: user)
  save
  @data[file]
end
save() click to toggle source
# File lib/unitylock/server/model.rb, line 21
def save
  File.write(@file, JSON.generate(@data))
end
unlock(file:, user:) click to toggle source
# File lib/unitylock/server/model.rb, line 37
def unlock(file:, user:) 
  raise "unlock failure" unless @data.has_key?(file) && @data[file].user == user
  @data[file] = UnityFile.create(file: file, user: nil)
  save
  @data[file]
end