unison-watch/lib/unison/config.rb

100 lines
1.7 KiB
Ruby
Raw Normal View History

2012-05-07 22:21:03 +00:00
require 'yaml'
require 'atomic'
module Unison
class Config
2012-05-09 00:11:35 +00:00
DEFAULT_TIME_BETWEEN_CHECKS = 60
2012-05-07 22:21:03 +00:00
def self.ensure(file)
if !File.file?(file)
File.open(file, 'wb') { |fh| fh.print YAML.dump(skel_data) }
end
new(file)
end
def self.skel_data
{
'profiles' => [],
'time_between_checks' => DEFAULT_TIME_BETWEEN_CHECKS,
'unison_binary' => '/usr/bin/unison'
}
2012-05-07 22:21:03 +00:00
end
def set_profile(profile, is_set)
@data.update do |d|
if is_set
d['profiles'] << profile
else
d['profiles'].delete(profile)
end
d['profiles'].uniq!
d
end
save
end
def on_update(&block)
2012-05-09 00:11:35 +00:00
@on_update ||= []
@on_update << block
2012-05-07 22:21:03 +00:00
end
def initialize(file)
@file = file
@data = Atomic.new(nil)
end
def active?(profile)
profiles.include?(profile)
end
def profiles
data['profiles']
end
2012-05-09 00:11:35 +00:00
def time_between_checks
data['time_between_checks']
end
def time_between_checks=(time)
time = DEFAULT_TIME_BETWEEN_CHECKS if time <= 10
data['time_between_checks'] = time
save
end
def unison_binary
data['unison_binary']
end
def unison_binary=(binary)
data['unison_binary'] = binary
save
end
2012-05-07 22:21:03 +00:00
def data
@data.update { |d| d || YAML.load_file(@file) }
@data.value
end
def active_profiles?
!profiles.empty?
end
def save
@data.update do |d|
File.open(@file, 'wb') { |fh| fh.print YAML.dump(d) }
d
end
2012-05-09 00:11:35 +00:00
if @on_update
@on_update.each(&:call)
end
2012-05-07 22:21:03 +00:00
end
end
end