Class: Unitylock::Server::Model
- Inherits:
-
Object
- Object
- Unitylock::Server::Model
- Defined in:
- lib/unitylock/server/model.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
Returns the value of attribute data.
-
#file ⇒ Object
Returns the value of attribute file.
Instance Method Summary collapse
- #create_or_update(file:) ⇒ Object
- #delete(file:) ⇒ Object
- #fetch(file:) ⇒ Object
-
#initialize(file) ⇒ Model
constructor
A new instance of Model.
- #load ⇒ Object
- #lock(file:, user:) ⇒ Object
- #save ⇒ Object
- #search ⇒ Object
- #unlock(file:, user:) ⇒ Object
Constructor Details
#initialize(file) ⇒ Model
Returns a new instance of Model.
11 12 13 14 15 |
# File 'lib/unitylock/server/model.rb', line 11 def initialize(file) @file = file File.write(@file, '{}') unless File.exists? @file load end |
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
9 10 11 |
# File 'lib/unitylock/server/model.rb', line 9 def data @data end |
#file ⇒ Object
Returns the value of attribute file.
9 10 11 |
# File 'lib/unitylock/server/model.rb', line 9 def file @file end |
Instance Method Details
#create_or_update(file:) ⇒ Object
44 45 46 47 48 |
# 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:) ⇒ Object
25 26 27 28 |
# File 'lib/unitylock/server/model.rb', line 25 def delete(file:) @data.delete(file) save end |
#fetch(file:) ⇒ Object
54 55 56 |
# File 'lib/unitylock/server/model.rb', line 54 def fetch(file:) @data[file] end |
#load ⇒ Object
17 18 19 |
# File 'lib/unitylock/server/model.rb', line 17 def load @data = JSON.parse(File.read(@file), create_additions: true) end |
#lock(file:, user:) ⇒ Object
30 31 32 33 34 35 |
# 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 ⇒ Object
21 22 23 |
# File 'lib/unitylock/server/model.rb', line 21 def save File.write(@file, JSON.generate(@data)) end |
#search ⇒ Object
50 51 52 |
# File 'lib/unitylock/server/model.rb', line 50 def search @data.map {|key,value| value} end |
#unlock(file:, user:) ⇒ Object
37 38 39 40 41 42 |
# 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 |