Class: NcsNavigator::Warehouse::Transformers::EnumTransformer
- Inherits:
-
Object
- Object
- NcsNavigator::Warehouse::Transformers::EnumTransformer
- Extended by:
- Forwardable
- Includes:
- StringifyTrace, RecordIdent
- Defined in:
- lib/ncs_navigator/warehouse/transformers/enum_transformer.rb
Overview
A transformer that accepts a series of model instances and NcsNavigator::Warehouse::TransformErrors in the form of a ruby Enumerable. An enumerable might be as simple as an array, or it might be a custom class that streams through thousands of instances without having them all in memory at once.
Each value yielded by the enumerable may be either an instance of an MDES model or a NcsNavigator::Warehouse::TransformError. If it is a model instance, it will have global values (e.g., PSU ID) applied as necessary, validated, and saved.
On the other hand, If it is a TransformError
the error will be
associated with the status for the transform run. The benefit of
the enumeration yielding a TransformError
instead of throwing an
exception is that the enumeration may continue after the error is
reported. If the error is unrecoverable, the enum should throw an
exception instead of returning a
TransformError
. EnumTransformer
will handle recording the
error appropriately in that case.
Defined Under Namespace
Modules: RecordIdent Classes: ForeignKeyChecker, PsuIdChecker, ValidateRecordChecker
Instance Attribute Summary collapse
-
#duplicates ⇒ Symbol
readonly
transformer.
-
#enum ⇒ Enumerable
readonly
The enumeration that will be transformed.
-
#filters ⇒ CompositeFilter
readonly
The filters in use on this transformer.
Instance Method Summary collapse
-
#initialize(configuration, enum, options = {}) ⇒ EnumTransformer
constructor
A new instance of EnumTransformer.
-
#name ⇒ String
A human-readable name for this transformer.
-
#transform(status)
Takes each in-memory record provided by the configured
Enumerable
, validates it, and saves it if it is valid.
Methods included from RecordIdent
Methods included from StringifyTrace
Constructor Details
#initialize(configuration, enum, options = {}) ⇒ EnumTransformer
Returns a new instance of EnumTransformer.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/ncs_navigator/warehouse/transformers/enum_transformer.rb', line 59 def initialize(configuration, enum, ={}) @configuration = configuration @enum = enum filter_list = .delete(:filters) @filters = NcsNavigator::Warehouse::Filters::CompositeFilter.new( filter_list ? [*filter_list].compact : []) @duplicates = .delete(:duplicates) || :error @duplicates_strategy = select_duplicates_strategy @record_checkers = { :validation => ValidateRecordChecker.new(log), :foreign_key => ForeignKeyChecker.new(log, foreign_key_index), :psus => PsuIdChecker.new(log, @configuration.navigator.psus) } end |
Instance Attribute Details
#duplicates ⇒ Symbol (readonly)
transformer.
42 43 44 |
# File 'lib/ncs_navigator/warehouse/transformers/enum_transformer.rb', line 42 def duplicates @duplicates end |
#enum ⇒ Enumerable (readonly)
Returns the enumeration that will be transformed.
33 34 35 |
# File 'lib/ncs_navigator/warehouse/transformers/enum_transformer.rb', line 33 def enum @enum end |
#filters ⇒ CompositeFilter (readonly)
Returns the filters in use on this transformer.
37 38 39 |
# File 'lib/ncs_navigator/warehouse/transformers/enum_transformer.rb', line 37 def filters @filters end |
Instance Method Details
#name ⇒ String
A human-readable name for this transformer.
79 80 81 82 83 84 85 86 |
# File 'lib/ncs_navigator/warehouse/transformers/enum_transformer.rb', line 79 def name enum_name = if enum.respond_to?(:name) enum.name else enum.class end "EnumTransformer for #{enum_name}" end |
#transform(status)
This method returns an undefined value.
Takes each in-memory record provided by the configured
Enumerable
, validates it, and saves it if it is valid.
94 95 96 97 98 99 100 101 102 |
# File 'lib/ncs_navigator/warehouse/transformers/enum_transformer.rb', line 94 def transform(status) begin do_transform(status) rescue => e err = NcsNavigator::Warehouse::TransformError.for_exception(e, 'Enumeration failed.') log.error err. status.transform_errors << err end end |