Class: DevSuite::Utils::Store::Driver::File

Inherits:
Base show all
Defined in:
lib/dev_suite/utils/store/driver/file.rb

Instance Method Summary collapse

Methods inherited from Construct::Component::Base

component_key

Constructor Details

#initialize(**options) ⇒ File

Returns a new instance of File.



8
9
10
11
12
13
# File 'lib/dev_suite/utils/store/driver/file.rb', line 8

def initialize(**options)
  super()
  @path = options[:path] || fetch_setting(:path, default: "tmp/store.json")
  @data = {}
  load_data
end

Instance Method Details

#clearObject



29
30
31
32
# File 'lib/dev_suite/utils/store/driver/file.rb', line 29

def clear
  @data = {}
  save_data
end

#delete(key) ⇒ Object



24
25
26
27
# File 'lib/dev_suite/utils/store/driver/file.rb', line 24

def delete(key)
  Data.delete_key_by_path(@data, key)
  save_data
end

#export(destination) ⇒ Object



40
41
42
# File 'lib/dev_suite/utils/store/driver/file.rb', line 40

def export(destination)
  FileWriter.write(destination, @data)
end

#fetch(key) ⇒ Object



20
21
22
# File 'lib/dev_suite/utils/store/driver/file.rb', line 20

def fetch(key)
  Data.get_value_by_path(@data, key)
end

#import(source) ⇒ Object

Raises:

  • (ArgumentError)


34
35
36
37
38
# File 'lib/dev_suite/utils/store/driver/file.rb', line 34

def import(source)
  raise ArgumentError, "The file does not exist" unless ::File.exist?(source)

  @data = FileLoader.load(source)
end

#set(key, value) ⇒ Object



15
16
17
18
# File 'lib/dev_suite/utils/store/driver/file.rb', line 15

def set(key, value)
  Data.set_value_by_path(@data, key, value)
  save_data
end