Class: NcsNavigator::Warehouse::Transformers::Database::OneForOneProducer
- Inherits:
-
RecordProducer
- Object
- Struct
- RecordProducer
- NcsNavigator::Warehouse::Transformers::Database::OneForOneProducer
- 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
-
#dsl_host ⇒ Object
readonly
Returns the value of attribute dsl_host.
-
#model_or_reference ⇒ Object
readonly
Returns the value of attribute model_or_reference.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Attributes inherited from RecordProducer
Instance Method Summary collapse
-
#arity ⇒ Object
Implemented so that this class behaves like a lambda.
-
#column_map(column_names, configuration) ⇒ Hash<String, String>
A mapping from the given column names to MDES variable names for the configured model.
-
#convert_row(row, meta) ⇒ Object
(also: #call)
Produces a single instance of #model using the values in the row as mapped by #column_map.
-
#initialize(name, query, model_or_reference, dsl_host, options) ⇒ OneForOneProducer
constructor
A new instance of OneForOneProducer.
-
#model(configuration) ⇒ Class
The model for this producer.
Constructor Details
#initialize(name, query, model_or_reference, dsl_host, options) ⇒ OneForOneProducer
Returns a new instance of 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, ) super(name, query, self) @model_or_reference = model_or_reference @dsl_host = dsl_host @options = end |
Instance Attribute Details
#dsl_host ⇒ Object (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_reference ⇒ Object (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 |
#options ⇒ Object (readonly)
Returns the value of attribute options.
325 326 327 |
# File 'lib/ncs_navigator/warehouse/transformers/database.rb', line 325 def @options end |
Instance Method Details
#arity ⇒ Object
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>
Returns a mapping from the given column names to MDES variable names for the configured model. This mapping reflects both the configured explicit mapping and the heuristic.
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 -= [:column_map].values column_names.inject([:column_map].dup) do |map, column| column = column.to_s prop = unless [:column_map][column] [ [//, ''], [/_code$/, ''], [/_code$/, '_id'], [/_other$/, '_oth'], ].collect do |pattern, substitution| if column =~ pattern prefixed_property_name(available_props, column.sub(pattern, substitution), [: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, ) col_map = column_map(row.members, [:configuration]) unused = row.members.collect(&:to_s) - col_map.keys - ignored_columns if on_unused == :fail && !unused.empty? raise UnusedColumnsForModelError.new(unused) end model([:configuration]).new( col_map.inject({}) { |pv, (col_name, var_name)| pv[var_name] = clean_value(row[col_name]); pv } ) end |
#model(configuration) ⇒ Class
Returns the model for this producer. If #model_or_reference is a symbolic reference, this method resolves it using the given configuration.
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 |