Class: Rbcli::LocalState

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

Overview

Local State Class

Instance Method Summary collapse

Constructor Details

#initialize(path, force_creation: false, ignore_file_errors: false) ⇒ LocalState

Returns a new instance of LocalState.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rbcli/stateful_systems/localstorage/localstate.rb', line 24

def initialize path, force_creation: false, ignore_file_errors: false
  @path = File.expand_path path
  @ignore_file_errors = ignore_file_errors

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

  if File.exist? @path
    load
  elsif force_creation
    create_file
    @data = base_data
    save
  else
    error "File #{@path} does not exist." unless @ignore_file_errors
    @data = base_data
  end
end

Instance Method Details

#[](key) ⇒ Object



51
52
53
# File 'lib/rbcli/stateful_systems/localstorage/localstate.rb', line 51

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

#[]=(key, value) ⇒ Object



45
46
47
48
49
# File 'lib/rbcli/stateful_systems/localstorage/localstate.rb', line 45

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

#clearObject



61
62
63
64
# File 'lib/rbcli/stateful_systems/localstorage/localstate.rb', line 61

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

#delete(key, &block) ⇒ Object



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

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

#each(&block) ⇒ Object



66
67
68
69
# File 'lib/rbcli/stateful_systems/localstorage/localstate.rb', line 66

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

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/rbcli/stateful_systems/localstorage/localstate.rb', line 71

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

#rbclidata(key = nil) ⇒ Object

For internal use



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

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

#set_rbclidata(key, value) ⇒ Object



86
87
88
89
# File 'lib/rbcli/stateful_systems/localstorage/localstate.rb', line 86

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

#to_hObject



75
76
77
# File 'lib/rbcli/stateful_systems/localstorage/localstate.rb', line 75

def to_h
  @data[:data]
end