Class: Rowr::StateSaver

Inherits:
Object
  • Object
show all
Defined in:
lib/rowr/state_saver.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(src_dir, filename) ⇒ StateSaver

Returns a new instance of StateSaver.



7
8
9
10
11
12
13
# File 'lib/rowr/state_saver.rb', line 7

def initialize(src_dir, filename)
  @src = src_dir
  @file = File.expand_path(File.join(@src, filename))
  @config = {}
  @cached = {}
  @scanned_files = []
end

Instance Attribute Details

#cachedObject

Returns the value of attribute cached.



19
20
21
# File 'lib/rowr/state_saver.rb', line 19

def cached
  @cached
end

#configObject (readonly)

Returns the value of attribute config.



17
18
19
# File 'lib/rowr/state_saver.rb', line 17

def config
  @config
end

#fileObject (readonly)

Returns the value of attribute file.



16
17
18
# File 'lib/rowr/state_saver.rb', line 16

def file
  @file
end

#scanned_filesObject

Returns the value of attribute scanned_files.



18
19
20
# File 'lib/rowr/state_saver.rb', line 18

def scanned_files
  @scanned_files
end

#srcObject

Returns the value of attribute src.



15
16
17
# File 'lib/rowr/state_saver.rb', line 15

def src
  @src
end

Instance Method Details

#config_file_exists?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/rowr/state_saver.rb', line 21

def config_file_exists?
  File.exist?(@file)
end

#load_stateObject



34
35
36
37
38
39
# File 'lib/rowr/state_saver.rb', line 34

def load_state
  file = JSON.parse(File.open(@file).read, symbolize_names: true)
  @config = file[:config]
  @cached = file[:cached]
  @scanned_files = file[:scanned_files]
end

#save_config(config) ⇒ Object



41
42
43
44
# File 'lib/rowr/state_saver.rb', line 41

def save_config(config)
  @config = config
  save_state
end

#save_stateObject



25
26
27
28
29
30
31
32
# File 'lib/rowr/state_saver.rb', line 25

def save_state
  hashed = {
    config: @config,
    cached: @cached,
    scanned_files: scanned_files
  }
  File.open(@file, 'wb') { |f| f.write JSON.pretty_generate(hashed) }
end