Class: NcsNavigator::Warehouse::Transformers::Database::OneForOneProducer

Inherits:
RecordProducer
  • Object
show all
Defined in:
lib/ncs_navigator/warehouse/transformers/database.rb

Overview

The class encapsulating one call to DSL#produce_one_for_one

Instance Attribute Summary collapse

Attributes inherited from RecordProducer

#name, #query, #row_processor

Instance Method Summary collapse

Constructor Details

#initialize(name, query, model_or_reference, dsl_host, options) ⇒ OneForOneProducer



327
328
329
330
331
332
# File 'lib/ncs_navigator/warehouse/transformers/database.rb', line 327

def initialize(name, query, model_or_reference, dsl_host, options)
  super(name, query, self)
  @model_or_reference = model_or_reference
  @dsl_host = dsl_host
  @options = options
end

Instance Attribute Details

#dsl_hostObject (readonly)

Returns the value of attribute dsl_host.



325
326
327
# File 'lib/ncs_navigator/warehouse/transformers/database.rb', line 325

def dsl_host
  @dsl_host
end

#model_or_referenceObject (readonly)

Returns the value of attribute model_or_reference.



325
326
327
# File 'lib/ncs_navigator/warehouse/transformers/database.rb', line 325

def model_or_reference
  @model_or_reference
end

#optionsObject (readonly)

Returns the value of attribute options.



325
326
327
# File 'lib/ncs_navigator/warehouse/transformers/database.rb', line 325

def options
  @options
end

Instance Method Details

#arityObject

Implemented so that this class behaves like a lambda.



355
# File 'lib/ncs_navigator/warehouse/transformers/database.rb', line 355

def arity; 2; end

#column_map(column_names, configuration) ⇒ Hash<String, String>



391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
# File 'lib/ncs_navigator/warehouse/transformers/database.rb', line 391

def column_map(column_names, configuration)
  available_props = model(configuration).properties.collect { |p| p.name.to_s }
  available_props -= options[:column_map].values

  column_names.inject(options[:column_map].dup) do |map, column|
    column = column.to_s
    prop =
      unless options[:column_map][column]
        [
          [//,        ''],
          [/_code$/,  ''],
          [/_code$/,  '_id'],
          [/_other$/, '_oth'],
        ].collect do |pattern, substitution|
          if column =~ pattern
            prefixed_property_name(available_props,
              column.sub(pattern, substitution), options[:prefix])
          end
        end.compact.first
      end
    if prop
      available_props.delete(prop)
      map[column] = prop
    end
    map
  end
end

#convert_row(row, meta) ⇒ Object Also known as: call

Produces a single instance of #model using the values in the row as mapped by #column_map.



337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/ncs_navigator/warehouse/transformers/database.rb', line 337

def convert_row(row, meta)
  col_map = column_map(row.members, meta[:configuration])
  unused = row.members.collect(&:to_s) - col_map.keys - ignored_columns

  if on_unused == :fail && !unused.empty?
    raise UnusedColumnsForModelError.new(unused)
  end
  model(meta[:configuration]).new(
    col_map.inject({}) { |pv, (col_name, var_name)|
      pv[var_name] = clean_value(row[col_name]);
      pv
    }
  )
end

#model(configuration) ⇒ Class



363
364
365
366
367
368
369
370
371
# File 'lib/ncs_navigator/warehouse/transformers/database.rb', line 363

def model(configuration)
  case model_or_reference
  when Class
    model_or_reference
  else
    configuration.model(model_or_reference) or
      fail("There is no table or model named #{model_or_reference.inspect} in MDES #{configuration.mdes_version}.")
  end
end