class Landrush::Store
Attributes
backing_file[RW]
Public Class Methods
config()
click to toggle source
# File lib/landrush/store.rb, line 14 def self.config @config ||= new(Server.working_dir.join('config.json')) end
hosts()
click to toggle source
# File lib/landrush/store.rb, line 10 def self.hosts @hosts ||= new(Server.working_dir.join('hosts.json')) end
new(backing_file)
click to toggle source
# File lib/landrush/store.rb, line 25 def initialize(backing_file) @backing_file = Pathname(backing_file) end
reset()
click to toggle source
# File lib/landrush/store.rb, line 18 def self.reset @config = nil @hosts = nil end
Public Instance Methods
clear!()
click to toggle source
# File lib/landrush/store.rb, line 79 def clear! with_file_lock do |file| write({}, file) end end
delete(key)
click to toggle source
# File lib/landrush/store.rb, line 42 def delete(key) with_file_lock do |file| write(current_config(file).reject { |k, v| k == key || v == key }, file) end end
each(*args, &block)
click to toggle source
# File lib/landrush/store.rb, line 36 def each(*args, &block) with_file_lock do |file| current_config(file).each(*args, &block) end end
find(search)
click to toggle source
# File lib/landrush/store.rb, line 58 def find(search) with_file_lock do |file| search = IPAddr.new(search).reverse if begin IPAddr.new(search) rescue StandardError nil end current_config(file).keys.detect do |key| key.casecmp(search) == 0 || search =~ /#{key}$/i || key =~ /^#{search}\./i end end end
get(key)
click to toggle source
# File lib/landrush/store.rb, line 73 def get(key) with_file_lock do |file| current_config(file)[key] end end
has?(key, value = nil)
click to toggle source
# File lib/landrush/store.rb, line 48 def has?(key, value = nil) with_file_lock do |file| if value.nil? current_config(file).key? key else current_config(file)[key] == value end end end
set(key, value)
click to toggle source
# File lib/landrush/store.rb, line 29 def set(key, value) with_file_lock do |file| config = current_config(file).merge(key => value) write(config, file) end end
Protected Instance Methods
current_config(file)
click to toggle source
# File lib/landrush/store.rb, line 95 def current_config(file) if backing_file.exist? begin file.rewind JSON.parse(file.read) rescue JSON::ParserError {} end else {} end end
with_file_lock() { |file| ... }
click to toggle source
# File lib/landrush/store.rb, line 87 def with_file_lock Filelock @backing_file.to_s, wait: 3 do |file| yield file end rescue Filelock::WaitTimeout raise ConfigLockError, 'Unable to lock Landrush config' end
write(config, file)
click to toggle source
# File lib/landrush/store.rb, line 108 def write(config, file) file.rewind file.truncate(0) file.write(JSON.pretty_generate(config)) file.flush end