Class: Rbcli::State::LocalStorage

Inherits:
StateStorage show all
Defined in:
lib/rbcli/stateful_systems/storagetypes/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_state ⇒ Object



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

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



78
79
80
# File 'lib/rbcli/stateful_systems/storagetypes/localstate.rb', line 78

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

#load_state ⇒ Object



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

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_state ⇒ Object



70
71
72
73
74
75
76
# File 'lib/rbcli/stateful_systems/storagetypes/localstate.rb', line 70

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:



49
50
51
# File 'lib/rbcli/stateful_systems/storagetypes/localstate.rb', line 49

def state_exists?
	File.exists? @path
end

#state_subsystem_init ⇒ Object



45
46
47
# File 'lib/rbcli/stateful_systems/storagetypes/localstate.rb', line 45

def state_subsystem_init
	@path = File.expand_path @path
end