Class: SwitchDb::ReferenceSet

Inherits:
Object
  • Object
show all
Defined in:
lib/switch_db/reference_set.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(directory_path) ⇒ ReferenceSet



9
10
11
12
# File 'lib/switch_db/reference_set.rb', line 9

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

Instance Attribute Details

#directory_pathObject (readonly)

Returns the value of attribute directory_path.



7
8
9
# File 'lib/switch_db/reference_set.rb', line 7

def directory_path
  @directory_path
end

#referencesObject (readonly)

Returns the value of attribute references.



7
8
9
# File 'lib/switch_db/reference_set.rb', line 7

def references
  @references
end

Class Method Details

.load_from_directory(directory_path) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/switch_db/reference_set.rb', line 14

def self.load_from_directory(directory_path)
  new(directory_path).tap do |instance|
    directories = Dir[File.join(directory_path, '*')].select { |path| File.directory?(path) }

    directories.each do |path|
      reference_name = File.basename(path)
      sql_file_paths = Dir[File.join(path, '*.sql')].select { |file_path| File.file?(file_path) }
      database_names = sql_file_paths.map { |file_path| File.basename(file_path).gsub(/\.sql$/, '') }

      reference = Reference.new(
        name: reference_name,
        database_names: database_names
      )

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

Instance Method Details

#add_reference(reference) ⇒ Object



35
36
37
# File 'lib/switch_db/reference_set.rb', line 35

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

#remove_reference(reference) ⇒ Object



39
40
41
# File 'lib/switch_db/reference_set.rb', line 39

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