Module: NcsNavigator::Warehouse::Filters::RecordIdChangingFilterSupport

Included in:
AddIdPrefixFilter, RemoveIdPrefixFilter
Defined in:
lib/ncs_navigator/warehouse/filters/record_id_changing_filter_support.rb

Overview

A framework for a filter which modifies all ID values for a particular record type. The class into which it is mixed must implement changed_id, a method which takes an incoming ID value and provides the replacement value.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#modelClass (readonly)

Returns the warehouse model for which the ID will be prefixed.

Returns:

  • (Class)

    the warehouse model for which the ID will be prefixed.



12
13
14
# File 'lib/ncs_navigator/warehouse/filters/record_id_changing_filter_support.rb', line 12

def model
  @model
end

Instance Method Details

#call(records) ⇒ Array<MdesModel>

Modifies all IDs for the target record type according to the consumer-defined changed_id method.

Parameters:

  • records (Array<MdesModel>)

    the records to review and modify

Returns:

  • (Array<MdesModel>)

    the same records. Any IDs will be updated in place.



45
46
47
48
49
50
51
52
53
54
# File 'lib/ncs_navigator/warehouse/filters/record_id_changing_filter_support.rb', line 45

def call(records)
  records.each do |rec|
    if rec.is_a?(model)
      change_primary_key(rec)
      # see the class comment on {CompositeFilter}
      rec.instance_eval { remove_instance_variable(:@_key) if defined?(@_key) }
    end
    change_foreign_keys_if_any(rec)
  end
end

#initialize(configuration, options = {}) ⇒ Object

An inheritable constructor for filters which mix in this module.

Parameters:

  • configuration (Configuration)

    the warehouse configuration

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :table (String, Symbol)

    the name of the table for the target record type.

  • :model (String, Symbol)

    the unqualified name of the warehouse model for the target record type. If both this and :table are specified, :table wins.



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ncs_navigator/warehouse/filters/record_id_changing_filter_support.rb', line 24

def initialize(configuration, options={})
  @model =
    if options[:table]
      configuration.model(options[:table])
    elsif options[:model]
      configuration.model(options[:model])
    else
      fail 'Please specify either :table or :model.'
    end
  unless self.respond_to?(:changed_id)
    fail "#{self.class} does not implement changed_id"
  end
end