Class: PgDice::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/pgdice/configuration.rb

Overview

Configuration class which holds all configurable values

Constant Summary collapse

DEFAULT_VALUES =
{ logger_factory: proc { Logger.new($stdout) },
database_url: nil,
dry_run: false,
batch_size: 7 }.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(existing_config = nil) ⇒ Configuration

Returns a new instance of Configuration.



33
34
35
36
37
38
39
# File 'lib/pgdice/configuration.rb', line 33

def initialize(existing_config = nil)
  DEFAULT_VALUES.each do |key, value|
    initialize_value(key, value, existing_config)
  end
  @approved_tables = PgDice::ApprovedTables.new(existing_config&.approved_tables(eager_load: true)&.tables)
  initialize_objects
end

Instance Attribute Details

#approved_tables(eager_load: false) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/pgdice/configuration.rb', line 60

def approved_tables(eager_load: false)
  return @approved_tables if eager_load
  unless @approved_tables.respond_to?(:empty?)
    raise PgDice::InvalidConfigurationError, 'approved_tables must be an instance of PgDice::ApprovedTables!'
  end

  if !config_file_loader.file_loaded? && !config_file.nil?
    config_file_loader.load_file
    @approved_tables
  end

  @approved_tables
end

#batch_sizeObject



82
83
84
85
86
# File 'lib/pgdice/configuration.rb', line 82

def batch_size
  return @batch_size.to_i if @batch_size.to_i >= 0

  raise PgDice::InvalidConfigurationError, 'batch_size must be a non-negative Integer!'
end

#config_fileObject

Returns the value of attribute config_file.



31
32
33
# File 'lib/pgdice/configuration.rb', line 31

def config_file
  @config_file
end

#config_file_loaderObject



94
95
96
# File 'lib/pgdice/configuration.rb', line 94

def config_file_loader
  @config_file_loader ||= ConfigurationFileLoader.new(self)
end

#database_urlObject



54
55
56
57
58
# File 'lib/pgdice/configuration.rb', line 54

def database_url
  return @database_url unless @database_url.nil?

  raise PgDice::InvalidConfigurationError, 'database_url must be present!'
end

#dry_runObject



88
89
90
91
92
# File 'lib/pgdice/configuration.rb', line 88

def dry_run
  return @dry_run if [true, false].include?(@dry_run)

  raise PgDice::InvalidConfigurationError, 'dry_run must be either true or false!'
end

#loggerObject



98
99
100
# File 'lib/pgdice/configuration.rb', line 98

def logger
  @logger ||= logger_factory.call
end

#logger_factoryObject



48
49
50
51
52
# File 'lib/pgdice/configuration.rb', line 48

def logger_factory
  return @logger_factory if @logger_factory.respond_to?(:call)

  raise PgDice::InvalidConfigurationError, 'logger_factory must be present!'
end

#pg_connectionObject

Lazily initialized



75
76
77
78
79
80
# File 'lib/pgdice/configuration.rb', line 75

def pg_connection
  @pg_connection ||= PG::Connection.new(database_url)
  return @pg_connection if @pg_connection.respond_to?(:exec)

  raise PgDice::InvalidConfigurationError, 'pg_connection must be present!'
end

Instance Method Details

#database_connectionObject



114
115
116
# File 'lib/pgdice/configuration.rb', line 114

def database_connection
  @database_connection_factory.call
end

#deep_cloneObject



118
119
120
# File 'lib/pgdice/configuration.rb', line 118

def deep_clone
  PgDice::Configuration.new(self)
end

#partition_helperObject



106
107
108
# File 'lib/pgdice/configuration.rb', line 106

def partition_helper
  @partition_helper_factory.call
end

#partition_managerObject



102
103
104
# File 'lib/pgdice/configuration.rb', line 102

def partition_manager
  @partition_manager_factory.call
end

#validate!Object



41
42
43
44
45
46
# File 'lib/pgdice/configuration.rb', line 41

def validate!
  logger_factory
  database_url
  batch_size
  approved_tables
end

#validationObject



110
111
112
# File 'lib/pgdice/configuration.rb', line 110

def validation
  @validation_factory.call
end