Class: FuryDumper::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/fury_dumper/config.rb

Constant Summary collapse

MODES =
%i[wide depth].freeze
FALSE_VALUES =
[false, 0, '0', 'f', 'F', 'false', 'FALSE', 'off', 'OFF'].freeze

Class Method Summary collapse

Class Method Details

.batch_sizeObject



29
30
31
# File 'lib/fury_dumper/config.rb', line 29

def self.batch_size
  @batch_size ||= (config['batch_size'] || 100).to_i
end

.check_presented(config, prefix) ⇒ Object



93
94
95
96
97
# File 'lib/fury_dumper/config.rb', line 93

def self.check_presented(config, prefix)
  return if config.present?

  raise "Configuration error! #{prefix} isn't describe"
end

.check_required_key(config, field, prefix) ⇒ Object



99
100
101
102
103
# File 'lib/fury_dumper/config.rb', line 99

def self.check_required_key(config, field, prefix)
  return if config[field]

  raise "Configuration error! #{prefix} #{field} expected"
end

.configObject



10
11
12
# File 'lib/fury_dumper/config.rb', line 10

def self.config
  @config || {}
end

.exclude_relation?(relation_name) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/fury_dumper/config.rb', line 41

def self.exclude_relation?(relation_name)
  exclude_relations.include?(relation_name)
end

.exclude_relationsObject



109
110
111
# File 'lib/fury_dumper/config.rb', line 109

def self.exclude_relations
  @exclude_relations ||= config['exclude_relations']&.split(',')&.map(&:strip) || []
end

.fast?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/fury_dumper/config.rb', line 59

def self.fast?
  !config['fast'].in?(FALSE_VALUES)
end

.fetch_service_config(ms_name) ⇒ Object



51
52
53
# File 'lib/fury_dumper/config.rb', line 51

def self.fetch_service_config(ms_name)
  relative_services[ms_name]
end

.limitObject



33
34
35
# File 'lib/fury_dumper/config.rb', line 33

def self.limit
  batch_size * ratio_records_batches
end

.load(file) ⇒ Object



14
15
16
17
# File 'lib/fury_dumper/config.rb', line 14

def self.load(file)
  @config = ::YAML.safe_load(file.respond_to?(:read) ? file : File.open(file))
  validate_config
end

.modeObject



45
46
47
48
49
# File 'lib/fury_dumper/config.rb', line 45

def self.mode
  @mode ||= config['mode'].to_sym if !@mode && MODES.include?(config['mode']&.to_sym)

  @mode ||= :wide
end

.ms_relations?(table_name) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/fury_dumper/config.rb', line 37

def self.ms_relations?(table_name)
  tables.include?(table_name)
end

.ratio_records_batchesObject



105
106
107
# File 'lib/fury_dumper/config.rb', line 105

def self.ratio_records_batches
  @ratio_records_batches ||= (config['ratio_records_batches'] || 10).to_i
end

.relative_servicesObject



55
56
57
# File 'lib/fury_dumper/config.rb', line 55

def self.relative_services
  config['relative_services']
end

.tablesObject



19
20
21
22
23
24
25
26
27
# File 'lib/fury_dumper/config.rb', line 19

def self.tables
  return @tables if @tables

  @tables = []
  relative_services&.each do |_ms_name, ms_config|
    @tables += ms_config['tables'].keys
  end
  @tables
end

.validate_configObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/fury_dumper/config.rb', line 63

def self.validate_config
  return true unless relative_services

  relative_services.each do |ms_name, ms_config|
    check_presented(ms_config, "[#{ms_name}]")
    %w[database host port user password].each do |required_field|
      check_required_key(ms_config, required_field, "[#{ms_name}]")
    end

    check_presented(ms_config['tables'], "[#{ms_name}] tables")
    validate_tables_config(ms_config['tables'], ms_name)
  end

  true
end

.validate_tables_config(config, ms_name) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/fury_dumper/config.rb', line 79

def self.validate_tables_config(config, ms_name)
  config.each do |this_table, table_config|
    check_presented(table_config, "[#{ms_name}] -> #{this_table}")

    table_config.each do |ms_table, ms_table_config|
      check_presented(ms_table_config, "[#{ms_name}] -> #{this_table} -> #{ms_table}")

      %w[self_field_name ms_model_name ms_field_name].each do |required_field|
        check_required_key(ms_table_config, required_field, "[#{ms_name}] -> #{this_table} -> #{ms_table}")
      end
    end
  end
end