This repository has been archived on 2020-10-16. You can view files and clone it, but cannot push or open issues or pull requests.
counter/app/models/cluster.rb

41 lines
609 B
Ruby
Raw Normal View History

2020-05-23 03:54:29 +00:00
# frozen_string_literal: true
require 'securerandom'
# A collective for a tally
class Cluster
attr_reader :count, :name, :status, :uuid
@@clusters = {}
def initialize(name)
@uuid = SecureRandom.uuid
@name = name
@count = 0
@status = :open
@mutex = Mutex.new
@@clusters[@uuid] = self
end
def increment(var = 1)
@mutex.synchronize do
@count += var
end
end
def decrement(var = 1)
@mutex.synchronize do
@count -= var
end
end
class << self
def get(uuid)
@@clusters[uuid]
end
def list
@@clusters
end
end
end