Class: Rbcli::State::StateStorage

Inherits:
Object
  • Object
show all
Defined in:
lib/rbcli/stateful_systems/state_storage.rb

Overview

Main State Class

Direct Known Subclasses

DynamoDBStorage, LocalStorage

Instance Method Summary collapse

Constructor Details

#initialize(path, force_creation: false, halt_on_error: true, lazy_load: true) ⇒ StateStorage

Returns a new instance of StateStorage.



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/rbcli/stateful_systems/state_storage.rb', line 10

def initialize path, force_creation: false, halt_on_error: true, lazy_load: true
	@path = path
	@force_creation = force_creation
	@halt_on_error = halt_on_error

	state_subsystem_init

	# Lazy load option
	@loaded = false
	state_create unless lazy_load
end

Instance Method Details

#[](key) ⇒ Object



29
30
31
32
# File 'lib/rbcli/stateful_systems/state_storage.rb', line 29

def [] key
	state_create unless @loaded
	@data[:data][key.to_sym]
end

#[]=(key, value) ⇒ Object



22
23
24
25
26
27
# File 'lib/rbcli/stateful_systems/state_storage.rb', line 22

def []= key, value
	state_create unless @loaded
	@data[:data][key.to_sym] = value
	save_state
	@data[:data][key.to_sym]
end

#clearObject



49
50
51
52
53
# File 'lib/rbcli/stateful_systems/state_storage.rb', line 49

def clear
	state_create unless @loaded
	@data[:data] = {}
	save_state
end

#delete(key, &block) ⇒ Object



34
35
36
37
38
39
# File 'lib/rbcli/stateful_systems/state_storage.rb', line 34

def delete key, &block
	state_create unless @loaded
	result = @data[:data].delete key.to_sym, block
	save_state
	result
end

#disconnectObject



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

def disconnect
	unlock if self.class.method_defined? :unlock
	@loaded = false
	@data[:data] = nil
end

#each(&block) ⇒ Object



61
62
63
64
65
# File 'lib/rbcli/stateful_systems/state_storage.rb', line 61

def each &block
	state_create unless @loaded
	@data[:data].each &block
	save_state
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
70
# File 'lib/rbcli/stateful_systems/state_storage.rb', line 67

def key? key
	state_create unless @loaded
	@data[:data].key? key.to_sym
end

#rbclidata(key = nil) ⇒ Object

For framework's internal use



84
85
86
87
88
# File 'lib/rbcli/stateful_systems/state_storage.rb', line 84

def rbclidata key = nil
	state_create unless @loaded
	return @data[:rbcli][key.to_sym] unless key.nil?
	@data[:rbcli]
end

#refreshObject



41
42
43
44
45
46
47
# File 'lib/rbcli/stateful_systems/state_storage.rb', line 41

def refresh
	if @loaded
		load_state
	else
		state_create
	end
end

#set_rbclidata(key, value) ⇒ Object



90
91
92
93
94
# File 'lib/rbcli/stateful_systems/state_storage.rb', line 90

def set_rbclidata key, value
	state_create unless @loaded
	@data[:rbcli][key.to_sym] = value
	save_state
end

#to_hObject



72
73
74
75
# File 'lib/rbcli/stateful_systems/state_storage.rb', line 72

def to_h
	state_create unless @loaded
	@data[:data]
end

#to_sObject



77
78
79
80
# File 'lib/rbcli/stateful_systems/state_storage.rb', line 77

def to_s
	state_create unless @loaded
	to_h.to_s
end