Class: NcsNavigator::Warehouse::Transformers::EnumTransformer

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Methods included from RecordIdent

#record_ident

Methods included from StringifyTrace

stringify_trace

Constructor Details

#initialize(configuration, enum, options = {}) ⇒ EnumTransformer

Returns a new instance of EnumTransformer.

Parameters:

  • configuration (Configuration)
  • enum (Enumerable)
  • options (Hash<Symbol, Object>) (defaults to: {})

Options Hash (options):

  • :filters (Array<#call>, #call)

    a list of filters to use for this transformer

  • the (:error, :ignore, :replace)

    desired behavior when this transformer encounters a record with the same PK as one already seen. :error means blindly save (and let the database error out). :ignore means do not attempt to save the duplicate. :replace means substitute the duplicate for the existing record.

See Also:

  • CompositeFilter


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, options={})
  @configuration = configuration
  @enum = enum
  filter_list = options.delete(:filters)
  @filters = NcsNavigator::Warehouse::Filters::CompositeFilter.new(
    filter_list ? [*filter_list].compact : [])
  @duplicates = options.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

#duplicatesSymbol (readonly)

transformer.

Returns:

  • (Symbol)

    the name of the duplicates mode in use on this



42
43
44
# File 'lib/ncs_navigator/warehouse/transformers/enum_transformer.rb', line 42

def duplicates
  @duplicates
end

#enumEnumerable (readonly)

Returns the enumeration that will be transformed.

Returns:

  • (Enumerable)

    the enumeration that will be transformed.



33
34
35
# File 'lib/ncs_navigator/warehouse/transformers/enum_transformer.rb', line 33

def enum
  @enum
end

#filtersCompositeFilter (readonly)

Returns the filters in use on this transformer.

Returns:

  • (CompositeFilter)

    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

#nameString

A human-readable name for this transformer.

Returns:

  • (String)


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.

Parameters:



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.message
    status.transform_errors << err
  end
end