Sha256: f36b5c5928bb723126358160d1145c303496e193ccc586fab009284fc36ff478

Contents?: true

Size: 1.59 KB

Versions: 2

Compression:

Stored size: 1.59 KB

Contents

require "toml"

module CoralBackup
  class Settings
    INITIAL_SETTINGS = {
      actions: {}
    }

    def initialize(filename = "#{Dir.home}/.coral_backup")
      @filename = filename
      @settings = file_load
    end

    def action_data(action_name)
      raise ArgumentError, "Backup action `#{action_name}' does not exist." unless exist_action?(action_name)
      @settings[:actions][action_name.to_sym]
    end

    def action_names
      @settings[:actions].keys.map(&:to_s)
    end

    def exist_action?(action_name)
      action_names.include?(action_name)
    end

    def add(action_name, source, destination, exclusions)
      raise ArgumentError, "Backup action `#{action_name}' already exists." if exist_action?(action_name)
      @settings[:actions][action_name.to_sym] = { source: source, destination: destination, exclusions: exclusions }
      file_dump
    end

    def delete(action_name)
      raise ArgumentError, "Backup action `#{action_name}' does not exist." unless exist_action?(action_name)
      @settings[:actions].delete(action_name.to_sym)
      file_dump
    end

    def update_time(action_name, time)
      raise ArgumentError, "Backup action `#{action_name}' does not exist." unless exist_action?(action_name)
      @settings[:actions][action_name.to_sym][:last_run_time] = time.to_s
      file_dump
    end

    private
    def file_load
      return INITIAL_SETTINGS unless FileTest.exist?(@filename)

      TOML.load_file(@filename, symbolize_keys: true)
    end

    def file_dump
      open(@filename, "w") do |f|
        f.puts TOML.dump(@settings)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
coral_backup-0.1.2 lib/coral_backup/settings.rb
coral_backup-0.1.1 lib/coral_backup/settings.rb