Class: PgObjects::Manager

Inherits:
Object
  • Object
show all
Defined in:
lib/pg_objects/manager.rb

Overview

Manages process to create objects

Usage (dependencies are auto-injected, keyword overrides optional):

Manager.new.load_files(:before).create_objects

or

Manager.new(config: custom_config, logger: custom_logger).load_files(:after).create_objects

Pass connection: to run against a specific database connection instead of the global one (Rails 6+ multi-DB):

Manager.new(connection: AnimalsRecord.connection).load_files(:before).create_objects

Instance Method Summary collapse

Constructor Details

#initialize(connection: nil, **deps) ⇒ Manager

Returns a new instance of Manager.



21
22
23
24
# File 'lib/pg_objects/manager.rb', line 21

def initialize(connection: nil, **deps)
  super(**deps)
  @connection = connection
end

Instance Method Details

#create_objectsObject



45
46
47
48
# File 'lib/pg_objects/manager.rb', line 45

def create_objects
  build_objects_index
  within_transaction { objects.each { |obj| create_object(obj) } }
end

#load_files(event) ⇒ Object

event: :before or :after

used to reference configuration settings before_path and after_path

Resets the object list before loading, so each call reflects only the files for the given event.



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/pg_objects/manager.rb', line 33

def load_files(event)
  validate_workability

  objects.clear
  dir = config.send "#{event}_path"
  Dir[File.join(dir, '**', "*.{#{config.extensions.join(',')}}")].each do |path|
    objects << db_object_factory.create_instance(path)
  end

  self
end

#objectsObject



50
51
52
# File 'lib/pg_objects/manager.rb', line 50

def objects
  @objects ||= []
end