Class: SleepRoom::Record::WriteStatus

Inherits:
Object
  • Object
show all
Defined in:
lib/sleeproom/record/write_status.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeWriteStatus

Returns a new instance of WriteStatus.



8
9
10
# File 'lib/sleeproom/record/write_status.rb', line 8

def initialize
  @queue = Async::Queue.new
end

Instance Attribute Details

#queueObject

Returns the value of attribute queue.



7
8
9
# File 'lib/sleeproom/record/write_status.rb', line 7

def queue
  @queue
end

Instance Method Details

#add(status) ⇒ Object



29
30
31
32
33
# File 'lib/sleeproom/record/write_status.rb', line 29

def add(status)
  Async do
    @queue.enqueue(status)
  end
end

#runObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/sleeproom/record/write_status.rb', line 12

def run
  Async do
    while status = @queue.dequeue
      status[:update] = Time.now
      old_status = SleepRoom.load_config(:status)
      room = status[:room]
      if old_status.find { |h| h[:room] == room }
        new_status = old_status.delete_if { |h| h[:room] == room }
        new_status.push(status)
      else
        new_status = old_status.push(status)
      end
      SleepRoom.write_config_file(:status, new_status)
    end
  end
end