Class: Riemann::Dash::BrowserConfig::File

Inherits:
Object
  • Object
show all
Defined in:
lib/riemann/dash/browser_config/file.rb

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ File

Returns a new instance of File.



5
6
7
# File 'lib/riemann/dash/browser_config/file.rb', line 5

def initialize(path)
  @path = path
end

Instance Method Details

#readObject



9
10
11
12
13
14
15
16
17
18
# File 'lib/riemann/dash/browser_config/file.rb', line 9

def read
  if ::File.exists? @path
    ::File.open(@path, 'r') do |f|
      f.flock ::File::LOCK_SH
      f.read
    end
  else
    MultiJson.encode({})
  end
end

#update(update) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/riemann/dash/browser_config/file.rb', line 20

def update(update)
  update = MultiJson.decode update

  # Read old config
  old = MultiJson.decode read

  new = Riemann::Dash::BrowserConfig.merge_configs update, old

  # Save new config
  FileUtils.mkdir_p ::File.dirname(@path)
  begin
    ::File.open(@path, ::File::RDWR|::File::CREAT, 0644) do |f|
      f.flock ::File::LOCK_EX
      f.write(MultiJson.encode(new, :pretty => true))
      f.flush
      f.truncate f.pos
    end
  end
end