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) ⇒ StateStorage

Returns a new instance of StateStorage.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rbcli/stateful_systems/state_storage.rb', line 10

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

  state_subsystem_init

  base_data = {
      data: {},
      rbcli: {}
  }

  if state_exists?
    load_state
  elsif force_creation
    create_state
    @data = base_data
    save_state
  else
    raise StandardError "State location #{@path} does not exist or can not be accessed." if @halt_on_error
    @data = base_data
  end
end

Instance Method Details

#[](key) ⇒ Object



40
41
42
# File 'lib/rbcli/stateful_systems/state_storage.rb', line 40

def [] key
  @data[:data][key.to_sym]
end

#[]=(key, value) ⇒ Object



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

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

#clearObject



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

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

#delete(key, &block) ⇒ Object



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

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

#each(&block) ⇒ Object



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

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

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/rbcli/stateful_systems/state_storage.rb', line 64

def key? key
  @data[:data].key? key.to_sym
end

#rbclidata(key = nil) ⇒ Object

For framework's internal use



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

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

#refreshObject



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

def refresh
  load_state
end

#set_rbclidata(key, value) ⇒ Object



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

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

#to_hObject



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

def to_h
  @data[:data]
end

#to_sObject



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

def to_s
  to_h.to_s
end