Class: DatabaseRecorder::Storage::File

Inherits:
Base
  • Object
show all
Defined in:
lib/database_recorder/storage/file.rb

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from DatabaseRecorder::Storage::Base

Instance Method Details

#loadObject



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/database_recorder/storage/file.rb', line 9

def load
  stored_data = ::File.exist?(storage_path) ? ::File.read(storage_path) : false
  if stored_data
    parsed_data = YAML.load(stored_data) # rubocop:disable Security/YAMLLoad
    data = Core.symbolize_recursive(parsed_data)
    @recording.cache = data[:queries] || []
    @recording.entities = data[:entities]
    true
  else
    false
  end
end

#saveObject



22
23
24
25
26
27
28
29
30
# File 'lib/database_recorder/storage/file.rb', line 22

def save
  data = {}
  data[:metadata] = @recording. unless @recording..empty?
  data[:queries] = @recording.queries if @recording.queries.any?
  data[:entities] = @recording.entities if @recording.entities.any?
  serialized_data = ::YAML.dump(Core.string_keys_recursive(data))
  ::File.write(storage_path, serialized_data)
  true
end

#storage_pathObject



32
33
34
35
36
37
38
39
# File 'lib/database_recorder/storage/file.rb', line 32

def storage_path
  @storage_path ||= begin
    name = normalize_name(@name)
    path = @options[:recordings_path] || 'spec/dbr'
    ::FileUtils.mkdir_p(path)
    "#{path}/#{name}.yml"
  end
end