Module: Importu::ConfigDSL
- Included in:
- Definition, Importer
- Defined in:
- lib/importu/config_dsl.rb
Overview
DSL methods for configuring importers.
These methods define your import specification: what fields to extract, how to convert them, where to persist, and what actions are allowed.
## Field Definition
-
#field - Define a single field with options
-
#fields - Define multiple fields at once
-
#converter - Create a custom converter
-
#convert_to - Reference a built-in or custom converter
## Persistence
-
#model - Connect to a model class for persistence
-
#find_by - Specify how to find existing records
-
#allow_actions - Control create/update permissions
-
#before_save - Hook to modify records before saving
## Source Configuration
-
#source - Configure source-specific options
Defined Under Namespace
Classes: ConverterStub
Instance Method Summary collapse
-
#allow_actions(*actions) ⇒ void
Specifies which persistence actions are allowed during import.
-
#before_save { ... } ⇒ void
Defines a callback to execute just before saving a record.
-
#config ⇒ Hash
Returns the current configuration hash for this importer.
-
#convert_to(type, **options) ⇒ ConverterStub
Creates a converter reference for use with field definitions.
-
#converter(name) {|field_name, **options| ... } ⇒ void
Defines a custom converter for use in field definitions.
-
#field(name, **props) {|field_name| ... } ⇒ void
Defines a single field with its configuration.
-
#fields(*names, **props) { ... } ⇒ void
Defines multiple fields with the same configuration.
-
#find_by(*field_groups) {|record| ... } ⇒ void
Specifies how to find existing records for updates.
-
#model(name, backend: nil) ⇒ void
Associates the importer with a model class for persistence.
-
#source(type, **props) ⇒ void
Configures source-specific options.
Instance Method Details
#allow_actions(*actions) ⇒ void
This method returns an undefined value.
Specifies which persistence actions are allowed during import.
66 67 68 69 70 |
# File 'lib/importu/config_dsl.rb', line 66 def allow_actions(*actions) @config = { **config, backend: { **config[:backend], allowed_actions: actions.compact } } end |
#before_save { ... } ⇒ void
This method returns an undefined value.
Defines a callback to execute just before saving a record.
The block is executed in the context of an AssignmentContext, which provides access to:
-
object: the model instance being saved
-
record: the converted record data
-
action: :create or :update
This is a backend hook. Backends may choose to honor it or ignore it. The ActiveRecord backend executes it after assigning field values but before calling save!.
265 266 267 268 269 |
# File 'lib/importu/config_dsl.rb', line 265 def before_save(&block) @config = { **config, backend: { **config[:backend], before_save: block } } end |
#config ⇒ Hash
Returns the current configuration hash for this importer.
48 49 50 51 52 |
# File 'lib/importu/config_dsl.rb', line 48 def config @config ||= superclass.respond_to?(:config) \ ? superclass.config : default_config end |
#convert_to(type, **options) ⇒ ConverterStub
Creates a converter reference for use with field definitions.
85 86 87 88 |
# File 'lib/importu/config_dsl.rb', line 85 def convert_to(type, **) config[:converters].fetch(type) # validate converter exists ConverterStub.for(type, **) end |
#converter(name) {|field_name, **options| ... } ⇒ void
This method returns an undefined value.
Defines a custom converter for use in field definitions.
112 113 114 115 116 |
# File 'lib/importu/config_dsl.rb', line 112 def converter(name, &block) @config = { **config, converters: { **config[:converters], name => block } } end |
#field(name, **props) {|field_name| ... } ⇒ void
This method returns an undefined value.
Defines a single field with its configuration.
146 147 148 149 150 151 152 153 154 155 |
# File 'lib/importu/config_dsl.rb', line 146 def field(name, **props, &block) field = config[:fields].fetch(name, field_defaults(name)) props[:converter] = block if block @config = { **config, fields: { **config[:fields], name => { **field, **props } } } end |
#fields(*names, **props) { ... } ⇒ void
This method returns an undefined value.
Defines multiple fields with the same configuration.
171 172 173 |
# File 'lib/importu/config_dsl.rb', line 171 def fields(*names, **props, &block) names.each {|name| field(name, **props, &block) } end |
#find_by(*field_groups) {|record| ... } ⇒ void
This method returns an undefined value.
Specifies how to find existing records for updates.
197 198 199 200 201 202 203 204 205 |
# File 'lib/importu/config_dsl.rb', line 197 def find_by(*field_groups, &block) finder_fields = [*field_groups, block].compact.map do |field_group| field_group.respond_to?(:call) ? field_group : Array(field_group) end @config = { **config, backend: { **config[:backend], finder_fields: finder_fields } } end |
#model(name, backend: nil) ⇒ void
This method returns an undefined value.
Associates the importer with a model class for persistence.
The backend can be:
-
nil or :auto - auto-detect from registered backends
-
:active_record - use ActiveRecord backend explicitly
-
Any other symbol registered with Importu::Backends.registry
230 231 232 233 234 |
# File 'lib/importu/config_dsl.rb', line 230 def model(name, backend: nil) @config = { **config, backend: { **config[:backend], name: backend, model: name } } end |
#source(type, **props) ⇒ void
This method returns an undefined value.
Configures source-specific options.
284 285 286 287 288 289 290 291 292 |
# File 'lib/importu/config_dsl.rb', line 284 def source(type, **props) source = config[:sources].fetch(type, {}) @config = { **config, sources: { **config[:sources], type => { **source, **props } } } end |