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 =

Returns The default route to the yaml files containing the records path is relative to the root directory of your application and will contain a subfolder for each record loader.

Returns:

  • (Array)

    The default route to the yaml files containing the records path is relative to the root directory of your application and will contain a subfolder for each record loader.

%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

Parameters:

  • files (Array<String>, NilClass) (defaults to: nil)

    pass in an array of file names to load, or nil to load all files. Dev and wip flags will be ignored for files passed in explicitly

  • directory (Pathname, String) (defaults to: default_path)

    The directory from which to load the files. defaults to config/default_records/plate_purposes

  • dev (Boolean) (defaults to: adapter.development?)

    Override the rails environment to generate (or not) data from dev.yml 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.

    Parameters:

    • adapter (Object)

      Set the adapter to use for the class

    Returns:

    • (Object)

      The configured adapter

  • .adapterObject

    Returns the configured adapter

    Returns:

    • (Object)

      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.

    Parameters:

    • config_folder (String)

      Set the config folder for the class

    Returns:

    • (String)

      The configured config folder

  • .config_folderString

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

    Returns:

    • (String)

      The configured config folder


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

Returns:

  • (Void)

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

def create!
  adapter.transaction do
    @config.each do |key, config|
      create_or_update!(key, config)
    end
  end
end