class StatusPageRuby::Storage

Attributes

data_file_path[R]

Public Class Methods

new(data_file_path) click to toggle source
# 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

Public Instance Methods

copy(target_file_path) click to toggle source
# 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) click to toggle source
# File lib/status_page_ruby/storage.rb, line 10
def include?(record)
  read.lazy.include?(record)
end
merge(records) click to toggle source
# 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
read() click to toggle source
# File lib/status_page_ruby/storage.rb, line 14
def read
  CSV.foreach(data_file_path)
end
restore(new_file_path) click to toggle source
# 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) click to toggle source
# File lib/status_page_ruby/storage.rb, line 18
def write(record)
  CSV.open(data_file_path, 'a') do |csv|
    csv << record
  end
end

Private Instance Methods

merge_records(records) click to toggle source
# File lib/status_page_ruby/storage.rb, line 51
def merge_records(records)
  (read.to_a | records).sort_by(&:last)
end
validate_file(path) click to toggle source
# File lib/status_page_ruby/storage.rb, line 45
def validate_file(path)
  return if File.file?(path.to_s) && File.readable?(path.to_s)

  raise ArgumentError, 'Invalid file given.'
end