Sha256: 0db75ac965bf41c9ad89a3749debdcc916bfe10e8fb7b2731e215003e7e226ae

Contents?: true

Size: 1.18 KB

Versions: 2

Compression:

Stored size: 1.18 KB

Contents

require 'fileutils'
require 'yaml'

module SwitchDb
  class ReferenceSet
    attr_reader :configuration_path, :references

    def initialize(configuration_path)
      @configuration_path = configuration_path
      @references = {}
    end

    def self.load_file(configuration_path)
      new(configuration_path).tap do |instance|
        yaml = YAML.load_file(configuration_path)

        (yaml[:references] || []).map do |reference|
          reference = Reference.new(
            name: reference[:name],
            database_names: reference[:database_names]
          )

          instance.add_reference(reference)
        end
      end
    rescue Errno::ENOENT
      new(configuration_path)
    end

    def add_reference(reference)
      @references[reference.name] = reference
    end

    def remove_reference(reference)
      @references.delete(reference.name)
    end

    def write_reference_set
      FileUtils.mkdir_p(configuration_directory)
      File.write(@configuration_path, to_h.to_yaml)
    end

    private

    def to_h
      {
        references: references.values.map(&:to_h)
      }
    end

    def configuration_directory
      File.dirname(@configuration_path)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
switch_db-0.2.0 lib/switch_db/reference_set.rb
switch_db-0.1.0 lib/switch_db/reference_set.rb