Class: SimpleStatsStore::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_stats_store/server.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Server

Returns a new instance of Server.



3
4
5
6
7
# File 'lib/simple_stats_store/server.rb', line 3

def initialize(options)
  @data_dump = options[:data_dump]
  @models = options[:models]
  @name = options[:name] || $0
end

Instance Method Details

#run(&block) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/simple_stats_store/server.rb', line 28

def run(&block)
  Process.fork do
    $0 = @name
    loop do
      self.scan
      yield if block_given?
      sleep 0.1
    end
  end
end

#scanObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/simple_stats_store/server.rb', line 9

def scan
  @data_dump.each do |stats|
    lines = stats.split("\n")
    if lines.shift != '---' or lines.pop != '---'
      puts "Corrupt statistics"
      return false
    end

    model = lines.shift.strip
    data = {}
    lines.each do |l|
      k, v = l.split(/:/, 2)
      data[k.strip.to_sym] = v.strip
    end

    @models[model.to_sym].create(data)
  end
end