Class: Rbcli::State::LocalStorage

Inherits:
StateStorage show all
Defined in:
lib/rbcli/state_storage/localstate.rb

Defined Under Namespace

Classes: LocalStateError

Instance Method Summary collapse

Methods inherited from StateStorage

#[], #[]=, #clear, #commit, #delete, #disconnect, #each, #initialize, #key?, #rbclidata, #refresh, #set_rbclidata, #to_h, #to_s

Constructor Details

This class inherits a constructor from Rbcli::State::StateStorage

Instance Method Details

#create_stateObject



45
46
47
48
49
50
51
52
# File 'lib/rbcli/state_storage/localstate.rb', line 45

def create_state
	begin
		FileUtils.mkdir_p File.dirname(@path)
		FileUtils.touch @path
	rescue Errno::EACCES => e
		error "Can not create file #{@path}. Please make sure the directory is writeable." if @halt_on_error
	end
end

#error(text) ⇒ Object



70
71
72
# File 'lib/rbcli/state_storage/localstate.rb', line 70

def error text
	raise LocalStateError.new "Error accessing local state: #{text}"
end

#load_stateObject



54
55
56
57
58
59
60
# File 'lib/rbcli/state_storage/localstate.rb', line 54

def load_state
	begin
		@data = JSON.parse(File.read(@path)).deep_symbolize!
	rescue Errno::ENOENT, Errno::EACCES => e
		error "Can not read from file #{@path}. Please make sure the file exists and is readable." if @halt_on_error
	end
end

#save_stateObject



62
63
64
65
66
67
68
# File 'lib/rbcli/state_storage/localstate.rb', line 62

def save_state
	begin
		File.write @path, JSON.dump(@data)
	rescue Errno::ENOENT, Errno::EACCES => e
		error "Can not write to file #{@path}. Please make sure the file exists and is writeable." if @halt_on_error
	end
end

#state_exists?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/rbcli/state_storage/localstate.rb', line 41

def state_exists?
	File.exists? @path
end

#state_subsystem_initObject



37
38
39
# File 'lib/rbcli/state_storage/localstate.rb', line 37

def state_subsystem_init
	@path = File.expand_path @path
end