Sha256: e16114f1b15cd2fc81c281c1aaeda9e421debbd06aecf44e263948cc1df19320

Contents?: true

Size: 1.64 KB

Versions: 1

Compression:

Stored size: 1.64 KB

Contents

require 'json'
require 'fileutils'
require 'logger'
module JSONORM
  class DB
    attr_reader :file_path, :backup_path, :logger

    def initialize(file_path, log_file)
      @file_path = file_path
      @backup_path = "#{file_path}.backup"
      @logger = Logger.new(log_file)
      initialize_file unless File.exist?(file_path)
    end

    def read
      with_lock do
        JSON.parse(File.read(file_path), symbolize_names: true)
      rescue JSON::ParserError
        raise "Error parsing JSON data in #{file_path}"
      end
    end

  def write(data)
    with_lock do
      create_backup
      File.open(file_path, 'w') { |f| f.write(JSON.pretty_generate(data)) }
      logger.info("Data written successfully")
    rescue IOError => e
      restore_backup
      logger.error("Error writing to file: #{e.message}")
      raise "Error writing to file: #{e.message}"
    end
  end


    private

    def initialize_file
      with_lock { File.open(file_path, 'w') { |f| f.write('[]') } }
    end

    def create_backup
      logger.info("Creating backup")
      FileUtils.cp(file_path, backup_path)
    rescue => e
      logger.error("Failed to create backup: #{e.message}")
      raise "Failed to create backup: #{e.message}"
    end

    def restore_backup
      logger.info("Restoring from backup")
      FileUtils.cp(backup_path, file_path)
    rescue => e
      logger.error("Failed to restore backup: #{e.message}")
      raise "Failed to restore backup: #{e.message}"
    end


    def with_lock
      File.open("#{file_path}.lock", 'w') do |f|
        f.flock(File::LOCK_EX)
        yield
      ensure
        f.flock(File::LOCK_UN)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
json-orm-0.1.0 lib/json-orm/db.rb