Class: RecordLoader::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/record_loader/base.rb

Overview

Inherit from RecordLoader base to automatically load one or more yaml files into a @config hash. Config folders are found in config/default_records and each loader should specify its own subfolder by setting the config_folder class attribute.

Direct Known Subclasses

ApplicationRecordLoader

Constant Summary collapse

BASE_CONFIG_PATH =
%w[config default_records].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(files: nil, directory: default_path, dev: adapter.development?) ⇒ Base

Create a new config loader from yaml files



66
67
68
69
70
71
72
73
# File 'lib/record_loader/base.rb', line 66

def initialize(files: nil, directory: default_path, dev: adapter.development?)
  @path = directory.is_a?(Pathname) ? directory : Pathname.new(directory)

  list = Filter.create(files: files, dev: dev, wip_list: wip_list)
  @files = @path.glob("*#{RecordFile::EXTENSION}")
                .select { |child| list.include?(RecordFile.new(child)) }
  load_config
end

Class Method Details

.adapter(adapter) ⇒ Object .adapterObject

Overloads:

  • .adapter(adapter) ⇒ Object

    Sets the adapter to use for the record loader, see the adapter for further information.

  • .adapterObject

    Returns the configured adapter



49
50
51
52
# File 'lib/record_loader/base.rb', line 49

def adapter(adapter = nil)
  @adapter = adapter unless adapter.nil?
  @adapter || superclass.adapter
end

.config_folder(config_folder) ⇒ String .config_folderString

Overloads:

  • .config_folder(config_folder) ⇒ String

    Sets the folder, located under BASE_CONFIG_PATH, from which the records will be loaded.

  • .config_folderString

    Returns the folder, located under BASE_CONFIG_PATH, from which the records will be loaded.



31
32
33
34
# File 'lib/record_loader/base.rb', line 31

def config_folder(config_folder = nil)
  @config_folder = config_folder unless config_folder.nil?
  @config_folder
end

Instance Method Details

#create!Void

Opens a transaction and creates or updates each of the records in the yml files via the #create_or_update! method



80
81
82
83
84
85
86
87
88
# File 'lib/record_loader/base.rb', line 80

def create!
  adapter.transaction do
    @config.each do |key, config|
      create_or_update!(key, config)
    rescue StandardError => e
      raise StandardError, "Failed to create #{key} due to: #{e.message}"
    end
  end
end