Class: RecordLoader::Base
- Inherits:
-
Object
- Object
- RecordLoader::Base
- 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
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.
%w[config default_records].freeze
Class Method Summary collapse
Instance Method Summary collapse
-
#create! ⇒ Void
Opens a transaction and creates or updates each of the records in the yml files via the #create_or_update! method.
-
#initialize(files: nil, directory: default_path, dev: adapter.development?) ⇒ Base
constructor
Create a new config loader from yaml files.
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 .adapter ⇒ Object
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_folder ⇒ String
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 |