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



53
54
55
56
57
# File 'lib/rbcli/stateful_systems/state_storage.rb', line 53

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

#commitObject



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

def commit
  save_state if @loaded
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



59
60
61
62
63
# File 'lib/rbcli/stateful_systems/state_storage.rb', line 59

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

#each(&block) ⇒ Object



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

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

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#rbclidata(key = nil) ⇒ Object

For framework's internal use



88
89
90
91
92
# File 'lib/rbcli/stateful_systems/state_storage.rb', line 88

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



94
95
96
97
98
# File 'lib/rbcli/stateful_systems/state_storage.rb', line 94

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

#to_hObject



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

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

#to_sObject



81
82
83
84
# File 'lib/rbcli/stateful_systems/state_storage.rb', line 81

def to_s
  state_create unless @loaded
  to_h.to_s
end