Class: Rbcli::State::StateStorage

Inherits:
Object
  • Object
show all
Defined in:
lib/rbcli/state_storage/common/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



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rbcli/state_storage/common/state_storage.rb', line 30

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



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

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

#[]=(key, value) ⇒ Object



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

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

#clearObject



73
74
75
76
77
# File 'lib/rbcli/state_storage/common/state_storage.rb', line 73

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

#commitObject



69
70
71
# File 'lib/rbcli/state_storage/common/state_storage.rb', line 69

def commit
  save_state if @loaded
end

#delete(key, &block) ⇒ Object



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

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

#disconnectObject



79
80
81
82
83
# File 'lib/rbcli/state_storage/common/state_storage.rb', line 79

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

#each(&block) ⇒ Object



85
86
87
88
89
# File 'lib/rbcli/state_storage/common/state_storage.rb', line 85

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

#key?(key) ⇒ Boolean



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

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

#rbclidata(key = nil) ⇒ Object

For framework’s internal use



108
109
110
111
112
# File 'lib/rbcli/state_storage/common/state_storage.rb', line 108

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

#refreshObject



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

def refresh
  if @loaded
    load_state
  else
    state_create
  end
end

#set_rbclidata(key, value) ⇒ Object



114
115
116
117
118
# File 'lib/rbcli/state_storage/common/state_storage.rb', line 114

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

#to_hObject



96
97
98
99
# File 'lib/rbcli/state_storage/common/state_storage.rb', line 96

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

#to_sObject



101
102
103
104
# File 'lib/rbcli/state_storage/common/state_storage.rb', line 101

def to_s
  state_create unless @loaded
  to_h.to_s
end