Class: StatusPageRuby::Storage

Inherits:
Object
  • Object
show all
Defined in:
lib/status_page_ruby/storage.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data_file_path) ⇒ Storage



5
6
7
8
# File 'lib/status_page_ruby/storage.rb', line 5

def initialize(data_file_path)
  validate_file(data_file_path)
  @data_file_path = data_file_path
end

Instance Attribute Details

#data_file_pathObject (readonly)

Returns the value of attribute data_file_path.



3
4
5
# File 'lib/status_page_ruby/storage.rb', line 3

def data_file_path
  @data_file_path
end

Instance Method Details

#copy(target_file_path) ⇒ Object



33
34
35
36
# File 'lib/status_page_ruby/storage.rb', line 33

def copy(target_file_path)
  FileUtils.mkpath(File.dirname(target_file_path))
  FileUtils.cp(data_file_path, target_file_path)
end

#include?(record) ⇒ Boolean



10
11
12
# File 'lib/status_page_ruby/storage.rb', line 10

def include?(record)
  read.lazy.include?(record)
end

#merge(records) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/status_page_ruby/storage.rb', line 24

def merge(records)
  updated_records = merge_records(records)
  CSV.open(data_file_path, 'w') do |csv|
    updated_records.each do |record|
      csv << record
    end
  end
end

#readObject



14
15
16
# File 'lib/status_page_ruby/storage.rb', line 14

def read
  CSV.foreach(data_file_path)
end

#restore(new_file_path) ⇒ Object



38
39
40
41
# File 'lib/status_page_ruby/storage.rb', line 38

def restore(new_file_path)
  validate_file(new_file_path)
  merge(CSV.foreach(new_file_path).to_a)
end

#write(record) ⇒ Object



18
19
20
21
22
# File 'lib/status_page_ruby/storage.rb', line 18

def write(record)
  CSV.open(data_file_path, 'a') do |csv|
    csv << record
  end
end