class SwitchDb::ReferenceSet

Attributes

configuration_path[R]
references[R]

Public Class Methods

load_file(configuration_path) click to toggle source
# File lib/switch_db/reference_set.rb, line 13
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
new(configuration_path) click to toggle source
# File lib/switch_db/reference_set.rb, line 8
def initialize(configuration_path)
  @configuration_path = configuration_path
  @references = {}
end

Public Instance Methods

add_reference(reference) click to toggle source
# File lib/switch_db/reference_set.rb, line 30
def add_reference(reference)
  @references[reference.name] = reference
end
remove_reference(reference) click to toggle source
# File lib/switch_db/reference_set.rb, line 34
def remove_reference(reference)
  @references.delete(reference.name)
end
write_reference_set() click to toggle source
# File lib/switch_db/reference_set.rb, line 38
def write_reference_set
  FileUtils.mkdir_p(configuration_directory)
  File.write(@configuration_path, to_h.to_yaml)
end

Private Instance Methods

configuration_directory() click to toggle source
# File lib/switch_db/reference_set.rb, line 51
def configuration_directory
  File.dirname(@configuration_path)
end
to_h() click to toggle source
# File lib/switch_db/reference_set.rb, line 45
def to_h
  {
    references: references.values.map(&:to_h)
  }
end